//#define INIDebug
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace GcDevicePc.Common
{
    class FileHelper
    {
        // 声明INI文件的写操作函数 WritePrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32", EntryPoint = "WritePrivateProfileStringW", CharSet = CharSet.Unicode)]
        private static extern long WritePrivateProfileStringW(string section, string key, string val, string filePath);

        // 声明INI文件的读操作函数 GetPrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32", EntryPoint = "GetPrivateProfileStringW", CharSet = CharSet.Unicode)]
        private static extern int GetPrivateProfileStringW(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 将指定的键值对写到指定的节点，如果已经存在则替换。
        /// </summary>
        /// <param name="lpAppName">节点，如果不存在此节点，则创建此节点</param>
        /// <param name="lpString">Item键值对，多个用\0分隔,形如key1=value1\0key2=value2
        /// <para>如果为string.Empty，则删除指定节点下的所有内容，保留节点</para>
        /// <para>如果为null，则删除指定节点下的所有内容，并且删除该节点</para>
        /// </param>
        /// <param name="lpFileName">INI文件</param>
        /// <returns>是否成功写入</returns>
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]     //可以没有此行
        private static extern bool WritePrivateProfileSection(string lpAppName, string lpString, string lpFileName);

        private string sPath = null;

        public FileHelper(string path)
        {
            this.sPath = path;
        }

        public void IniHelperRename(string path)
        {
            this.sPath = path;
        }


        /// <summary>
        /// 在INI文件中，删除指定节点中的所有内容。
        /// </summary>
        /// <param name="iniFile">INI文件</param>
        /// <param name="section">节点</param>
        /// <returns>操作是否成功</returns>
        public bool EmptySection(string section)
        {
            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException("必须指定节点名称", "section");
            }

            return WritePrivateProfileSection(section, string.Empty, this.sPath);
        }

        /// <summary>
        /// 写值
        /// </summary>
        /// <param name="section">配置节点</param>
        /// <param name="key">键名</param>
        /// <param name="value">键值</param>
        public void WriteValue(string section, string key, string value)
        {
            Int64 ret = 0;
            try
            {
                ret = WritePrivateProfileStringW(section, key, value, this.sPath);
                if(ret == 0)
                {
#if INIDebug
                    LogHelper.I("WritePrivateProfileStringW", e.Message.ToString());
#endif
                }
            }
            catch
            {

            }

#if INIDebug        
            catch(Exception e){

                LogHelper.I("WritePrivateProfileStringW", e.Message.ToString());

            }
#endif
        }

        /// <summary>
        /// 数据读取
        /// </summary>
        /// <param name="section">配置节</param>
        /// <param name="key">键名</param>
        /// <returns>读取的值</returns>
        public string ReadValue(string section, string key)
        {
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
            GetPrivateProfileStringW(section, key, "", temp, 255, this.sPath);
            return temp.ToString();
        }

        /// <summary>
        /// 擦出节点
        /// </summary>
        /// <param name="section"></param>
        public void EraseSection(string section)
        {
            WriteValue(section, null, null);
        }

        /// <summary>
        /// 擦除节点key
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        public void EraseSectionKey(string section,string key)
        {
            WriteValue(section, key, null);
        }

        /// <summary>
        /// 写BOOL
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public void WriteProfileBool(string section, string key, bool val)
        {
            string writeval = (val == false) ? "0" : "1";
            WriteValue(section, key, writeval);       
        }

        /// <summary>
        /// 写INT
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public void WriteProfileInt(string section, string key, int val)
        {
            string writeval = val.ToString();
            WriteValue(section, key, writeval);
        }

        /// <summary>
        /// 写Float
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public void WriteProfileFloat(string section, string key, float val)
        {
            string writeval = val.ToString();
            WriteValue(section, key, writeval);
        }

        /// <summary>
        /// 写String
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public void WriteProfileString(string section, string key, string val)
        {
            string writeval = val.ToString();
            WriteValue(section, key, writeval);
        }

        /// <summary>
        /// 获取INT
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="Default"></param>
        /// <returns></returns>
        public ushort GetProfileInt(string section, string key, ushort Default)
        {
            string val;
            int ret = 0;
            val = ReadValue(section, key);
            if(!int.TryParse(val, out ret))
            {
                ret = Default;
            }

            if(ret < 0)
            {
                ret = Default;
            }
            return (ushort)ret;
        }

        /// <summary>
        /// 获取Float
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="Default"></param>
        /// <returns></returns>
        public float GetProfileFloat(string section, string key, float Default)
        {
            string val;
            float ret = 0.0f;
            val = ReadValue(section, key);
            if (!float.TryParse(val, out ret))
            {
                ret = Default;
            }

            if (ret < 0)
            {
                ret = Default;
            }
            return ret;
        }

        /// <summary>
        /// 获取文本
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetProfileString(string section, string key)
        {
            string val;
            val = ReadValue(section, key);
            return val;
        }
    }
}
