using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;
using System.IO;

namespace Kanban
{
    //ini文件操作类
    public class INIClass
    {
        public delegate void EventHandler(object sender, EventArgs e);          //定义委托
        public event EventHandler IniFileChanged;                               //绑定事件
        public event EventHandler Initialization;

        protected string inipath;

        public string INIPath      //设置属性
        {
            get
            {
                return inipath;
            }
            set
            {
                if (value != inipath)
                {
                    inipath = value;
                    OnIniFileChanged(new EventArgs());
                }
            }
        }

        protected void OnIniFileChanged(EventArgs e)
        {
            if (IniFileChanged != null)
                IniFileChanged(null, e);
        }
        protected void OnInitialization(EventArgs e)
        {
            if (Initialization != null)
                Initialization(null, e);
        }



        [DllImport("kernel32")]                 //导用win32 API函数，非托管代码
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        /*
         section: 要写入的段落名
         key: 要写入的键，如果该key存在则覆盖写入
         val: key所对应的值
         filePath: INI文件的完整路径和文件名 
        */

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        /*
         section：要读取的段落名
         key: 要读取的键
         defVal: 读取异常的情况下的缺省值
         retVal: key所对应的值，如果该key不存在则返回空值
         size: 值允许的大小
         filePath: INI文件的完整路径和文件名
        */


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="INIPath">文件路径</param>
        public INIClass(string INIPath)
        {
            inipath = INIPath;
        }

        ///<summary>
        ///写入INI文件
        ///</summary>
        ///<param name="section">项目名称(如[TypeName])</param>
        ///<param name="Key">键</param>
        /// <param name="Value">值</param>
        public void IniWriteValue(string section, string key, string val)
        {
            WritePrivateProfileString(section, key, val, this.inipath);
        }

        ///<summary>
        ///读出INI文件
        ///</summary>
        ///<param name="section">项目名称(如 [TypeName])</param>
        ///<param name="key">键</param>
        public string IniReadValue(string section, string key, string Default)
        {
            if ((section == "") || (key == ""))
            {
                return "NG";
            }
            if (this.inipath == "")
            {
                return "File path is not exist!";
            }

            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(section, key, "", temp, 500, this.inipath);
            return temp.ToString();
        }

        ///<summary>
        ///验证文件是否存在
        ///</summary>
        ///<returns>布尔值</returns>
        public bool ExistINIFile()
        {
            return File.Exists(inipath);
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="path">路径</param>
        private void CreateNewDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// 添加一行注释
        /// </summary>
        /// <param name="Notes">注释</param>
        public void AddNotes(string Notes)
        {
            string filepath = inipath;
            string path;
            path = Directory.GetParent(filepath).ToString();
            CreateNewDirectory(path);
            FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.WriteLine(@";" + Notes);
            sw.Flush();
            sw.Close();
            fs.Close();
            sw.Dispose();
            fs.Dispose();
        }

        /// <summary>
        /// 添加一行文本
        /// </summary>
        /// <param name="Text">文本</param>
        public void AddText(string Text)
        {
            string filepath = inipath;
            string path;
            path = Directory.GetParent(filepath).ToString();
            CreateNewDirectory(path);
            FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.WriteLine(Text);
            sw.Flush();
            sw.Close();
            fs.Close();
            sw.Dispose();
            fs.Dispose();
        }

        #region  "重载"
        public void IniWriteValue(string Section, string Key, int Value)        //写函数 类型为空
        {
            IniWriteValue(Section, Key, Value.ToString());
        }
        public void IniWriteValue(string Section, string Key, double Value)
        {
            IniWriteValue(Section, Key, Value.ToString());
        }
        public void IniWriteValue(string Section, string Key, Boolean Value)
        {
            IniWriteValue(Section, Key, Value.ToString());
        }
        public void IniWriteValue(string Section, string Key, DateTime Value)
        {
            IniWriteValue(Section, Key, Value.ToString());
        }
        public void IniWriteValue(string Section, string Key, object Value)
        {
            IniWriteValue(Section, Key, Value.ToString());
        }


        public int IniReadValue(string Section, string Key, int Default)            //读函数 有返回值 类型
        {
            return Convert.ToInt32(IniReadValue(Section, Key, Default.ToString()));
        }
        public double IniReadValue(string Section, string Key, double Default)
        {
            return Convert.ToDouble(IniReadValue(Section, Key, Default.ToString()));
        }
        public bool IniReadValue(string Section, string Key, bool Default)
        {
            return Convert.ToBoolean(IniReadValue(Section, Key, Default.ToString()));
        }
        public DateTime IniReadValue(string Section, string Key, DateTime Default)
        {
            return Convert.ToDateTime(IniReadValue(Section, Key, Default.ToString()));
        }
        public string IniReadValue(string Section, string Key)
        {
            return IniReadValue(Section, Key, "");
        }
        #endregion
        


    }
}
