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
                
        //int count = 0;
   
        System.Timers.Timer OpacityTimer;
        Thread t_preload;
        //Thread addhmi;

        //ManualResetEvent mre = new ManualResetEvent(false);

        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)
            {
                //如果当前的透明度>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();

            t_preload = new Thread(PreLoadingProc);
            t_preload.IsBackground = true;
            t_preload.Start();
        }

        private void PreLoadingProc()
        {
            //diagToolDevProxy.Instance.OpenDevice();
            //AppCfg.Instance.Init();
            //Frm_manager.Instance.Init();
        }

        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();
            }
        }

       
        private void StartForm_Load(object sender, EventArgs e)
        {
            //if (mre != null)
            //    mre.Reset();
            //addhmi = new Thread(searchhmi);
            //addhmi.IsBackground = true;
            //addhmi.Start();
        }

        //string ip;
        //private void searchhmi()
        //{
        //    ProThread.HMISearch mysearch = new ProThread.HMISearch();
        //    int hmicount = 0;

        //    while (!mre.WaitOne(100))
        //    {
        //        mysearch.ThStart();
        //        Thread.Sleep(3000);
        //        hmicount = mysearch.HMICount();

        //        if (hmicount == 1)
        //        {
        //            mre.Set();
        //            break;
        //        }

        //        mysearch.ThStop();
        //        count++;
        //        if (count >= 10)
        //        {
        //            mre.Set();
        //            break;
        //        }
        //    }

        //    if (count < 10)
        //    {
        //        ip = (((globaldata.DeviceInfo)(mysearch.myHMIList[0])).IpAddr);
        //        globaldata.connection_ip = ip;
        //        mysearch.ThStop();

        //    }
        //    count = 0;
        //}


    }
}
