using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GcDevicePc.Common;
using GcDevicePc.GCBuffer;
using Ini.Net;

namespace GcDevicePc
{
    class AppInit
    {
        
        /// <summary>
        /// 检测文件和目录是否存在
        /// </summary>
        /// <param name="flag">检测标志 0-表示检测目录</param>
        /// <param name="sPath"></param>
        /// <returns></returns>
        private bool CheckFolderandFile(int flag, string sPath)
        {
            switch (flag)
            {
                case 0:     //检测并创建目录
                    {
                        if (!Directory.Exists(sPath))
                        {
                            Directory.CreateDirectory(sPath);
                        }
                    }
                    break;
                case 1:
                    break;
            }

            return true;
        }

        private IniFile IniParser;

        //private void GetGCCode()
        //{
        //    int devicecount = 0;
        //    devicecount = globaldata.DeviceList.Count;
        //    int i = 0;
        //    int count = 0;
        //    string tmpfile = "";
        //    string tmpip = "";
        //    string tmpname = "";
        //    int ret = 0;

        //    if (devicecount > 0)
        //    {
        //        globaldata.DeviceName.Clear();
        //        FileServerClient mainclient = new FileServerClient();

        //        for (i = 0; i < devicecount; i++)
        //        {
        //            tmpip = (((globaldata.DeviceInfo)(globaldata.DeviceList[i])).IpAddr);
        //            mainclient.WtClientInit(tmpip, globaldata.fileserverport);

        //            //获取设备配置
        //            tmpfile = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.tmpFolder + "\\" + i.ToString() + "\\DevStatus.ini";
        //            if (System.IO.File.Exists(tmpfile))
        //            {
        //                Thread.Sleep(100);
        //                System.IO.File.Delete(tmpfile);
        //            }

        //            ret = mainclient.WtClientCopyfromServer("DevStatus.ini", tmpfile, globaldata.remoteFolder + "仪器状态\\");
        //            Thread.Sleep(400);

        //            {
        //                FileHelper tmpfh = new FileHelper(tmpfile);
        //                tmpname = tmpfh.GetProfileString("仪器编号", "仪器编码", "无编码设备");
        //                if (tmpname == "无编码设备")
        //                {
        //                    tmpname = tmpname + count.ToString();
        //                    count++;
        //                }
        //                globaldata.DeviceName.Add(tmpname);
        //            }
        //        }
        //    }
        //}

        //private void MoveDevFile()
        //{
        //    int devicecount = 0;
        //    devicecount = globaldata.DeviceList.Count;
        //    int i = 0;
        //    string tmpfile = "";
        //    string tmpfile2 = "";
        //    string folderToBeDeleted = "";

        //    if (devicecount > 0)
        //    {
        //        for (i = 0; i < devicecount; i++)
        //        {
        //            tmpfile = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.tmpFolder + "\\" + i.ToString() + "\\DevStatus.ini";
        //            tmpfile2 = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder + "\\" + globaldata.DeviceName[i].ToString() + "\\DevStatus.ini";
        //            if (System.IO.File.Exists(tmpfile2))
        //            {
        //                System.IO.File.Delete(tmpfile2);
        //            }

        //            if (System.IO.File.Exists(tmpfile))
        //            {
        //                System.IO.File.Move(tmpfile, tmpfile2);
        //                Thread.Sleep(100);
        //            }

        //            {
        //                folderToBeDeleted = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.tmpFolder + "\\" + i.ToString();
        //                DirectoryInfo folder = new DirectoryInfo(folderToBeDeleted);
        //                if (folder.Exists)
        //                {
        //                    folder.Delete(true);
        //                }
        //            }
        //        }
        //    }
        //}

        public void InitAppFolder()
        {
            string tmpFolder;
            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunFolder = System.Windows.Forms.Application.StartupPath;

            tmpFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunFolder;
            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder = System.IO.Path.Combine(tmpFolder, "仪器方法");
            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder);

            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder = System.IO.Path.Combine(tmpFolder, "仪器状态");
            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder);

            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder = System.IO.Path.Combine(tmpFolder, "运行表");
            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder);

            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder = System.IO.Path.Combine(tmpFolder, "历史数据");
            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder);

            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.FwFolder = System.IO.Path.Combine(tmpFolder, "固件更新");
            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.FwFolder);

            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.TmpFolder = System.IO.Path.Combine(tmpFolder, "tmpfile");
            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.TmpFolder);

            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.LogFolder = System.IO.Path.Combine(tmpFolder, "Logs");
            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.LogFolder);

            tmpFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder;
            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder = System.IO.Path.Combine(tmpFolder, "子状态");
            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder);

            tmpFolder = null;
        }

        public void InitAppDefaultConfig()
        {
            List<String> foldername = new List<string>
            {
                globaldata.remoteFolder + "仪器方法\\",
                globaldata.remoteFolder + "仪器状态\\",
                globaldata.remoteFolder + "运行表\\"
            };

            List<String> localfoldername = new List<string>
            {
                System.Windows.Forms.Application.StartupPath + "\\仪器方法\\",
                System.Windows.Forms.Application.StartupPath + "\\仪器状态\\",
                System.Windows.Forms.Application.StartupPath + "\\运行表\\"
            };

            List<String> filename = new List<string>
            {
                "SingleAnal.ini",
                "DevStatus.ini",
                "RunMethod.ini"
            };

           

            int checkflag = 0;
            int ret = 0;


            if (!String.IsNullOrEmpty(globaldata.connection_ip))
            {
                FileTools filecheck = new FileTools();
                FileServerClient mainclient = new FileServerClient();
                for (int i = 0; i < filename.Count; i++)
                {
                    String getfile = localfoldername[i] + filename[i];
                    checkflag = filecheck.IsUse(getfile);

                    if (checkflag == 0)
                    {
                        continue;
                    }
                    else if (checkflag == 1)
                    {
                        mainclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                        ret = mainclient.WtClientCopyfromServer(filename[i], getfile, foldername[i]);

                        checkflag = filecheck.IsUse(getfile);
                        while (checkflag != 0)
                        {
                            Thread.Sleep(200);
                            checkflag = filecheck.IsUse(getfile);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                mainclient.WtClientClose();
            }

            //单次
            String tmpfile = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder,"SingleAnal.ini");
            IniParser = new IniFile(tmpfile);
            if (IniParser.KeyExists("方法名称", "名称"))
                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = IniParser.ReadString("方法名称", "名称");
            else
                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = "SingleAnal.ini";

            // BAT
            tmpfile = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder, "RunMethod.ini");
            globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab = tmpfile;
            if (!File.Exists(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab))
            {
                RunTableHelper default_runtable =
                    new RunTableHelper(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder, "RunMethod.ini");
                default_runtable.SaveRunTable();
            }

            IniParser = new IniFile(tmpfile);
            if(IniParser.KeyExists("运行表", "1"))
                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_BATMethodName = IniParser.ReadString("运行表", "1");
            else
                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_BATMethodName = null;


            tmpfile = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder, "DevStatus.ini");
            if (File.Exists(tmpfile) == false)
            {
                HWConfig default_devstatus = new HWConfig(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder, "DevStatus.ini");
                default_devstatus.SavaHWConfig();
            }
            else
            {
                globaldata.m_hwconfig.setFileName(tmpfile);

                Console.WriteLine(globaldata.m_hwconfig.hwconfiginfo.GCNum);
            }

            globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_StatusName = tmpfile;

            tmpfile = null;
        }

        public void InitDB()
        {
            //SQLiteHelper mydb = new SQLiteHelper();
            //mydb.fillmachineinfo("GC1001", "a.ini", "DevStatus.ini", "192.168.1.87", 1, 2);
        }

        /// <summary>
        /// 获取所有GC的配置和方法
        /// </summary>
        //public void InitAllGC()
        //{
        //    int devicecount = 0;
        //    devicecount = globaldata.DeviceList.Count;
        //    int i = 0;
        //    int j = 0;
        //    string tmpfile = "";
        //    string tmpip = "";
        //    string tmpname = "";
        //    int ret = 0;

        //    if (devicecount > 0)
        //    {
        //        //globaldata.DeviceName.Clear();
        //        globaldata.GCMethodName.Clear();
        //        globaldata.GCBatMethodName.Clear();

        //        FileServerClient mainclient = new FileServerClient();
        //        try
        //        {
        //            for (i = 0; i < devicecount; i++)
        //            {
        //                tmpip = (((globaldata.DeviceInfo)(globaldata.DeviceList[i])).IpAddr);
        //                mainclient.WtClientInit(tmpip, globaldata.fileserverport);

        //                ////获取设备配置
        //                //tmpfile = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder + "\\" + globaldata.DeviceName[i].ToString() + "\\DevStatus.ini";
        //                //if (System.IO.File.Exists(tmpfile))
        //                //{
        //                //    Thread.Sleep(100);
        //                //    System.IO.File.Delete(tmpfile);
        //                //}
        //                //ret = mainclient.WtClientCopyfromServer("DevStatus.ini", tmpfile, globaldata.remoteFolder + "仪器状态\\");
        //                //Thread.Sleep(400);

        //                //{
        //                //    FileHelper tmpfh = new FileHelper(tmpfile);
        //                //    tmpname = tmpfh.GetProfileString("仪器编号", "仪器编码", "无编码设备");
        //                //    globaldata.DeviceName.Add(tmpname);
        //                //}

        //                //获取单次运行方法
        //                tmpfile = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder + "\\" + globaldata.DeviceName[i].ToString() + "\\SingleAnal.ini";
        //                if (System.IO.File.Exists(tmpfile))
        //                {
        //                    Thread.Sleep(100);
        //                    System.IO.File.Delete(tmpfile);
        //                }

        //                ret = mainclient.WtClientCopyfromServer("SingleAnal.ini", tmpfile, globaldata.remoteFolder + "仪器方法\\");
        //                Thread.Sleep(500);

        //                if (System.IO.File.Exists(tmpfile))
        //                {
        //                    FileHelper tmpfh = new FileHelper(tmpfile);
        //                    tmpname = tmpfh.GetProfileString("方法名称", "名称", "SingleAnal.ini");
        //                    if (tmpname != "SingleAnal.ini")
        //                    {
        //                        tmpfile = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder + "\\" + globaldata.DeviceName[i].ToString() + "\\" + tmpname;
        //                        ret = mainclient.WtClientCopyfromServer(tmpname, tmpfile, globaldata.remoteFolder + "仪器方法\\");
        //                        Thread.Sleep(500);
        //                    }
        //                    globaldata.GCMethodName.Add(tmpname);
        //                }

        //                //获取批处理及其方法
        //                tmpfile = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder + "\\" + globaldata.DeviceName[i].ToString() + "\\RunMethod.ini";
        //                if (System.IO.File.Exists(tmpfile))
        //                {
        //                    Thread.Sleep(100);
        //                    System.IO.File.Delete(tmpfile);
        //                }

        //                ret = mainclient.WtClientCopyfromServer("RunMethod.ini", tmpfile, globaldata.remoteFolder + "运行表\\");
        //                Thread.Sleep(500);

        //                if (System.IO.File.Exists(tmpfile))
        //                {
        //                    string[] runtabnames = new string[256];
        //                    FileHelper tmpfh = new FileHelper(tmpfile);
        //                    runtabnames = tmpfh.GetAllItemKeys("运行表");

        //                    Console.WriteLine("IP:" + tmpip);
        //                    for (j = 0; j < runtabnames.Length; j++)
        //                    {
        //                        if (j != 0)
        //                        {
        //                            tmpname = tmpfh.GetProfileString("运行表", runtabnames[j], "SingleAnal.ini");
        //                            tmpfile = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder + "\\" + globaldata.DeviceName[i].ToString() + "\\" + tmpname;
        //                            if (System.IO.File.Exists(tmpfile))
        //                            {
        //                                Console.WriteLine(tmpfile.ToString() + "  方法已经存在！");
        //                            }
        //                            else
        //                            {
        //                                mainclient.WtClientCopyfromServer(tmpname, tmpfile, globaldata.remoteFolder + "仪器方法\\");
        //                                Thread.Sleep(500);
        //                            }

        //                            if (j == 0)
        //                            {
        //                                globaldata.GCBatMethodName.Add(tmpname);
        //                            }
        //                        }
        //                    }
        //                }

        //            }
        //        }
        //        catch
        //        {
        //            Console.WriteLine(tmpip + " is error!");
        //        }
        //        mainclient.WtClientClose();
        //        Console.WriteLine("InitAllGC is OK!");
        //    }
        //}

        //public void InitGCHWConfig()
        //{
        //    int devicecount = 0;
        //    devicecount = globaldata.DeviceList.Count;
        //    int i = 0;
        //    string tmpfile = "";
        //    if (devicecount > 0)
        //    {
        //        for (i = 0; i < devicecount; i++)
        //        {
        //            tmpfile = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder + "\\DevStatus.ini";
        //            HWConfig m_hwconfig = new HWConfig(tmpfile);
        //            m_hwconfig.GetHWConfig();
        //        }
        //    }

        //}

        /// <summary>
        /// 初始化基础线程
        /// </summary>
        //public void InitGCThread()
        //{
        //    int devicecount = 0;
        //    devicecount = globaldata.DeviceList.Count;
        //    int i = 0;
        //    string tmpip = "";
        //    HWConfig m_hwconfig;


        //    HMIState mystate;
        //    ChannelData mychanneldata;
        //    GCDisplay mydisplay;

        //    Console.WriteLine("开始启动线程！");
        //    if (devicecount > 0)
        //    {
        //        globaldata.t_hmistate.Clear();  //清理状态采集线程
        //        globaldata.t_channeldata.Clear(); //清理通道采集线程
        //        globaldata.t_displaydata.Clear();

        //        for (i = 0; i < devicecount; i++)
        //        {
        //            tmpip = (((globaldata.DeviceInfo)(globaldata.DeviceList[i])).IpAddr);
        //            m_hwconfig = (HWConfig)globaldata.GCHWConfigs[i];

        //            mystate = new HMIState(tmpip, 502);
        //            mystate.SetHMIMAcState((((globaldata.DeviceInfo)(globaldata.DeviceList[i])).MacAddr));

        //            mystate.HmiStatueStart();
        //            mystate.SetHWConfig(m_hwconfig);
        //            globaldata.t_hmistate.Add(mystate);

        //            mychanneldata = new ChannelData(tmpip, 502);
        //            mychanneldata.ChannelDataStart();
        //            mychanneldata.SetHWConfig(m_hwconfig);
        //            globaldata.t_channeldata.Add(mychanneldata);

        //            mydisplay = new GCDisplay(tmpip, 502);
        //            mydisplay.DisplayDataStart();
        //            globaldata.t_displaydata.Add(mydisplay);


        //        }
        //    }
        //    Console.WriteLine("线程均启动！");
        //}

        //public void InitGCControl()
        //{
        //    int devicecount = 0;
        //    devicecount = globaldata.DeviceList.Count;
        //    int i = 0;
        //    string tmpip = "";
        //    HMIControl mycontrol;

        //    if (devicecount > 0)
        //    {
        //        globaldata.t_hmicontrol.Clear();
        //        for (i = 0; i < devicecount; i++)
        //        {
        //            tmpip = (((globaldata.DeviceInfo)(globaldata.DeviceList[i])).IpAddr);
        //            mycontrol = new HMIControl(tmpip, globaldata.datamodbusport);
        //            globaldata.t_hmicontrol.Add(mycontrol);
        //        }
        //    }
        //}
    }
}
