using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace ChuPiao.Common.Utils
{
    public class LocalConfigUtil
    {
        public static int KeyInterval = 0;
        public static int TicketInterval = 0;

        private static Hashtable localConfigHashTable = new Hashtable();
        private static bool haslocalconfig = false;
        private static string localConfigPath = Path.Combine(Application.StartupPath, "configs/localconfig.xml");

        static LocalConfigUtil()
        {
            if (File.Exists(localConfigPath))
            {
                haslocalconfig = true;
                XmlDocument doc = new XmlDocument();
                doc.Load(localConfigPath);

                foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                {
                    localConfigHashTable.Add(node.Attributes["key"].InnerText, node.Attributes["value"].InnerText);
                }
            }
        }

        /// <summary>
        /// 保存设置到AppSetting,有就修改，没有就新增
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SaveConfig(string key, string value)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode root;
            if (!haslocalconfig)
            {
                XmlDeclaration xd = doc.CreateXmlDeclaration("1.0", null, null);
                doc.AppendChild(xd);
                root = doc.CreateElement("LocalConfigs");
                doc.AppendChild(root);
            }
            else
            {
                doc.Load(localConfigPath);
                root = doc.DocumentElement;
            }


            if (localConfigHashTable.ContainsKey(key))
            {
                foreach (XmlNode node in root.ChildNodes)
                {
                    if (node.Attributes["key"].InnerText == key)
                    {
                        node.Attributes["value"].InnerText = value;

                        localConfigHashTable[key] = value;
                        break;
                    }
                }
            }
            else
            {
                XmlNode config = doc.CreateElement("add");
                XmlAttribute keyatt = doc.CreateAttribute("key");
                keyatt.InnerText = key;
                config.Attributes.Append(keyatt);
                XmlAttribute valueatt = doc.CreateAttribute("value");
                valueatt.InnerText = value;
                config.Attributes.Append(valueatt);
                root.AppendChild(config);

                localConfigHashTable.Add(key, value);
            }
            doc.Save(localConfigPath);
        }


        public static string GetConfig(string key)
        {
            if (localConfigHashTable.ContainsKey(key))
            {
                return localConfigHashTable[key].ToString();
            }
            else
            {
                return null;
            }
        }
    }
}
