using DDUtilityApp.SECS;
using JWH;
using System;
using System.Collections.Generic;
using System.Xml;

namespace DDUtilityApp.LOGPARSER
{

    public class GemSetting
    {

        public string ModelName { get; protected set; } = "COMMON";

        public Dictionary<string, CEID> CeidDictionary = new Dictionary<string, CEID>();

        public Dictionary<string, RPT> RptDictionary = new Dictionary<string, RPT>();

        public Dictionary<string, SECSDefine> EquipmentModelDictionary = new Dictionary<string, SECSDefine>();

        public GemSetting(string filename, string modelName = "COMMON")
        {
            this.Load(filename);
            this.SetModelName(modelName);
        }

        public void Load(string filename)
        {
            XmlDocument document = null;
            try
            {
                if (string.IsNullOrEmpty(filename)) return;

                document = new XmlDocument();
                document.Load(filename);
                foreach (XmlNode child in document.ChildNodes[0])
                {
                    if (string.Compare(child.Name, "MODEL", true) == 0)
                    {
                        string modelName = child.Attributes["NAME"].Value;
                        if (string.IsNullOrEmpty(modelName)) continue;

                        SECSDefine eqModel = new SECSDefine(child);
                        this.EquipmentModelDictionary.Add(modelName, eqModel);
                    }
                }
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
            }
            finally
            {
            }
        }

        public void SetModelName(string modelName)
        {
            try
            {
                this.ModelName = modelName;

                if (this.EquipmentModelDictionary.ContainsKey("COMMON"))
                {
                    SECSDefine model = this.EquipmentModelDictionary["COMMON"];
                    this.CeidDictionary = new Dictionary<string, CEID>(model.CeidDictionary);
                    this.RptDictionary = new Dictionary<string, RPT>(model.RptDictionary);
                }

                if (this.ModelName != "COMMON" && this.EquipmentModelDictionary.ContainsKey(this.ModelName))
                {
                    SECSDefine model = this.EquipmentModelDictionary[this.ModelName];

                    foreach (KeyValuePair<string, CEID> pair in model.CeidDictionary)
                        if (this.CeidDictionary.ContainsKey(pair.Key)) this.CeidDictionary[pair.Key] = pair.Value;
                        else this.CeidDictionary.Add(pair.Key, pair.Value);

                    foreach (KeyValuePair<string, RPT> pair in model.RptDictionary)
                        if (this.RptDictionary.ContainsKey(pair.Key)) this.RptDictionary[pair.Key] = pair.Value;
                        else this.RptDictionary.Add(pair.Key, pair.Value);
                }
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
            }
        }

        /// <summary>
        /// Change ID, Name, Format
        /// </summary>
        /// <param name="item"></param>
        public void Setting(SECSItem item)
        {
            try
            {
                if (item.Stream != "6" || item.Function != "11") return;
                if (item.ChildItems.Count != 3) return;

                item.ChildItems[0].Name = "DATAID";
                item.ChildItems[1].Name = "CEID";

                if (this.CeidDictionary.ContainsKey(item.ChildItems[1].Value) == false) return;
                CEID ceid = this.CeidDictionary[item.ChildItems[1].Value];
                if (ceid != null) item.ChildItems[1].Description = ceid.Name;

                foreach (SECSItem report in item.ChildItems[2])
                {
                    if (report.ChildItems.Count != 2) continue;

                    report.ChildItems[0].Name = "RPTID";

                    if (this.RptDictionary.ContainsKey(report.ChildItems[0].Value) == false) continue;
                    RPT rpt = this.RptDictionary[report.ChildItems[0].Value];

                    if (rpt == null) continue;
                    int i = 0;
                    foreach (VID vid in rpt.VidCollection)
                    {
                        SECSItem secsVid = report.ChildItems[1].ChildItems[i];
                        secsVid.ID = vid.ID;
                        if (string.IsNullOrEmpty(secsVid.Name))
                        {
                            secsVid.Name = vid.Name;
                            //secsVid.Format = vid.Format;
                            //secsVid.Length = vid.Length;
                        }
                        i++;
                    }
                }
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
            }
        }

    }

}
