using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using SystemX.Net.Platform.Common.Util;
using static SystemX.Product.ALIS.UI.View.ViewCfg;

namespace SystemX.Product.ALIS.UI.View
{
    public static class ViewCfg
    {
        public enum eOperationMode
        {
            ALL,
            AUTO,
            MANUAL
        }
        public enum eWorkMode
        {
            NORMAL,
            RETEST,
            REWORK
        }
        public enum eAppFunctionType
        {
            None = 0,

            M_HostList,
            M_UserList,
            M_GroupList,
            M_TestCodeList,
            M_TestList,
            M_TestListRel,

            A_AnalysisPartID,
            A_LogSearchTrend,
            A_AnalysisProcessTrend
        }
        public enum eSelectType
        {
            None = 0,
            HostList,
            UserList,
            GroupList,
            TestCodeList,

            TestList,
            TestListRel
        }
        public enum eEditType
        {
            Insert = 0,
            Modify
        }

        public enum eModelType
        {
            NONE,
            IMAGE,
            TEXT,
            CPLOG,
            PARTID_MAP
        }
        
        public enum eProcessTable
        {
            HIST_ProdLoad = 0,
            HIST_CapDeassy,
            HIST_PreHeating,
            HIST_PreMeas,
            HIST_Leak,
            HIST_LaserTrim,
            HIST_LaserTrimVision,
            HIST_IsoRes,
            HIST_CapAssy,
            HIST_Function,
            HIST_OutSealPress,
            HIST_PinLVDT,
            HIST_PinVision,
            HIST_ProdUnload
        }

        private enum eProcessTableNumber
        {
            HIST_ProdLoad = 180,
            HIST_CapDeassy = 180,
            HIST_PreHeating = 190,
            HIST_PreMeas = 190,
            HIST_Leak = 200,
            HIST_LaserTrim = 210,
            HIST_LaserTrimVision = 220,
            HIST_IsoRes = 220,
            HIST_CapAssy = 230,
            HIST_Function = 240,
            HIST_OutSealPress = 250,
            HIST_PinLVDT = 260,
            HIST_PinVision = 260,
            HIST_ProdUnload = 270
        }

        public static string GetTableProcessNumber(eProcessTable processTable)
        {
            string[] strSetProcessNumberList = new string[] 
            { "180", "180", "190", "190", "200", "210", "220", "220", "230", "240", "250", "260", "260", "270" };

            if (strSetProcessNumberList.Length > (int)processTable)
                return strSetProcessNumberList[(int)processTable];
            else
                return "";
        }
        public enum eDataTable
        {
            HIST_TestResultData,
            HIST_TestResultDatail
        }

        public const string SystemConfigPath = @"./Config/SystemConfig.xml";
        public const string SaveLayoutPath = @"./Layout/Search/";

        public enum eConfigElemList
        {
            Root,
            RegisteredModels,
            Model
        }

        public static XElement OpenSystemConfig()
        {
            var xElement = XMLControl.OpenXMLDocument(SystemConfigPath, eConfigElemList.Root.ToString());

            if (xElement == null) return null;

            return xElement;
        }

        public static XElement SaveSystemConfig(List<SysModelDef> registeredModels)
        {
            XDocument xmlDoc = new XDocument();
            XElement xRoot = new XElement(eConfigElemList.Root.ToString());
            XElement xRegModels = new XElement(eConfigElemList.RegisteredModels.ToString());

            foreach (SysModelDef modeldef in registeredModels)
                xRegModels.Add(modeldef.SaveXML());

            xRoot.Add(xRegModels);
            xmlDoc.Add(xRoot);
            xmlDoc.Save(SystemConfigPath);

            return xRoot;
        }

        public static List<SysModelDef> GetRegisteredModels(XElement xelemRoot)
        {
            XElement elemModelRoot = xelemRoot.Element(eConfigElemList.RegisteredModels.ToString());

            List<SysModelDef> modeldef = new List<SysModelDef>();

            foreach (XElement elemModel in elemModelRoot.Elements())
                modeldef.Add(new SysModelDef(elemModel));

            return modeldef;
        }
        public static Dictionary<string, string> GetProcessTableNameMap()
        {
            Dictionary<string, string> NameMap = new Dictionary<string, string>();

            NameMap.Add(eProcessTable.HIST_ProdLoad.ToString(), "LX/LU-180:Loading");
            NameMap.Add(eProcessTable.HIST_CapDeassy.ToString(), "LX/LU-180:Cap De-Ass'y");
            NameMap.Add(eProcessTable.HIST_PreHeating.ToString(), "LX/LU-190:Pre-Heating");
            NameMap.Add(eProcessTable.HIST_PreMeas.ToString(), "LX/LU-190:Pre-Measuring");
            NameMap.Add(eProcessTable.HIST_Leak.ToString(), "LX/LU-200:Leak Test");
            NameMap.Add(eProcessTable.HIST_LaserTrim.ToString(), "LX/LU-210:Laser Trimming");
            NameMap.Add(eProcessTable.HIST_LaserTrimVision.ToString(), "LX/LU-220:Trimming Vision Inspection");
            NameMap.Add(eProcessTable.HIST_IsoRes.ToString(), "LX/LU-220:Isolation Resistance Test");
            NameMap.Add(eProcessTable.HIST_CapAssy.ToString(), "LX/LU-230:Cap Ass'y");
            NameMap.Add(eProcessTable.HIST_Function.ToString(), "LX/LU-240:Function Test");
            NameMap.Add(eProcessTable.HIST_OutSealPress.ToString(), "LX/LU-250:Outer Seal Press");
            NameMap.Add(eProcessTable.HIST_PinLVDT.ToString(), "LX/LU-260:Pin Height Inspection");
            NameMap.Add(eProcessTable.HIST_PinVision.ToString(), "LX/LU-260:Pin-Align Vision Inspection");
            NameMap.Add(eProcessTable.HIST_ProdUnload.ToString(), "LX/LU-270:Unloading");

            return NameMap;
        }

        public class NameMapItem
        {
            public string TableName { get; set; }
            public string ProcName { get; set; }
        }
    }

    public class SysModelDef
    {
        public string ID { get; set; }
        public string Path { get; set; }
        public string Type { get; set; }
        public eModelType eType { get; set; } = eModelType.NONE;

        public SysModelDef(XElement elem)
        {
            SetModelDef(elem);
            SetType(Type);
        }

        public SysModelDef(string strPath)
        {
            Path = strPath;
        }

        public bool SetType(string strType)
        {
            eModelType eparsetype = eModelType.NONE;

            if (!Enum.TryParse(strType.ToUpper(), out eparsetype))
                return false;

            eType = eparsetype;

            return true;
        }

        public bool SetModelDef(XElement elem)
        {
            try
            {
                foreach (XAttribute regAttrb in elem.Attributes())
                {
                    string strAttrbName = regAttrb.Name.LocalName;

                    if (CommonUtil.GetProperty(this, strAttrbName) == null)
                        continue;

                    CommonUtil.SetPropertyValue(this, strAttrbName, regAttrb.Value);
                }
            }
            catch (Exception ex)
            {
                LogMessage.MessageOutput.ConsoleWrite("Error during Read Mdoel Definition in System Config.", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
                LogMessage.MessageOutput.ConsoleWrite($" - Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
                throw;
            }

            return true;
        }

        public XElement SaveXML()
        {
            XElement xelemSave = new XElement(eConfigElemList.Model.ToString());

            xelemSave.SetAttributeValue("ID", ID);
            xelemSave.SetAttributeValue("Path", Path);
            xelemSave.SetAttributeValue("Type", Type);

            return xelemSave;
        }
    }

    public class LogInfo
    {
        public string LogAccKey { get; set; }
        public string ProductID { get; set; }
        public string PalletID { get; set; }
        public string UpdateDT { get; set; }
        public string ProcessNo { get; set; }
        public string PalletNo { get; set; }
        public string PalletIndex { get; set; }
        public string Status { get; set; }
        public string TestTime { get; set; }
        public string Result { get; set; }

		public Dictionary<int, string> TestResult { get; set; }
    }

    public class ChartLogInfo
    {
        public string TableName { get; set; }
        public string StartDateTime { get; set; }
        public string EndDateTime { get; set; }
        public string ProductNo { get; set; }
        public string PalletNo { get; set; }
        public string PalletIndex { get; set; }
        public string WorkMode { get; set; }
        public string OperationMode { get; set; }

        public string StepNo { get; set; }
        public string MO { get; set; }
        public string FuncName { get; set; }
        public string Min { get; set; }
        public string Max { get; set; }

        public Dictionary<string, double> MeasuredData = new Dictionary<string, double>();
    }
	
}
