using System;
using System.ComponentModel;

namespace StandardScene.Charge
{
    /// <summary>
    /// 报警配置数据模型
    /// </summary>
    public class AlarmConfig
    {
        /// <summary>
        /// 报警编号（自动生成）
        /// </summary>
        [DisplayName("编号")]
        public string AlarmId { get; set; }

        /// <summary>
        /// 报警编码值
        /// </summary>
        [DisplayName("报警编码")]
        public int AlarmCode { get; set; }

        /// <summary>
        /// 报警内容描述
        /// </summary>
        [DisplayName("报警内容")]
        public string AlarmContent { get; set; }

        /// <summary>
        /// 报警级别
        /// </summary>
        [DisplayName("报警级别")]
        public AlarmLevel Level { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        [DisplayName("启用")]
        public bool Enabled { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [DisplayName("备注")]
        public string Remarks { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [DisplayName("创建时间")]
        public DateTime CreatedTime { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        [DisplayName("修改时间")]
        public DateTime ModifiedTime { get; set; }

        public AlarmConfig()
        {
            AlarmId = GenerateAlarmId();
            Level = AlarmLevel.Medium;
            Enabled = true;
            CreatedTime = DateTime.Now;
            ModifiedTime = DateTime.Now;
        }

        /// <summary>
        /// 生成报警编号
        /// </summary>
        private static string GenerateAlarmId()
        {
            return $"ALM{DateTime.Now:yyyyMMddHHmmss}{new Random().Next(100, 999)}";
        }

        /// <summary>
        /// 验证数据有效性
        /// </summary>
        public bool IsValid(out string errorMessage)
        {
            if (string.IsNullOrWhiteSpace(AlarmId))
            {
                errorMessage = "报警编号不能为空";
                return false;
            }

            if (AlarmCode < 0)
            {
                errorMessage = "报警编码不能为负数";
                return false;
            }

            if (string.IsNullOrWhiteSpace(AlarmContent))
            {
                errorMessage = "报警内容不能为空";
                return false;
            }

            errorMessage = string.Empty;
            return true;
        }

        public override string ToString()
        {
            return $"[{AlarmCode}] {AlarmContent}";
        }
    }
}

