using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Threading;

namespace GcDevicePc
{
    public partial class StartForm : Form
    {
        private double m_CurrentOpacity = 0;
        private const double OpacityInterval = 0.02; //0.02
        private const double StayTime = 2000;   //5s
                

        System.Timers.Timer OpacityTimer;
        static Thread t_preload;
        MainForm m_mainform;

        private void OpacityTimerProcOpen(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (this.m_CurrentOpacity < 1)
            {
                ////如果当前的透明度小于1，则继续增加透明度
                this.m_CurrentOpacity += OpacityInterval;
                MethodInvoker mi = new MethodInvoker(UpdateFrmOpacity);
                this.BeginInvoke(mi);
            }
            else
            {
                //如果当前已经完全不透明，则停止timer
                this.OpacityTimer.Stop();
                System.Timers.Timer t = new System.Timers.Timer(StayTime); //设置时间间隔为5秒
                t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);
                t.AutoReset = false; //每到指定时间Elapsed事件是触发一次（false），还是一直触发（true）
                t.Start();
            }
        }

        private void OpacityTimerProcClose(object sender, EventArgs e)
        {
            if (this.m_CurrentOpacity > 0)
            {
                t_preload.Join();   //拦截透明度下降请求

                //如果当前的透明度>0，则降低透明度
                this.m_CurrentOpacity -= OpacityInterval;
                MethodInvoker mi = new MethodInvoker(UpdateFrmOpacity);
                this.BeginInvoke(mi);
            }
            else
            {
                //如果完全不透明，则关闭降低透明度timer,且关闭窗体
                this.OpacityTimer.Stop();
                this.OpacityTimer.Enabled = false;

                MethodInvoker mi = new MethodInvoker(MainFrmStartProc);
                this.BeginInvoke(mi);
            }
        }

        private void UpdateFrmOpacity()
        {
            this.Opacity = this.m_CurrentOpacity;
        }

        public StartForm()
        {
            InitializeComponent();
          
            this.Opacity = this.m_CurrentOpacity;
            OpacityTimer = new System.Timers.Timer();
            OpacityTimer.Interval = 30;
            OpacityTimer.Elapsed += new System.Timers.ElapsedEventHandler(OpacityTimerProcOpen);
            OpacityTimer.Start();
        }

        private void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e)
        {
            OpacityTimer = new System.Timers.Timer();
            OpacityTimer.Interval = 30;
            OpacityTimer.Elapsed += new System.Timers.ElapsedEventHandler(OpacityTimerProcClose);
            OpacityTimer.Start();
        }

        private void MainFrmStartProc()
        {
            if (t_preload != null)
            {
                t_preload.Join();
                m_mainform = new MainForm();  
                this.Hide();
                m_mainform.Show();
            }
        }
       
        /// <summary>
        /// 窗口载入函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartForm_Load(object sender, EventArgs e)
        {
            //载入时开启预载线程
            t_preload = new Thread(PreLoadingProc);
            t_preload.IsBackground = true;
            t_preload.Start();
        }

        /// <summary>
        /// 预载操作
        /// </summary>
        private void PreLoadingProc()
        {
            globaldata.m_appinit.InitAppFolder();
            globaldata.m_appinit.InitAppDefaultConfig();

            // globaldata.m_appinit.InitDB();
            //  globaldata.m_appinit.InitAllGC();
            //globaldata.m_appinit.InitAllGCConfig();
            //globaldata.m_appinit.InitGCThread();
            //globaldata.m_appinit.InitGCControl();
        }
    }
}
