// ----------------------------------------------------------------------------------
// 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.Windows.Forms;
    using Microsoft.ManagementConsole;

    public class PendingOperationsFormView : FormView
    {
        private Forms.PendingOperationsControl control = null;
        private AzureManagerSnapIn parentSnapIn = null;
        private ExecutionEngine executor;

        public PendingOperationsFormView() : this(ExecutionEngine.Instance)
        {
        }

        public PendingOperationsFormView(ExecutionEngine executor)
        {
            this.parentSnapIn = (AzureManagerSnapIn)this.SnapIn;
            
            this.executor = executor;
            this.executor.OperationStatusChanged += new EventHandler<OperationStatusChangedEventArgs>(OnOperationStatusChanged);
        }

        private void OnOperationStatusChanged(object sender, OperationStatusChangedEventArgs e)
        {
            ShowOperations(e.Operations);
        }

        private void ShowOperations(OperationsDictionary operations)
        {
            if (operations == null)
                return;

            this.control.DisplayedOperations.Clear();

            foreach (var operation in operations.Values)
            {
                AddPendingOperation(operation);
            }

            this.control.ShowPendingOperations();
        }

        protected override void OnInitialize(AsyncStatus status)
        {
            base.OnInitialize(status);

            control = (Forms.PendingOperationsControl)this.Control;
            control.Dock = DockStyle.Fill;
            ShowOperations(this.executor.PendingOperations);
        }

        private void AddPendingOperation(OperationInfo operation)
        {
            this.control.DisplayedOperations.Add(operation.Id, operation);
        }
    }
}