using System;
using System.Collections.Generic;
using System.Text;
using ChuPiao.AutoPrint.Configuration;
using ChuPiao.Common.Models;
using System.Xml;
using System.Reflection;
using ChuPiao.Common.Utils;
using System.Collections;

namespace ChuPiao.Office.Services
{
    /// <summary>
    /// 2011.9.17  默认配置文件 修改为 不缓存在服务器上
    /// </summary>
    public class M_MachineConfigTemplateService
    {
        public M_MachineConfigTemplateService()
        {
            _documents = new Hashtable();
            _section = TemplateConfigurationSettingSection.GetTemplateConfigurationSection();
        }

        private XmlDocument GetXmlDocument(string name)
        {
            string configPath = _section.Templates[name].Path;
            XmlDocument document = (XmlDocument)_documents[name];
            if (document == null)
            {
                document = new XmlDocument();
                document.Load(configPath);
                _documents.Add(name,document);
            }
            return document;
        }

        //public M_MachineConfigEntity GetNativeMachineConfig(string machineConfigName)
        //{
        //    switch (machineConfigName)
        //    {
        //        case "basic":
        //            return GetNativeBasicMachineConfig();
        //        default :
        //            return null;
        //    }
        //}

        //public List<M_MachineModeConfigEntity> GetNativeMachineModeConfigs(string machineConfigName)
        //{
        //    switch (machineConfigName)
        //    {
        //        case "basic":
        //            return GetNativeBasicMachineModeConfigs();
        //        default:
        //            return null;
        //    }
        //}

        //private M_MachineConfigEntity GetNativeBasicMachineConfig()
        //{
        //    return ParseToGetConfig<M_MachineConfigEntity>(_baseConfig, _machineConfigNodePath);
        //}

        //private List<M_MachineModeConfigEntity> GetNativeBasicMachineModeConfigs()
        //{
        //    return ParseToGetConfigs<M_MachineModeConfigEntity>(_baseConfig, _machineModeConfigNodePath);
        //}

        public M_MachineConfigEntity GetBasicMachineConfig(string machinetypename)
        {
            return GetConfig<M_MachineConfigEntity>(machinetypename, _machineConfigNodePath);
        }

        public List<M_MachineModeConfigEntity> GetBasicMachineModeConfigs(string machinetypename)
        {
            return GetConfigs<M_MachineModeConfigEntity>(machinetypename, _machineModeConfigNodePath);
        }

        //public void RemoveBasicMachineConfig()
        //{
        //    string key = _baseConfig + _split + _machineConfigHeadKey;
        //    MemCacheAgent.Delete(key);
        //}

        //public void RemoveBasicMachineModeConfigs()
        //{
        //    string key = _baseConfig + _split + _machineModeConfigHeadKey;
        //    MemCacheAgent.Delete(key);
        //}

        private T GetConfig<T>(string name, string nodePath)
        {
            T entity = default(T);
            //bool isMemcachedGood = true;
            //try
            //{
            //    entity = (T)MemCacheAgent.Get(key);
            //}
            //catch (Exception e)
            //{
            //    isMemcachedGood = false;
            //}
            //if (entity == null)
            //{
            //    entity = ParseToGetConfig<T>(name, nodePath);
            //    if (isMemcachedGood)
            //    {
            //        MemCacheAgent.Set(key, entity);
            //    }
            //}
            entity = ParseToGetConfig<T>(name, nodePath);
            return entity;
        }


        private List<T> GetConfigs<T>(string name, string nodePath)
        {
            List<T> entity = default(List<T>);
            //bool isMemcachedGood = true;
            //try
            //{
            //    entity = (List<T>)MemCacheAgent.Get(key);
            //}
            //catch (Exception e)
            //{
            //    isMemcachedGood = false;
            //}
            //if (entity == null)
            //{
            //    entity = ParseToGetConfigs<T>(name, nodePath);
            //    if (isMemcachedGood)
            //    {
            //        MemCacheAgent.Set(key, entity);
            //    }
            //}
            entity = ParseToGetConfigs<T>(name, nodePath);
            return entity;
        }

        private T ParseToGetConfig<T>(string name, string nodePath)
        {
            XmlDocument document = GetXmlDocument(name);
            XmlNode machineConfigNode = document.SelectSingleNode(nodePath);
            T t = default(T);
            t = (T)(typeof(T).GetConstructor(new Type[0]).Invoke(new Object[0]));
            Type type = t.GetType();
            XmlNodeList children = machineConfigNode.ChildNodes;
            foreach (XmlNode child in children)
            {
                PropertyInfo property = type.GetProperty(child.Name);
                Object convertedValue = Convert.ChangeType(child.InnerText, property.PropertyType);
                property.SetValue(t, convertedValue, null);
            }
            return t;
        }

        private List<T> ParseToGetConfigs<T>(string name, string nodePath)
        {
            XmlDocument document = GetXmlDocument(name);
            XmlNodeList machineConfigNodes = document.SelectNodes(nodePath);
            List<T> entity = new List<T>(machineConfigNodes.Count);
            Type type = typeof(T);
            foreach (XmlNode machineConfigNode in machineConfigNodes)
            {
                T t = (T)(typeof(T).GetConstructor(new Type[0]).Invoke(new Object[0]));
                XmlNodeList children = machineConfigNode.ChildNodes;
                foreach (XmlNode child in children)
                {
                    PropertyInfo property = type.GetProperty(child.Name);
                    Object convertedValue = Convert.ChangeType(child.InnerText, property.PropertyType);
                    property.SetValue(t, convertedValue, null);
                }
                entity.Add(t);
            }
            return entity;
        }

        //public bool AddBasicMachineModeConfig(M_MachineModeConfigEntity machineModeEntity)
        //{
        //   XmlDocument document = GetXmlDocument(_baseConfig);
        //   XmlNode machineModeConfigsNode = document.SelectSingleNode(_machineModeConfigsNodePath);
        //   XmlNode newChildNode = ParseMachineModeConfigEntityEntityToXmlNode(machineModeEntity);
        //   machineModeConfigsNode.AppendChild(newChildNode);
        //   document.Save(_section.Templates[_baseConfig].Path);
        //    return true;
        //}

        //private XmlNode ParseMachineModeConfigEntityEntityToXmlNode(M_MachineModeConfigEntity machineModeEntity)
        //{
        //    XmlDocument document = GetXmlDocument(_baseConfig);
        //    XmlNode rootNode = document.CreateElement("MachineModeConfig");
        //    Type type = machineModeEntity.GetType();
        //    PropertyInfo[] properties = type.GetProperties();
        //    foreach (PropertyInfo p in properties)
        //    {
        //        if(p.Name == "C_Id" || p.Name == "C_Mid")
        //        {
        //            continue;
        //        }
        //        XmlNode childNode = document.CreateElement(p.Name);
        //        childNode.InnerText = Convert.ToString(p.GetValue(machineModeEntity,null)); ;
        //        rootNode.AppendChild(childNode);
        //    }
        //    return rootNode;
        //}

        //public bool RemoveBasicMachineModeConfig(M_MachineModeConfigEntity machineModeEntity)
        //{
        //    XmlDocument document = GetXmlDocument(_baseConfig);
        //    XmlNode machineModeConfigsNode = document.SelectSingleNode(_machineModeConfigsNodePath);
        //    XmlNode targetNode = FindXmlNode(machineModeConfigsNode,machineModeEntity);
        //    machineModeConfigsNode.RemoveChild(targetNode);
        //    document.Save(_section.Templates[_baseConfig].Path);
        //    return true;
        //}

        //public bool SaveBasicMachineModeConfig(M_MachineModeConfigEntity machineModeEntity)
        //{
        //    XmlDocument document = GetXmlDocument(_baseConfig);
        //    XmlNode machineModeConfigsNode = document.SelectSingleNode(_machineModeConfigsNodePath);
        //    XmlNode targetNode = FindXmlNode(machineModeConfigsNode, machineModeEntity);
        //    Type type = machineModeEntity.GetType();
        //    PropertyInfo[] properties = type.GetProperties();
        //    foreach (PropertyInfo p in properties)
        //    {
        //        if (p.Name == "C_Id" || p.Name == "C_Mid")
        //        {
        //            continue;
        //        }
        //        XmlNode childNode = targetNode.SelectSingleNode(p.Name);
        //        childNode.InnerText = Convert.ToString(p.GetValue(machineModeEntity,null));
        //    }
        //    document.Save(_section.Templates[_baseConfig].Path);
        //    return true;
        //}

        //private XmlNode FindXmlNode(XmlNode parent, M_MachineModeConfigEntity machineModeEntity)
        //{
        //    XmlNodeList children = parent.ChildNodes;
        //    string nodeName = "C_MacModeId";
        //    XmlNode targetNode = null;
        //    foreach (XmlNode node in children)
        //    {
        //        XmlNode findedNode = node.SelectSingleNode(nodeName);
        //        string modeId = Convert.ToString(machineModeEntity.C_MacModeId);
        //        if (modeId == findedNode.InnerText)
        //        {
        //            targetNode = node;
        //            break;
        //        }
        //    }
        //    return targetNode;
        //}

        public bool SaveBasicMachineConfig(string machinetypename, M_MachineConfigEntity machineConfigEntity)
        {
            XmlDocument document = GetXmlDocument(machinetypename);
            XmlNode machineConfigsNode = document.SelectSingleNode(_machineConfigNodePath);
            Type type = machineConfigEntity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo p in properties)
            {
                if (p.Name == "C_Id" || p.Name == "C_Mid")
                {
                    continue;
                }
                XmlNode childNode = machineConfigsNode.SelectSingleNode(p.Name);
                childNode.InnerText = Convert.ToString(p.GetValue(machineConfigEntity, null));
            }
            document.Save(_section.Templates[machinetypename].Path);
            return true;
        }

        #region 字段与属性

        //private string _baseConfig = "basic";
        //private string _machineConfigHeadKey = "MachineConfig";
        //private string _machineModeConfigHeadKey = "MachineModeConfig";
        private string _machineConfigNodePath = "templateConfiguration/MachineConfig";
        private string _machineModeConfigNodePath = "templateConfiguration/MachineModeConfigs/MachineModeConfig";
        //private string _machineModeConfigsNodePath = "templateConfiguration/MachineModeConfigs";
        //private string _split = "_";
        private TemplateConfigurationSettingSection _section;
        private Hashtable _documents;

        #endregion
    }
}
