using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using CRVM.Entity;

namespace CRVM
{
    public partial  class ConfigHelper
    {
        private static ConfigHelper _orig_instance = null;
        private static Dictionary<int, IList<EntityAlarmSpeed>> orig_config_base = null;
        private static XmlDocument orig_xmldoc = null;
        private float[] orig_senor = new float[SysParam.Instance.channelCount];
        private float[] orig_sp = new float[SysParam.Instance.channelCount];
        //private string orig_path;

        public static ConfigHelper GetOrigInstance(string path)
        {
            if (_orig_instance == null)
            {
                _orig_instance = new ConfigHelper(path);
                _orig_instance.Orig_Initial();
                _orig_instance.OrigLoadConfigData(path);
                _orig_instance.OrigInitialData();
            }
            return _orig_instance;
        }

        private void Orig_Initial()
        {
            orig_config_base = new Dictionary<int, IList<EntityAlarmSpeed>>();
            orig_xmldoc = new XmlDocument();
        }

        private void OrigLoadConfigData(string path)
        {
            if (File.Exists(path))
            {
                try
                {
                    orig_xmldoc.Load(path);
                    
                    //sourceType = xmldoc.DocumentElement.Attributes[0].InnerText;
                    XmlNodeList nodes = orig_xmldoc.SelectNodes("configuration/ALARM_PARAM");
                    int count = nodes.Count;
                    if (count > 0)
                    {
                        int iccount = nodes[0].ChildNodes.Count;
                        for (int i = 0; i < iccount; i++)
                        {
                            //SysParam.Instance.channel[i].alarmTimes_l = Convert.ToInt32(nodes[0].ChildNodes[i].Attributes[0].InnerText);
                            //SysParam.Instance.channel[i].alarmTimes_m = Convert.ToInt32(nodes[0].ChildNodes[i].Attributes[1].InnerText);
                            //SysParam.Instance.channel[i].alarmTimes_h = Convert.ToInt32(nodes[0].ChildNodes[i].Attributes[2].InnerText);
                            IList<EntityAlarmSpeed> itemlist = new List<EntityAlarmSpeed>();
                            int inCount = nodes[0].ChildNodes[i].ChildNodes.Count;
                            for (int j = 0; j < inCount; j++)
                            {
                                EntityAlarmSpeed itemdata = new EntityAlarmSpeed();
                                itemdata.alarmSpeed = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[0].InnerText);
                                itemdata.lowPassL = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[1].InnerText);
                                itemdata.lowPassU = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[2].InnerText);
                                itemdata.lowAlarmValue = Convert.ToSingle(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[3].InnerText);
                                itemdata.midPassL = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[4].InnerText);
                                itemdata.midPassU = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[5].InnerText);
                                itemdata.midAlarmValue = Convert.ToSingle(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[6].InnerText);
                                itemdata.highPassL = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[7].InnerText);
                                itemdata.highPassU = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[8].InnerText);
                                itemdata.highAlarmValue = Convert.ToSingle(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[9].InnerText);                                
                                itemlist.Add(itemdata);
                            }
                            if (!orig_config_base.ContainsKey(i))
                            {
                                orig_config_base.Add(i, itemlist);
                            }
                        }
                    }
                }
                catch (XmlException xmlexp)
                {
                    throw new Exception("打开的项目配置文件错误，请检查配置文件: " + xmlexp.Message);
                }
            }
        }
       
        public void OrigInitialData()
        {
            for (int i = 0; i < orig_config_base.Keys.Count; i++)
            {
                EntityAlarmSpeed[] eass = new EntityAlarmSpeed[orig_config_base[i].Count];
                int k = 0;
                foreach (EntityAlarmSpeed item in orig_config_base[i])
                {
                    EntityAlarmSpeed eas = item;
                    eass[k] = eas;
                    k++;
                }
                SysParam.Instance.channel[i].reAlarmConfig = eass;
            }
        }
        
    }
}
