using System.Data;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;

namespace BrilliantSightClient.Model.Helper;

public class Common
{
    public const string AesKey = "2AB6AA27B1302AEB747E952D5C5C73B9";

    public const string AesIV = "B1302AEB747E952D";
    
    public static readonly string BasePath = AppDomain.CurrentDomain.BaseDirectory;
    
    public const string DataBaseFileName = "BrilliantSightED.db";
    public const string DataBaseTempFileName = "temp.db";
    public const string DataBaseName = "BrilliantSightED";
    public const string DatabasePwd = "DyumL4FvF6wVHWw";
    
    public static int RunMode { get; set; } = 0;

    public static object LastParam = "ROUND P8 P8";
    
    
    /// <summary>
    /// 将 DataTable 转换为 JSON 字符串
    /// </summary>
    /// <param name="table">要转换的 DataTable</param>
    /// <returns>JSON 字符串</returns>
    public static string DataTableToJson(DataTable table)
    {
        if (table == null)
        {
            throw new ArgumentNullException(nameof(table), "DataTable cannot be null.");
        }

        return JsonConvert.SerializeObject(table, Formatting.Indented);
    }

    /// <summary>
    /// 将 JSON 字符串转换为 DataTable
    /// </summary>
    /// <param name="json">要转换的 JSON 字符串</param>
    /// <returns>还原的 DataTable</returns>
    public static DataTable JsonToDataTable(string json)
    {
        if (string.IsNullOrEmpty(json))
        {
            throw new ArgumentNullException(nameof(json), "JSON string cannot be null or empty.");
        }

        return JsonConvert.DeserializeObject<DataTable>(json);
    }
    
    /// <summary>
    /// 将 DataTable 转换为实体对象列表
    /// </summary>
    /// <typeparam name="T">目标实体类型</typeparam>
    /// <param name="table">要转换的 DataTable</param>
    /// <returns>实体对象列表</returns>
    public static List<T> ConvertToEntityList<T>(DataTable table) where T : new()
    {
        if (table == null)
        {
            throw new ArgumentNullException(nameof(table), "DataTable cannot be null.");
        }

        var entityList = new List<T>();

        // 获取实体类型的所有属性
        var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (DataRow row in table.Rows)
        {
            // 创建实体对象
            var entity = new T();

            foreach (var property in properties)
            {
                // 检查 DataTable 是否包含与属性名匹配的列
                if (table.Columns.Contains(property.Name) && row[property.Name] != DBNull.Value)
                {
                    // 设置实体属性值
                    property.SetValue(entity, Convert.ChangeType(row[property.Name], property.PropertyType));
                }
            }

            entityList.Add(entity);
        }

        return entityList;
    }
    
    public static string GenerateMd5Hash(string input)
    {
        using (var md5 = MD5.Create())
        {
            var inputBytes = Encoding.UTF8.GetBytes(input);
            var hashBytes = md5.ComputeHash(inputBytes);
            return string.Concat(hashBytes.Select(b => b.ToString("X2")));
        }
    }
    
    public static string AssemblyFileVersion()
    {
        object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
        if (attributes.Length == 0)
        {
            return "";
        }
        else
        {
            return ((AssemblyFileVersionAttribute)attributes[0]).Version;
        } 
    }
}