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

namespace GcDevicePc.GCBuffer
{
    class PCBuffer
    {
        public struct FolderInfo
        {
            public string DataFolder;        //数据保存目录
            public string ConfigFolder;         //程序配置目录
            public string RunFolder;         //当前程序运行目录
            public string MethodFolder;      //当前方法目录
            public string StatusFolder;      //当前配置目录
            public string SubStatusFolder;   //当前配置子目录
            public string RunTabFolder;      //当前运行表目录
            public string FwFolder;      //固件更新目录
            public string StartUpFolder;      //启动配置目录
            public string TmpFolder;      //TMPFile目录
            public string LogFolder;      //Log目录
        }

        public struct GCFileInfo
        {
            public string cur_MethodName;     //当前使用方法名
            public string cur_BATMethodName;     //当前批处理方法名
            public string new_MethodName;     //新建使用方法名
            public string cur_OpenMethodName; //当前打开方法名
            public string cur_StatusName;     //当前设备配置名
            public string cur_RunTab;         //当前运行表名
            public string config_RunTab;         //配置中运行表名
            public string cur_DataName;       //保存数据文件名
            public string show_MethodName;    //显示方法名
        }

        public struct WorkInfo
        {
            public bool system_Statue;  //系统开启状态
            public ushort work_Statue;  //系统工作状态
            public ushort RunType;  //运行类型  0 打开运行  1 智能运行 2 批处理
            public bool sendtohw;
            public bool sendtoZb;
            public bool sendtoSP;
            public bool info_log;
            public bool error_log;
            public string hmimac;
            public bool hmi_log;

        }

        public struct ModuleInfo
        {
            public string ip;
            public string mask;
            public string gw;
        }


        public struct OutPutInfo
        {
            public byte port;
        }

        public struct ThreadInfo
        {
            public ushort HMI_Monitor_TH;   // 0 --线程结束  1--线程运行中
        }

        public struct PCInfo
        {
            public FolderInfo pcfolderinfo;
            public GCFileInfo pcfileinfo;
            public WorkInfo pcworkinfo;
            public ModuleInfo moduleinfo;
            public ThreadInfo pcthreadinfo;

            public OutPutInfo outputinfo;
            public Adjust adjust;
        }

        public PCInfo gcpcinfo = new PCInfo();

        public struct Adjust
        {
            public int AdjustType;
            public int AdjustOS;

            public int AdjustTime;
            
        }

        public PCBuffer()
        {
            gcpcinfo.pcfolderinfo.DataFolder = null;
            gcpcinfo.pcfolderinfo.ConfigFolder = null;
            gcpcinfo.pcfolderinfo.MethodFolder = null;
            gcpcinfo.pcfolderinfo.StatusFolder = null;
            gcpcinfo.pcfolderinfo.SubStatusFolder = null;
            gcpcinfo.pcfolderinfo.RunTabFolder = null;
            gcpcinfo.pcfolderinfo.FwFolder = null;
            gcpcinfo.pcfolderinfo.StartUpFolder = null;
            gcpcinfo.pcfolderinfo.TmpFolder = null;
            gcpcinfo.pcfolderinfo.LogFolder = null;

            gcpcinfo.pcfileinfo.cur_MethodName = null;
            gcpcinfo.pcfileinfo.cur_BATMethodName = null;
            gcpcinfo.pcfileinfo.new_MethodName = null;
            gcpcinfo.pcfileinfo.cur_OpenMethodName = null;
            gcpcinfo.pcfileinfo.cur_StatusName = null;
            gcpcinfo.pcfileinfo.cur_RunTab = null;
            gcpcinfo.pcfileinfo.config_RunTab = null;
            gcpcinfo.pcfileinfo.cur_DataName = null;
            gcpcinfo.pcfileinfo.show_MethodName = null;

            gcpcinfo.pcworkinfo.system_Statue = false;
            gcpcinfo.pcworkinfo.work_Statue = 0;

            gcpcinfo.pcworkinfo.info_log = false;
            gcpcinfo.pcworkinfo.error_log = false;
            gcpcinfo.pcworkinfo.hmi_log = false;
            gcpcinfo.pcworkinfo.sendtoZb = false;
            gcpcinfo.pcworkinfo.sendtohw = false;
            gcpcinfo.pcworkinfo.sendtoSP = false;
            gcpcinfo.moduleinfo.ip = "";

            gcpcinfo.outputinfo.port = 0;
            gcpcinfo.adjust.AdjustOS = 0;
            gcpcinfo.adjust.AdjustType = 0;
            gcpcinfo.adjust.AdjustTime = 0;

            try
            {
                //string file = System.Windows.Forms.Application.StartupPath+ "\\startup.ini";
                //string file = System.Windows.Forms.Application.StartupPath+ "\\..\\GC_Config\\GC_Set\\启动参数" + "\\startup.ini";
                string file = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.StartupPath), "GC_Config\\GC_Set\\启动参数\\startup.ini");

                if (File.Exists(file))
                {
                    IniFile ini = new IniFile(file);

                    int opensys = ini.ReadInteger("StartUp", "打开系统");
                    int runtype = ini.ReadInteger("StartUp", "运行类型");


                    gcpcinfo.pcfolderinfo.DataFolder = ini.ReadString("DataFolder", "历史数据");

                    int Version = ini.ReadInteger("Version", "VersionType");
                    string uname = ini.ReadString("User", "Name");
                    string upwd = ini.ReadString("User", "Password");

                    
                    int dsf = ini.ReadInteger("SendData", "Thirdparty");
                    int dw = ini.ReadInteger("SendData", "Foreign");
                    int sdzb = ini.ReadInteger("SaveData", "ZBSaveData");
                    int apname = ini.ReadInteger("AppName", "AName");
                    int fi = ini.ReadInteger("StartUp", "Forceintegral");
                    globaldata.UserName = uname;
                    globaldata.UserPwd = upwd;
                    globaldata.CurrentVersion = Version;
                    globaldata.Foreign = dw;
                    globaldata.ZbDataSave = sdzb == 0 ? false : true;
                    globaldata.AppName = apname == 0 ? "Vocs在线色谱软件" : "TetChrom色谱软件";
                    gcpcinfo.pcworkinfo.hmimac = ini.ReadString("NetWorkConfig", "MAC地址");

                    CKVocAnalyzer.GlobalCKV.vocAlgorithm.Forceintegral = fi == 1 ? true : false;
                    if (opensys == 1)
                    {
                        gcpcinfo.pcworkinfo.RunType = (ushort)runtype;
                    }
                    else
                    {
                        gcpcinfo.pcworkinfo.RunType = 0;
                    }

                    if (dsf == 0)
                    {
                        gcpcinfo.pcworkinfo.sendtohw = true;
                    }
                    else if (dsf == 1)
                    {
                        gcpcinfo.pcworkinfo.sendtoZb = true;
                    }
                    else if(dsf == 2)
                    {
                        gcpcinfo.pcworkinfo.sendtoSP = true;
                    }
                    else
                    {
                        gcpcinfo.pcworkinfo.sendtohw = false;
                        gcpcinfo.pcworkinfo.sendtoZb = false;
                        gcpcinfo.pcworkinfo.sendtoSP = false;
                    }


                    //string wtd624x_ip = ini.ReadString("WTD624X", "IP");
                    //string wtd624x_mask = ini.ReadString("WTD624X", "MASK");
                    //string wtd624x_gw = ini.ReadString("WTD624X", "GW");


                    string info_log = ini.ReadString("Logs", "InfoLog");
                    string err_log = ini.ReadString("Logs", "ErrLog");
                    string hmi_log = ini.ReadString("Logs", "HmiLog");


                    string gc485 = ini.ReadString("GC485", "COM");

                    //string senddata = test.INIGetStringValue("StartUp", "SendData", null);

                    //if (Convert.ToInt32(opensys) == 1)
                    //{
                    //    gcpcinfo.pcworkinfo.RunType = Convert.ToUInt16(runtype);
                    //}

                    //if (!String.IsNullOrEmpty(wtd624x_ip))
                    //{
                    //    gcpcinfo.moduleinfo.ip = wtd624x_ip;
                    //}

                    //if (!String.IsNullOrEmpty(wtd624x_mask))
                    //{
                    //    gcpcinfo.moduleinfo.mask = wtd624x_mask;
                    //}

                    //if (!String.IsNullOrEmpty(wtd624x_gw))
                    //{
                    //    gcpcinfo.moduleinfo.gw = wtd624x_gw;
                    //}

                    if (!String.IsNullOrEmpty(gc485))
                    {
                        gcpcinfo.outputinfo.port = Convert.ToByte(gc485);
                    }

                    if (!String.IsNullOrEmpty(info_log))
                    {
                        gcpcinfo.pcworkinfo.info_log = info_log.Equals("1") ? true : false;
                    }

                    if (!String.IsNullOrEmpty(err_log))
                    {
                        gcpcinfo.pcworkinfo.error_log = err_log.Equals("1") ? true : false;
                    }

                    if (!String.IsNullOrEmpty(hmi_log))
                    {
                        gcpcinfo.pcworkinfo.hmi_log = hmi_log.Equals("1") ? true : false;
                    }


                    //string file = System.Windows.Forms.Application.StartupPath + "\\startup.ini";
                    //if (File.Exists(file))

                    // gcpcinfo.pcworkinfo.RunType = 0;   //运行类型  0 打开运行  1 智能运行 2 批处理

                }
                else
                {
                    gcpcinfo.pcworkinfo.RunType = 0;
                    gcpcinfo.pcworkinfo.sendtohw = false;
                    gcpcinfo.pcworkinfo.sendtoZb = false;
                    gcpcinfo.pcworkinfo.sendtoSP = false;
                    gcpcinfo.pcfolderinfo.DataFolder = null;
                }
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }
        }

    }
}
