using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SWRIS.Enums;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;

namespace SWRIS.Models
{
    public class ConfigModel
    {
        public string AppName { get; set; }
        public string Version { get; set; }
        public string Copyright { get; set; }
        public string IpAddress { get; set; }
        public int Port { get; set; }
        public int DataSaveDays { get; set; } = 120;
        public TimeSpan CleanScheduledTime { get; set; }
        public int DamageNearPointCount { get; set; } = 20;
        public bool ShowDebugMessage { get; set; } = false;
        public RiskLevel SoundRiskLevel { get; set; } = RiskLevel.Moderate;
        public double SpeedVariationThreshold { get; set; } = 0.25;
        public string Crane { get; set; }
        public List<EquipmentModel> Equipments { get; set; }
        public List<SwitchInstance> SwitchInstances { get; set; }
        public CalibrationSettings Calibration { get; set; }
        public RiskLevelSettings RiskLevelSettings { get; set; }
        public double DamageExtent { get; set; }
        public int AlarmValidCount { get; set; }

        public string GenerateNewSerialNo()
        {
            string prefix = DateTime.Now.ToString("yyMM");

            // 获取当前月份最大序号
            int max = Equipments.Select(e => e.SerialNo)?
                                .Where(s => s != null && s.StartsWith(prefix))
                                .Select(s => int.Parse(s.Substring(4)))
                                .DefaultIfEmpty(0)
                                .Max() ?? 0;

            // 生成新序号（最大+1，格式化为3位数）
            return $"{prefix}{(max + 1):D3}";

        }
    }
    public class CalibrationSettings
    {
        public ModuleType ModuleType { get; set; }
        public List<LimitData> Limits { get; set; }
        public TcpSettings Tcp { get; set; }
        public SerialSettings Serial { get; set; }
    }
    public class TcpSettings
    {
        public string IpAddress { get; set; }
        public int Port { get; set; }
        public byte Station { get; set; } = 1;
    }
    public class SerialSettings
    {
        public string PortName { get; set; }
        public int BaudRate { get; set; }
        public int DataBits { get; set; }
        [JsonConverter(typeof(StringEnumConverter))]
        public Parity Parity { get; set; }
        public StopBits StopBits { get; set; }
        public byte Station { get; set; } = 1;
    }

    public class RiskLevelSettings
    {
        public List<RiskLevelConfig> Levels { get; set; }
    }
    public class RiskLevelConfig
    {
        public RiskLevel Level { get; set; }
        public Range MaxDamageRange { get; set; }
        public Range AvgDamageRange { get; set; }
    }
    public class Range
    {
        public int Min { get; set; }
        public int Max { get; set; }
    }
    public class LimitData
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public double Position { get; set; }
        public bool IsReverse { get; set; }
        public LimitState State { get; set; }
        public bool IsEnable { get; set; }
        public List<int> RelatedRopes { get; set; } = new List<int>();
    }
    public class SwitchInstance
    {
        public string InstanceName { get; set; }
        public string ProgressName { get; set; }
        public string ImageSource { get; set; }
    }
}
