// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsAzureMmc.SnapIn.Views
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.ManagementConsole;
    using System.Windows.Forms;

    public class QueueFormView : FormView
    {
        #region members

        private Forms.QueueControl _control = null;
        private AzureManagerSnapIn _parentSnapIn = null;

        #endregion

        /// <summary>
        /// Basic constructor
        /// </summary>
        public QueueFormView()
        {
            _parentSnapIn = (AzureManagerSnapIn)this.SnapIn;
        }

        /// <summary>
        /// Setup event handlers to handle actions triggered from the control
        /// </summary>
        /// <param name="status"></param>
        protected override void OnInitialize(AsyncStatus status)
        {
            base.OnInitialize(status);

            _control = (Forms.QueueControl)this.Control;
            _control.SetupView(this);
            _control.Dock = DockStyle.Fill;

            //Set handlers for the controls events
            _control.GetMessagesForQueue += new Forms.ActionNamedItemDelegate(control_GetMessagesForQueue);
            _control.UpdateQueues += new Forms.ActionDelegate(control_UpdateQueues);
            _control.AddNewQueue += new Forms.ActionDelegate(control_AddNewQueue);
            _control.AddMessage += new Forms.ActionNamedItemDelegate(control_AddMessage);
            _control.DeleteQueue += new Forms.ActionNamedItemDelegate(control_DeleteQueue);
            _control.PurgeQueue += new Forms.ActionNamedItemDelegate(control_PurgeQueue);
            _control.ShowRightClick += new Forms.ActionShowRightClickDelegate(_control_ShowRightClick);
            control_UpdateQueues();
        }

        /// <summary>
        /// Show the right-click menu 
        /// </summary>
        /// <param name="location"></param>
        void _control_ShowRightClick(System.Drawing.Point location)
        {
            this.ShowContextMenu(location, true);
        }

        /// <summary>
        /// Purge messages from a queue
        /// </summary>
        /// <param name="queueName"></param>
        void control_PurgeQueue(string queueName)
        {
            if (_parentSnapIn != null)
            {
                //Try a purge
                _parentSnapIn.PurgeQueue(queueName);
            }
        }

        /// <summary>
        /// Delete a queue
        /// </summary>
        /// <param name="queueName"></param>
        void control_DeleteQueue(string queueName)
        {
            if (_parentSnapIn != null)
            {
                //Try a delete
                _parentSnapIn.DeleteQueue(queueName);
            }
        }

        /// <summary>
        /// Add a message to a queue
        /// </summary>
        /// <param name="queueName"></param>
        void control_AddMessage(string queueName)
        {
            if (_parentSnapIn != null)
            {
                using (Forms.MessageCreate msgForm = new Forms.MessageCreate())
                {
                    //Show the blob container add form.
                    if (msgForm.ShowDialog() == DialogResult.OK)
                    {
                        //Add the new container
                        _parentSnapIn.AddMessage(queueName, msgForm.Message);
                        // refresh the message count in the queue
                        _control.DisplayedQueues = _parentSnapIn.GetQueues();
                    }
                }
            }
        }

        /// <summary>
        /// Create a new queue
        /// </summary>
        void control_AddNewQueue()
        {
            if (_parentSnapIn != null)
            {
                using (Forms.QueueForm queueForm = new Forms.QueueForm())
                {
                    //Show the blob container add form.
                    if (queueForm.ShowDialog() == DialogResult.OK)
                    {
                        //Add the new container
                        _parentSnapIn.AddQueue(queueForm.QueueName);
                    }
                }
            }
        }

        /// <summary>
        /// Refresh the list of queues
        /// </summary>
        void control_UpdateQueues()
        {
            if (_parentSnapIn != null) _control.DisplayedQueues = _parentSnapIn.GetQueues();
        }

        /// <summary>
        /// Refresh the list of messages in a queue
        /// </summary>
        /// <param name="name"></param>
        void control_GetMessagesForQueue(string queueName)
        {
            //Set the displayed blobs according to the snapin results
            if (_parentSnapIn != null) _control.DisplayedMessages = _parentSnapIn.GetMessagesFromQueue(queueName);
        }

    }
}