using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Aitex.Common.Util;
using Aitex.Core.RT.Log;
using Aitex.Core.Util;
using VenusCommon;

namespace VenusCommon
{
    public class LeakCheckResultManager : Singleton<LeakCheckResultManager>
    {
        private string _template = "<?xml version=\"1.0\" encoding=\"utf-16\"?> <LeakCheckResult xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"></LeakCheckResult>";

        private Dictionary<string, XmlDocument> _dicXmlFile = new Dictionary<string, XmlDocument>();

        string _filePath = PathManager.GetAppDir() + "\\LeakCheckResult\\";

        string _fileName = "\\LeakCheckResult.xml";

        public void Initialize(string Module)
        {
            try
            {
                XmlDocument _dom = new XmlDocument();

                if (!Directory.Exists(_filePath + Module))
                {
                    Directory.CreateDirectory(_filePath + Module);
                }
                if (!File.Exists(_filePath + Module + _fileName))
                {
                    _dom.LoadXml(_template);
                    _dom.Save(_filePath + Module + _fileName);
                }
                else
                {
                    _dom.Load(_filePath + Module + _fileName);
                }
                _dicXmlFile.Add(Module, _dom);
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);
                throw;
            }
        }

        /*
         * 
         * 
         *                             Date = Convert.ToDateTime(element.GetAttribute("Date")),
                            LeakCheckStatus = element.GetAttribute("LeakCheckStatus"),
                            LeakRate = Convert.ToDouble(element.GetAttribute("LeakCheckStatus")),
                            StartPressure = Convert.ToInt32(element.GetAttribute("StartPressure")),
                            StopPressure = Convert.ToInt32(element.GetAttribute("StopPressure")),
                            LeakCheckMode = element.GetAttribute("LeakCheckMode"),
                            LeakCheckTime = Convert.ToInt32(element.GetAttribute("LeakCheckTime")),
         * 
         */
        public void AddLeakCheck(string Module, DateTime date, int leakCheckTime, int beginPressure, int endPressure, double leakRate, string status, string mode)
        {
            try
            {
                XmlElement node = _dicXmlFile[Module].CreateNode(XmlNodeType.Element, "LeakCheck", _dicXmlFile[Module].NamespaceURI) as XmlElement;
                node.SetAttribute("Id", Guid.NewGuid().ToString());
                node.SetAttribute("Date", date.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                node.SetAttribute("LeakCheckStatus", status);
                node.SetAttribute("LeakRate", leakRate.ToString("F5"));
                node.SetAttribute("StartPressure", beginPressure.ToString());
                node.SetAttribute("StopPressure", endPressure.ToString());
                node.SetAttribute("LeakCheckMode", mode);
                node.SetAttribute("LeakCheckTime", leakCheckTime.ToString());

                XmlElement root = _dicXmlFile[Module].SelectSingleNode("/LeakCheckResult") as XmlElement;
                root.AppendChild(node);

                _dicXmlFile[Module].Save(_filePath + Module + _fileName);
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);

            }

        }

        /*
         * 
         <LeakCheck Date="" StartPressure="" StopPressure 
         */
        public List<LeakCheckResultItem> GetHistoryLeakCheck(string Module)
        {
            List<LeakCheckResultItem> result = new List<LeakCheckResultItem>();

            try
            {
                XmlNodeList lst = _dicXmlFile[Module].SelectNodes("/LeakCheckResult/LeakCheck");
                if (lst != null)
                {
                    foreach (XmlElement element in lst)
                    {
                        result.Add(new LeakCheckResultItem()
                        {
                            Id = element.GetAttribute("Id"),
                            Date = Convert.ToDateTime(element.GetAttribute("Date")),
                            LeakCheckStatus = element.GetAttribute("LeakCheckStatus"),
                            LeakRate = Convert.ToDouble(element.GetAttribute("LeakRate")),
                            StartPressure = Convert.ToInt32(element.GetAttribute("StartPressure")),
                            StopPressure = Convert.ToInt32(element.GetAttribute("StopPressure")),
                            LeakCheckMode = element.GetAttribute("LeakCheckMode"),
                            LeakCheckTime = Convert.ToInt32(element.GetAttribute("LeakCheckTime")),
                        });
                    }
                }


            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);

            }

            return result;
        }

        public void Delete(string Module, string id)
        {
            try
            {
                XmlElement node = _dicXmlFile[Module].SelectSingleNode(string.Format("/LeakCheckResult/LeakCheck[@Id='{0}']", id)) as XmlElement;

                node.ParentNode.RemoveChild(node);

                _dicXmlFile[Module].Save(_filePath + Module + _fileName);
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);

            }
        }
    }
}