using System.ComponentModel;
using Newtonsoft.Json;

namespace StandardScene.Signal.Model
{
    /// <summary>
    /// 机构工位：一块 DB 上的一组 BOOL 点 + 地标。同一机构可多行（dock1 / dock2 / …）。
    /// </summary>
    [SignalTable("docks", "station-docks.json", "机构工位", Order = 2)]
    [Category("PLC数据管理")]
    [DisplayName("机构工位列表(机构名称+工位编号不能重复)")]
    public class PlcStationDockModel
    {
        /// <summary>外键，对应 <see cref="PlcStationModel.JgName"/>。</summary>
        [Category("基本")]
        [DisplayName("机构名称")]
        public string JgName { get; set; } = "";

        /// <summary>机构内唯一。空则按 default。</summary>
        [Category("基本")]
        [DisplayName("工位编号")]
        public string DockId { get; set; } = "dock1";

        [Category("PLC 读 BOOL")]
        [DisplayName("允许进入字节")]
        public int AllowInByte { get; set; }

        [Category("PLC 读 BOOL")]
        [DisplayName("允许进入位")]
        public int AllowInBit { get; set; }

        [Category("PLC 读 BOOL")]
        [DisplayName("允许离开字节")]
        public int AllowOutByte { get; set; }

        [Category("PLC 读 BOOL")]
        [DisplayName("允许离开位")]
        public int AllowOutBit { get; set; } = 1;

        [Category("PLC 写 BOOL")]
        [DisplayName("进入中字节")]
        public int EnteringByte { get; set; } = 2;

        [Category("PLC 写 BOOL")]
        [DisplayName("进入中位")]
        public int EnteringBit { get; set; }

        [Category("PLC 写 BOOL")]
        [DisplayName("到位字节")]
        public int ArrivedByte { get; set; } = 2;

        [Category("PLC 写 BOOL")]
        [DisplayName("到位位")]
        public int ArrivedBit { get; set; } = 1;

        [Category("PLC 写 BOOL")]
        [DisplayName("离开中字节")]
        public int LeavingByte { get; set; } = 2;

        [Category("PLC 写 BOOL")]
        [DisplayName("离开中位")]
        public int LeavingBit { get; set; } = 2;

        [Category("PLC 写 BOOL")]
        [DisplayName("已离开字节")]
        public int LeftByte { get; set; } = 2;

        [Category("PLC 写 BOOL")]
        [DisplayName("已离开位")]
        public int LeftBit { get; set; } = 3;

        [Category("地标")]
        [DisplayName("进入地标")]
        public int JgInPointId { get; set; }

        [Category("地标")]
        [DisplayName("进入包含地标")]
        public string JgInContains { get; set; } = "";

        [Category("地标")]
        [DisplayName("机构地标")]
        public int JgPointId { get; set; }

        [Category("地标")]
        [DisplayName("机构包含地标")]
        public string JgContains { get; set; } = "";

        /// <summary>旧配置合成：按一字节枚举读写，不走 BOOL。</summary>
        [Browsable(false)]
        [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
        public bool UseByteIo { get; set; }

        /// <summary>仅 UseByteIo 时使用的读字节地址。</summary>
        [Browsable(false)]
        [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
        public string ReadDb { get; set; }

        /// <summary>仅 UseByteIo 时使用的写字节地址。</summary>
        [Browsable(false)]
        [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
        public string WriteDb { get; set; }

        public string ResolvedDockId =>
            string.IsNullOrWhiteSpace(DockId) ? "default" : DockId.Trim();
    }
}
