// ----------------------------------------------------------------------------------
// 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.Forms
{
    using System;
    using System.Windows.Forms;
    using Microsoft.Samples.WindowsAzureMmc.Model;
    using Microsoft.Samples.WindowsAzureMmc.Powershell.Host;

    public partial class ProgressDialog : Form
    {
        public ProgressDialog(AzurePSHost psHost)
        {
            this.PSHost = psHost;
            this.InitializeComponent();
            this.Message = "Loading...";
        }

        /// <summary>
        /// The action to perform when the dialog is shown
        /// </summary>
        public Action ActionToPerform { get; set; }

        /// <summary>
        /// The initial message to display
        /// </summary>
        public string Message
        {
            get
            {
                return this.lbl.Text;
            }
            set
            {
                this.lbl.Text = value;
                this.Refresh();
            }
        }

        /// <summary>
        /// The PSHost will notify us when something worth talking about has happened
        /// </summary>
        public AzurePSHost PSHost { get; set; }

        /// <summary>
        /// When the progress window displays, start the activity
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Progress_Shown(object sender, EventArgs e)
        {
            this.PSHost.OnProgress += new ProgressEventHandler(this.PSHost_OnProgress);
            Application.DoEvents();

            this.ActionToPerform();

            this.PSHost.OnProgress -= PSHost_OnProgress;

            this.Close();
        }

        /// <summary>
        /// Update the message from the PSHost status
        /// </summary>
        /// <param name="message"></param>
        private void PSHost_OnProgress(string message)
        {
            Application.DoEvents();
            if (!string.IsNullOrEmpty(message))
            {
                this.Message = message;
            }
        }
    }
}