using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class IniHelper
    {
        [DllImport("kernel32")]
        public static extern bool WritePrivateProfileString(string lpapplication, string lpkeyname, string lpstring, string lpfilename);
        
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string lpszSection, string lpszKey, string lpszDefault, System.Text.StringBuilder lpReturnedString, int cchReturnBuffer, string lpsFile);

        public static string IniRead(string _filePath, string _section, string _key, string _default)
        {
            System.Text.StringBuilder _builder = new System.Text.StringBuilder(1024);
            int len = GetPrivateProfileString(_section, _key, _default, _builder, 1024, _filePath);
            string str = Convert.ToString(_builder).Trim();
            int num = str.IndexOf(Convert.ToChar(0));
            if (num >= 0) str = str.Substring(0, num - 1);
            return str;
        }

        public static bool IniWrite(string _filePath, string _section, string _key, string _value)
        {
            return WritePrivateProfileString(_section, _key, _value, _filePath);
        }
    }

    public class EnumItem<TEnum> where TEnum : struct, Enum
    {
        public TEnum Value { get; set; } // 枚举值（如MA）
        public string DisplayName { get; set; } // 显示文本（如“电容式”）
    }

    public class EnumHelperr
    {
        public static string GetDescription(Enum value)
        {
            Type type = value.GetType();
            MemberInfo[] memberInfo = type.GetMember(value.ToString());
            if (memberInfo.Length > 0)
            {
                object[] attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attributes.Length > 0)
                {
                    return ((DescriptionAttribute)attributes[0]).Description;
                }
            }
            return value.ToString();
        }
       
        public static List<EnumItem<TEnum>> GetEnumDescriptionList<TEnum>() where TEnum : struct, Enum
        {
            var list =new List<EnumItem<TEnum>>();
            foreach (TEnum enumValue in Enum.GetValues(typeof(TEnum)))
            {
                list.Add(new EnumItem<TEnum>
                {
                    Value = enumValue, // 存储原始枚举值
                    DisplayName = GetDescription(enumValue) // 存储显示文本
                });
            }
            return list;
        }

    }

}
