using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using AssistClient.Utils;
using Newtonsoft.Json.Linq; 

namespace AssistClient
{
    public class Config
    {
        public static Models.User loginUser;
        public static Dictionary<string, string> dicDevType = new Dictionary<string, string>();
        //
        public static string appBasePath = ""; 

        //跳过检测设备
        public static bool SkipAxis0, SkipAxis1, SkipAxis2,SkipAxis3;
        public static bool SkipLight, SkipScannerGL, SkipScannerCC, SkipSmallAxis;

        //DEV
        public static int[] Axis_PulseOutMode=new int[4];
        public static int[] Axis_MM2PulseNum = new int[4] { 1000, 1000, 1000, 1000};//1mm对应脉冲数
        public static int[] Axis_HomeMode = new int[4];
        public static int[] Axis_HomeDir = new int[4];//回Home方向
        //回HOME速度
        public static JArray Axis_HomeVelLow = new JArray();
        public static JArray Axis_HomeVelHigh = new JArray();
        public static JArray Axis_HomeAcc = new JArray();
        public static JArray Axis_HomeDec = new JArray();
        //回JOG速度
        public static JArray Axis_JogVelLow = new JArray();
        public static JArray Axis_JogVelHigh = new JArray();
        public static JArray Axis_JogAcc = new JArray();
        public static JArray Axis_JogDec = new JArray();

        public static string IOCard_DeviceNum = "PCI-1730,BID#0";
        public static int Light_PortNum = 1;//COM端口号
         
        public static string SmallAxis_ComName = "COM2";//COM端口号 

        public static string Scanner_GENTL_CTI = "ScannerRuntime\\Win64_x64\\MvFGProducerCXP.cti";
        public static string SizeEnginePath;
        public static string SizeBmp_Path, Defect_SavePath;
        public static string SizeRepairTablePath;
        //尺寸配置
        //public static string Size_LoadPath, Size_FileName;

        //
        public static Dictionary<CMDName, JObject> CMDProcess;
        //DB
        //有些服务器防火墙有问题需要加上 min pool size=1 避免认为是恶意请求
        //如果用到bulkCopy需要加： AllowLoadLocalInfile=true
        //public static string dbMysqlCon = "server=localhost;Database=ProductionDB;Uid=root;Pwd=123456;";
        public static string DBConStr = "server=localhost;Database=ProductionDB;Uid=root;Pwd=123456; AllowLoadLocalInfile=true";
        public static string ServerIP = "";
        public static int ServerPort = 18082;

        //LOG
        public static string LogPath;

        //WEBAPI
        public static string baseUrl=""; 

        //==========位置  初始位 上料位 下料位(下料位不存在时使用上料位)
        public static JObject joPTSetting = new JObject() 
        {
            { "initPT", null },
            { "upPT", null},
            { "downPT", null}
        }; 

        public static void LoadAllConfig()
        {
            InitDevDic();
            LoadSysConfig();
            LoadPTConfig();
            DeleteFiles(Config.SizeBmp_Path, -2);
            //
            string lsCmdPath = appBasePath + "\\CMDProcess\\";
            if(!Directory.Exists(lsCmdPath))
                Directory.CreateDirectory(lsCmdPath);

            string fileName;
            CMDProcess = new Dictionary<CMDName, JObject>();
            foreach (int item in Enum.GetValues(typeof(CMDName)))//item为key值
            {
                //string name = Enum.GetName(typeof(CMDName), item);//获取名称
                fileName = lsCmdPath + item + ".json";
                if (File.Exists(fileName))
                    CMDProcess.Add((CMDName)item, JObject.Parse(File.ReadAllText(fileName)));
                //else
                //    CMDProcess.Add((CMDName)item, null);
            }
        }
        public static void LoadSysConfig()
        {
            if (string.IsNullOrWhiteSpace(appBasePath))
                appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
            string configPath = appBasePath + "\\SysConfig.ini";
            string lsTmp = "";

            DBConStr = Util.ReadIniValue(configPath, "Server", "DBConStr");
            ServerIP = Util.ReadIniValue(configPath, "Server", "ServerIP");
            lsTmp = Util.ReadIniValue(configPath, "Server", "ServerPort"); 
            if (Util.IsNumber(lsTmp)) ServerPort = Convert.ToInt32(lsTmp);

            //Skip
            SkipAxis0 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis0") == "1";
            SkipAxis1 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis1") == "1";
            SkipAxis2 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis2") == "1";
            SkipAxis3 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis3") == "1"; 
            SkipLight = Util.ReadIniValue(configPath, "SKIP", "SkipLight") == "1";
            SkipScannerGL = Util.ReadIniValue(configPath, "SKIP", "SkipScannerGL") == "1";
            SkipScannerCC = Util.ReadIniValue(configPath, "SKIP", "SkipScannerCC") == "1";
            SkipSmallAxis = Util.ReadIniValue(configPath, "SKIP", "SkipSmallAxis") == "1";

            //DEV
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_PulseOutMode");
            if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[0] = Convert.ToInt32(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_PulseOutMode");
            if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[1] = Convert.ToInt32(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_PulseOutMode");
            if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[2] = Convert.ToInt32(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_PulseOutMode");
            if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[3] = Convert.ToInt32(lsTmp);

            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_HomeMode");
            if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[0] = Convert.ToInt32(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_HomeMode");
            if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[1] = Convert.ToInt32(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_HomeMode");
            if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[2] = Convert.ToInt32(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_HomeMode");
            if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[3] = Convert.ToInt32(lsTmp);

            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_HomeDir");
            if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[0] = Convert.ToInt32(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_HomeDir");
            if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[1] = Convert.ToInt32(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_HomeDir");
            if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[2] = Convert.ToInt32(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_HomeDir");
            if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[3] = Convert.ToInt32(lsTmp);

            //HOME速度
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelLow");
            if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeVelLow = JArray.Parse(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelHigh");
            if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeVelHigh = JArray.Parse(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeAcc");
            if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeAcc = JArray.Parse(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeDec");
            if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeDec = JArray.Parse(lsTmp);
            //JOG速度
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_JogVelLow");
            if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_JogVelLow = JArray.Parse(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_JogVelHigh");
            if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_JogVelHigh = JArray.Parse(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_JogAcc");
            if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_JogAcc = JArray.Parse(lsTmp);
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_JogDec");
            if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_JogDec = JArray.Parse(lsTmp);
            //
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Light_PortNum");
            if (Util.IsNumber(lsTmp)) Config.Light_PortNum = Convert.ToInt32(lsTmp); 
              
            IOCard_DeviceNum = Util.ReadIniValue(configPath, "DEV", "IOCard_DeviceNum");
             
            SizeEnginePath = Util.ReadIniValue(configPath, "DEV", "SizeEnginePath");
            SizeBmp_Path = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Path");
            SizeRepairTablePath = Util.ReadIniValue(configPath, "DEV", "SizeRepairTablePath");
            //LOG
            LogPath = Util.ReadIniValue(configPath, "LOG", "LogPath"); 
        }
        public static void LoadPTConfig()
        { 
            if (string.IsNullOrWhiteSpace(appBasePath))
                appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
            string configPath = appBasePath + "\\PTConfig.json";
            string lsTmp = File.ReadAllText(configPath);
            joPTSetting = JObject.Parse(lsTmp); 
        }

        private static void InitDevDic()
        {
            //dicDevType.Add("CodeScanner", "扫码枪");//固定为必有开始
            if (!dicDevType.ContainsKey("IOCard")) dicDevType.Add("IOCard", "I/O控制"); 
            if (!dicDevType.ContainsKey("Axis")) dicDevType.Add("Axis", "滑台电机");
            if (!dicDevType.ContainsKey("Light")) dicDevType.Add("Light", "光源");
            if (!dicDevType.ContainsKey("Scanner_CC")) dicDevType.Add("Scanner_CC", "网口相机");   
            if (!dicDevType.ContainsKey("Size")) dicDevType.Add("Size", "尺寸测量");
            if (!dicDevType.ContainsKey("For")) dicDevType.Add("For", "For循环");
            if (!dicDevType.ContainsKey("If")) dicDevType.Add("If", "If条件");
        }

        private static void DeleteFiles(string dirPath, int days = -7)
        {
            if (!Directory.Exists(dirPath)) return;

            string[] file_list = Directory.GetFiles(dirPath);
            var delFiles = file_list.Select(file => new FileInfo(file))
            .Where(f => f.CreationTime <= DateTime.Now.AddDays(days))
            .Select(x => x.FullName)
            .ToList();
            foreach (string file in delFiles)
                File.Delete(file);
        }
        public static string getDefectName(string defectCode)
        {
            defectCode= defectCode.ToLower();
            switch (defectCode)
            {
                case "dk":
                    return "堵孔"; 
                case "zw":
                    return "脏污";
                case "xws":
                    return "纤维丝";
                case "gsyc":
                    return "钢丝异常"; 
                case "qk":
                    return "缺口"; 
                case "zk":
                    return "针孔"; 
                case "pp":
                    return "泡泡";
                case "hs":
                    return "划伤";
                case "yx":
                    return "压线";
                case "xb":
                    return "斜边";
                case "sx":
                    return "栅线";
                case "ds":
                    return "断栅";
                case "wtg":
                    return "未通过";
                case "qs":
                    return "缺失";
                case "dc":
                    return "多出";
                default:
                    return "未知"; 
        
            }
        }
    }
}
