using System;
using System.ComponentModel;
using Newtonsoft.Json;

namespace StandardScene.Charge
{
    /// <summary>
    /// 充电桩数据模型
    /// </summary>
    public class ChargeStation
    {
        /// <summary>
        /// 充电桩编号（唯一标识）
        /// </summary>
        [DisplayName("编号")]
        public string StationId { get; set; } = "1";

        /// <summary>
        /// 充电桩名称
        /// </summary>
        [DisplayName("名称")]
        public string Name { get; set; }

        /// <summary>
        /// 充电桩类型
        /// </summary>
        [DisplayName("类型")]
        public ChargeStationType Type { get; set; }

        /// <summary>
        /// 充电方式
        /// </summary>
        [DisplayName("充电方式")]
        public ChargeMethodType ChargeMethod { get; set; }

        /// <summary>
        /// IP地址
        /// </summary>
        [DisplayName("IP地址")]
        public string IpAddress { get; set; }

        /// <summary>
        /// 端口号
        /// </summary>
        [DisplayName("端口")]
        public int Port { get; set; }

        /// <summary>
        /// 通讯类型 (UDP/TCP)
        /// </summary>
        [DisplayName("通讯类型")]
        public string CommunicationType { get; set; } = "TCP";

        /// <summary>
        /// 额定电压 (V)
        /// </summary>
        [DisplayName("电压(V)")]
        public double SetVoltage { get; set; }

        /// <summary>
        /// 额定电流 (A)
        /// </summary>
        [DisplayName("电流(A)")]
        public double SetElectricCurrent { get; set; }

        /// <summary>
        /// 实时电压 (V) - 当前充电时的实际电压
        /// </summary>
        [DisplayName("实时电压(V)")]
        [JsonIgnore]
        public double RealTimeVoltage { get; set; }

        /// <summary>
        /// 实时电流 (A) - 当前充电时的实际电流
        /// </summary>
        [DisplayName("实时电流(A)")]
        [JsonIgnore]
        public double RealTimeCurrent { get; set; }

        /// <summary>
        /// 最后发送数据时间
        /// </summary>
        [DisplayName("发送时间")]
        [JsonIgnore]
        public DateTime? LastSendTime { get; set; }

        /// <summary>
        /// 最后接收数据时间
        /// </summary>
        [DisplayName("接收时间")]
        [JsonIgnore]
        public DateTime? LastReceiveTime { get; set; }

        /// <summary>
        /// 是否有报警
        /// </summary>
        [DisplayName("报警")]
        [JsonIgnore]
        public bool HasAlarm { get; set; }

        /// <summary>
        /// 报警信息
        /// </summary>
        [DisplayName("报警信息")]
        [JsonIgnore]
        public string AlarmMessage { get; set; }

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

        /// <summary>
        /// 网络通讯状态
        /// </summary>
        [DisplayName("通讯状态")]
        [JsonIgnore]
        public CommunicationStatus CommStatus { get; set; }

        /// <summary>
        /// 最后通讯成功时间
        /// </summary>

        [JsonIgnore]
        [DisplayName("最后通讯时间")]
        public DateTime? LastCommunicationTime { get; set; }

        /// <summary>
        /// 机构伸缩状态
        /// </summary>
         [JsonIgnore]
        [DisplayName("机构状态")]
        public MechanismStatus MechanismStatus { get; set; }

        [DisplayName("屏蔽机构状态交互")]
        public bool ShieldSiteMechanismStatus { get; set; }


        /// <summary>
        /// 当前充电车辆编号
        /// </summary>
        [DisplayName("当前车辆")]
        [JsonIgnore]
        public string CurrentVehicle { get; set; }

        /// <summary>
        /// 当前电量百分比 (0-100)
        /// </summary>
        [DisplayName("电量")]
        [JsonIgnore]
        public double BatteryLevel { get; set; }

        /// <summary>
        /// 发送充电的状态
        /// </summary>
        [DisplayName("充电指令状态")]
        [JsonIgnore]
        public ChargeCommandStatus ChargeCommandStatus { get; set; }

        /// <summary>
        /// 充电桩状态
        /// </summary>
        [DisplayName("状态")]
        [JsonIgnore]
        public ChargeStationStatus Status { get; set; }

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

        [DisplayName("停靠车辆类型")]
        public ChargeStationCarType GroupCarType { get; set; }

        /// <summary>
        /// 关联的站点ID（可选）
        /// </summary>
        [DisplayName("站点ID")]
        public int? SiteId { 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; }

        /// <summary>
        /// 计算功率 (W)
        /// </summary>
        [JsonIgnore]
        [DisplayName("功率(W)")]
        public double Power => SetVoltage * SetElectricCurrent;

        public ChargeStation()
        {
            StationId = GenerateStationId();
            Type = ChargeStationType.FRLDTall; // 默认FRLD高款充电桩
            ChargeMethod = ChargeMethodType.Ground; // 默认地充
            Status = ChargeStationStatus.Idle;
            Enabled = true;
            CreatedTime = DateTime.Now;
            ModifiedTime = DateTime.Now;
            Port = 502; // 默认Modbus TCP端口
            CommunicationType = "UDP"; // 默认UDP通讯
            SetVoltage = 29.2;
            SetElectricCurrent = 45.0;
        }

        /// <summary>
        /// 生成充电桩编号
        /// </summary>
        private static string GenerateStationId()
        {
            return "1";
            //return $"CS{DateTime.Now:yyyyMMddHHmmss}{new Random().Next(1000, 9999)}";
        }

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

            if (string.IsNullOrWhiteSpace(Name))
            {
                errorMessage = "充电桩名称不能为空";
                return false;
            }

            if (string.IsNullOrWhiteSpace(IpAddress))
            {
                errorMessage = "IP地址不能为空";
                return false;
            }

            // 验证IP格式
            if (!System.Net.IPAddress.TryParse(IpAddress, out _))
            {
                errorMessage = "IP地址格式不正确";
                return false;
            }

            // 验证端口范围
            if (Port < 1 || Port > 65535)
            {
                errorMessage = "端口号必须在 1-65535 之间";
                return false;
            }

            // 验证电压范围
            if (SetVoltage <= 0 || SetVoltage > 64)
            {
                errorMessage = "电压必须在 0-64V 之间";
                return false;
            }

            // 验证电流范围
            if (SetElectricCurrent <= 0 || SetElectricCurrent > 101)
            {
                errorMessage = "电流必须在 0-101A 之间";
                return false;
            }

            errorMessage = string.Empty;
            return true;
        }

        public override string ToString()
        {
            return $"[{StationId}] {Name} ({IpAddress}:{Port}) - {Status}";
        }
    }

    /// <summary>
    /// 充电桩类型枚举
    /// </summary>
    public enum ChargeStationType
    {
        [Description("FRLD高款充电桩")]
        FRLDTall = 0,

        [Description("FRLD矮款充电桩")]
        FRLDShort = 1,

        [Description("牧星充电桩")]
        MuXing = 2

        // 后续可在此处添加其他充电桩类型
    }

    public enum ChargeStationCarType
    {
        [Description("FRLD充电")]
        FRLD = 0,

        [Description("牧星充电桩充电")]
        MuXing = 1

        // 后续可在此处添加其他充电桩类型
    }
    /// <summary>
    /// 充电桩状态枚举
    /// </summary>
    public enum ChargeStationStatus
    {
        [Description("空闲")]
        Idle = 0,

        [Description("充电中")]
        Charging = 1,

        [Description("报警中")]
        Fault = 2,

        [Description("AGV电池已接入")]
        Battery = 3
    }

    /// <summary>
    /// 报警级别枚举
    /// </summary>
    public enum AlarmLevel
    {
        [Description("无")]
        None = 0,

        [Description("低")]
        Low = 1,

        [Description("中")]
        Medium = 2,

        [Description("高")]
        High = 3,

        [Description("严重")]
        Critical = 4
    }

    /// <summary>
    /// 机构伸缩状态枚举
    /// </summary>
    public enum MechanismStatus
    {
        [Description("伸出")]
        Extended = 1,

        [Description("缩回")]
        Retracted = 2,

        [Description("运动中")]
        Extending = 3,

    }

    /// <summary>
    /// 网络通讯状态枚举
    /// </summary>
    public enum CommunicationStatus
    {
        [Description("未知")]
        Unknown = 0,

        [Description("正常")]
        Normal = 1,

        [Description("延迟")]
        Delayed = 2,

        [Description("超时")]
        Timeout = 3,

        [Description("断开")]
        Disconnected = 4,

        [Description("错误")]
        Error = 5
    }

    /// <summary>
    /// 充电指令状态枚举
    /// </summary>
    public enum ChargeCommandStatus
    {
        [Description("停止")]
        Stopped = 0,

        [Description("启动")]
        Started = 1
    }


    /// <summary>
    /// 充电方式枚举
    /// </summary>
    public enum ChargeMethodType
    {
        [Description("地充")]
        Ground = 0,

        [Description("尾充")]
        Rear = 1,

        [Description("侧充")]
        Side = 2
    }
}



