using System;
using System.ComponentModel;

namespace StandardScene.Charge
{
    /// <summary>
    /// 充电策略配置
    /// </summary>
    public class ChargeStrategyConfig
    {
        #region SOC 参数

        /// <summary>
        /// 必充电量 (%)
        /// </summary>
        [Description("必充电量")]
        [DisplayName("必充电量(%)")]
        public double MustChargeSoc { get; set; }

        /// <summary>
        /// 空闲充电电量 (%)
        /// </summary>
        [Description("空闲充电电量")]
        [DisplayName("空闲充电电量(%)")]
        public double IdleChargeSoc { get; set; }

        /// <summary>
        /// 任务可用电量 (%)
        /// </summary>
        [Description("任务可用电量")]
        [DisplayName("任务可用电量(%)")]
        public double TaskAvailableSoc { get; set; }

        /// <summary>
        /// 满电电量 (%)
        /// </summary>
        [Description("满电电量")]
        [DisplayName("满电电量(%)")]
        public double FullChargeSoc { get; set; }

        /// <summary>
        /// 允许中断电量 (%)
        /// </summary>
        [Description("允许中断电量")]
        [DisplayName("允许中断电量(%)")]
        public double AllowInterruptSoc { get; set; }

        #endregion

        #region 时间参数

        /// <summary>
        /// 空闲充电时间 (秒)
        /// </summary>
        [Description("空闲充电时间")]
        [DisplayName("空闲充电时间(秒)")]
        public double IdleChargeSeconds { get; set; }

        /// <summary>
        /// 空闲时间 (秒)
        /// </summary>
        [Description("空闲时间")]
        [DisplayName("空闲时间(秒)")]
        public double IdleSeconds { get; set; }

        /// <summary>
        /// 必充时间 (秒)
        /// </summary>
        [Description("必充时间")]
        [DisplayName("必充时间(秒)")]
        public double MustChargeSeconds { get; set; }

        /// <summary>
        /// 补电时间 (分钟)
        /// </summary>
        [Description("补电时间")]
        [DisplayName("补电时间(分钟)")]
        public double TopUpMinutes { get; set; }

        #endregion

        #region 任务参数

        /// <summary>
        /// 允许空闲车充电的最小任务数
        /// </summary>
        [Description("允许空闲车充电的最小任务数")]
        [DisplayName("最小任务数")]
        public int MinAllowFreeCarToChargeTaskCnt { get; set; }

        #endregion

        #region 开关参数

        /// <summary>
        /// 允许中断充电任务
        /// </summary>
        [Description("允许中断充电任务")]
        [DisplayName("允许中断任务")]
        public bool AllowInterruptTask { get; set; }

        /// <summary>
        /// 优先使用低电量车辆充电
        /// </summary>
        [Description("优先使用低电量车辆充电")]
        [DisplayName("优先低电量充电")]
        public bool UseLowerSocForCharge { get; set; }

        /// <summary>
        /// 启用充电错误检测
        /// </summary>
        [Description("启用充电错误检测")]
        [DisplayName("错误检测")]
        public bool EnableErrorChargeDetection { get; set; }

        /// <summary>
        /// 使用充电站点筛选
        /// </summary>
        [Description("使用充电站点筛选")]
        [DisplayName("站点筛选")]
        public bool UseChargeSiteFilter { get; set; }

        #endregion

        #region 构造函数

        public ChargeStrategyConfig()
        {
            // 使用默认值初始化
            SetDefaults();
        }

        /// <summary>
        /// 设置默认值
        /// </summary>
        private void SetDefaults()
        {
            // SOC 参数默认值
            MustChargeSoc = 20;
            IdleChargeSoc = 90;
            TaskAvailableSoc = 60;
            FullChargeSoc = 90;
            AllowInterruptSoc = 45;

            // 时间参数默认值
            IdleChargeSeconds = 30;
            IdleSeconds = 5;
            MustChargeSeconds = 60;
            TopUpMinutes = 5;

            // 任务参数默认值
            MinAllowFreeCarToChargeTaskCnt = 0;

            // 开关参数默认值
            AllowInterruptTask = false;
            UseLowerSocForCharge = true;
            EnableErrorChargeDetection = false;
            UseChargeSiteFilter = false;
        }

        /// <summary>
        /// 创建默认配置
        /// </summary>
        public static ChargeStrategyConfig CreateDefault()
        {
            return new ChargeStrategyConfig();
        }

        #endregion

        #region 验证

        /// <summary>
        /// 验证配置是否有效
        /// </summary>
        public bool Validate(out string errorMessage)
        {
            // 验证 SOC 范围
            if (MustChargeSoc < 0 || MustChargeSoc > 100)
            {
                errorMessage = "必充电量必须在 0-100 之间";
                return false;
            }

            if (IdleChargeSoc < 0 || IdleChargeSoc > 100)
            {
                errorMessage = "空闲充电电量必须在 0-100 之间";
                return false;
            }

            if (TaskAvailableSoc < 0 || TaskAvailableSoc > 100)
            {
                errorMessage = "任务可用电量必须在 0-100 之间";
                return false;
            }

            if (FullChargeSoc < 0 || FullChargeSoc > 100)
            {
                errorMessage = "满电电量必须在 0-100 之间";
                return false;
            }

            if (AllowInterruptSoc < 0 || AllowInterruptSoc > 100)
            {
                errorMessage = "允许中断电量必须在 0-100 之间";
                return false;
            }

            // 验证 SOC 逻辑关系
            if (MustChargeSoc >= IdleChargeSoc)
            {
                errorMessage = "必充电量必须小于空闲充电电量";
                return false;
            }

            if (TaskAvailableSoc <= MustChargeSoc)
            {
                errorMessage = "任务可用电量必须大于必充电量";
                return false;
            }

            if (FullChargeSoc < IdleChargeSoc)
            {
                errorMessage = "满电电量必须大于等于空闲充电电量";
                return false;
            }

            if (AllowInterruptSoc <= MustChargeSoc)
            {
                errorMessage = "允许中断电量必须大于必充电量";
                return false;
            }

            // 验证时间参数
            if (IdleChargeSeconds < 0)
            {
                errorMessage = "空闲充电时间不能为负数";
                return false;
            }

            if (IdleSeconds < 0)
            {
                errorMessage = "空闲时间不能为负数";
                return false;
            }

            if (MustChargeSeconds < 0)
            {
                errorMessage = "必充时间不能为负数";
                return false;
            }

            if (TopUpMinutes < 0)
            {
                errorMessage = "补电时间不能为负数";
                return false;
            }

            // 验证任务参数
            if (MinAllowFreeCarToChargeTaskCnt < 0)
            {
                errorMessage = "最小任务数不能为负数";
                return false;
            }

            errorMessage = string.Empty;
            return true;
        }

        #endregion

        #region 辅助方法

        /// <summary>
        /// 克隆配置
        /// </summary>
        public ChargeStrategyConfig Clone()
        {
            return new ChargeStrategyConfig
            {
                MustChargeSoc = this.MustChargeSoc,
                IdleChargeSoc = this.IdleChargeSoc,
                TaskAvailableSoc = this.TaskAvailableSoc,
                FullChargeSoc = this.FullChargeSoc,
                AllowInterruptSoc = this.AllowInterruptSoc,
                IdleChargeSeconds = this.IdleChargeSeconds,
                IdleSeconds = this.IdleSeconds,
                MustChargeSeconds = this.MustChargeSeconds,
                TopUpMinutes = this.TopUpMinutes,
                MinAllowFreeCarToChargeTaskCnt = this.MinAllowFreeCarToChargeTaskCnt,
                AllowInterruptTask = this.AllowInterruptTask,
                UseLowerSocForCharge = this.UseLowerSocForCharge,
                EnableErrorChargeDetection = this.EnableErrorChargeDetection,
                UseChargeSiteFilter = this.UseChargeSiteFilter
            };
        }

        /// <summary>
        /// 转换为字符串
        /// </summary>
        public override string ToString()
        {
            return $"充电策略配置 [必充:{MustChargeSoc}%, 空闲充:{IdleChargeSoc}%, 任务可用:{TaskAvailableSoc}%]";
        }

        #endregion
    }
}

