using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ThreadedSplashScreenSample
{

    public partial class SplashScreen : MicroFour.StrataFrame.UI.Windows.Forms.StandardForm //MedicalSN.UI.Windows.Forms.ApplicationBaseForm
    {

        #region [ Constructors ]

        /// <summary>
        /// Default Constructor
        /// </summary>
        public SplashScreen()
        {
            InitializeComponent();

            //-- Update the version
            //lblVersion.Text = BuildInformation.BuildVersion;
        } 

        #endregion

        #region [ Private Fields ]

        private static ManualResetEvent _CreationHandle;
        private static SplashScreen _Dialog;
        private static bool _IsVisible = false;
        private static object _Lock = new object();
        private static Thread _Thread;

        #endregion

        #region  Private Methods

        private static void FormHandleCreated(object sender, EventArgs e)
        {
            _Dialog.HandleCreated -= new System.EventHandler(FormHandleCreated);
            _CreationHandle.Set();
        }

        private static void SetMessageInvoke(string Message)
        {
            _Dialog.lblStatus.Text = Message;
        }

        private static void ShutdownInvoke()
        {
            //-- Close the message pump on the current thread
            System.Windows.Forms.Application.ExitThread();
            _IsVisible = false;
            //DoEventsHelper.DoEvents();
        }

        private static void StartupInvoke()
        {
            //-- Create the form and start a message pump
            _Dialog = new SplashScreen();
            _Dialog.HandleCreated += new System.EventHandler(FormHandleCreated);
            System.Windows.Forms.Application.Run(_Dialog);
        }

        #endregion

        #region  Public Methods

        /// <summary>
        /// Hides the splash screen.
        /// </summary>
        /// <remarks></remarks>
        public static void HideSplash()
        {
            if (!_IsVisible)
            {
                return;
            }
            lock (_Lock)
            {
                _Dialog.Invoke(new ShutdownDeleg(ShutdownInvoke));
            }
        }

        /// <summary>
        /// Sets the text on the splash screen.
        /// </summary>
        /// <remarks></remarks>
        public static void SetMessage(string Message)
        {
            if (!_IsVisible)
            {
                return;
            }
            lock (_Lock)
            {
                if (_Dialog != null && !_Dialog.IsDisposed)
                {
                    _Dialog.Invoke(new UpdateTextDeleg(SetMessageInvoke), Message);
                }
            }
        }

        /// <summary>
        /// Shows the splash screen.
        /// </summary>
        /// <remarks></remarks>
        public static void ShowSplash()
        {
            if (_IsVisible)
            {
                return;
            }
            _CreationHandle = new ManualResetEvent(false);
            _Thread = new Thread(new System.Threading.ThreadStart(StartupInvoke));
            _Thread.Start();
            _CreationHandle.WaitOne();
            _IsVisible = true;
        }

        #endregion

        #region  Nested Types

        private delegate void UpdateTextDeleg(string Message);
        private delegate void ShutdownDeleg();

        #endregion

    }

}