using System.ComponentModel;
using System.IO.Ports;
using System.Text.Json.Serialization;
using AntdUI;
using DH.Commons.Enums; // 请确保此命名空间包含EnumPLCType

namespace DH.Commons.Base
{
    public class PLCBase : NotifyProperty
    {
        // 私有字段
        private bool _enable;
        private bool _connected;
        private string _plcName;
        private EnumPLCType _plcType;
        private string _com = "COM1";
        private int _baudRate = 9600;
        private int _dataBit = 8;
        private StopBits _stopBit = StopBits.One;
        private Parity _parity = Parity.None;
        private string _ip = "192.168.6.61";
        private int _port = 502;
        private BindingList<PLCItem> _PLCItemList = new BindingList<PLCItem>();

        [Category("设备配置")]
        [DisplayName("是否启用")]
        [Description("是否启用")]
        public bool Enable
        {
            get => _enable;
            set
            {
                if (_enable == value) return;
                _enable = value;
                OnPropertyChanged(nameof(Enable));
            }
        }


        [Category("状态监控")]
        [DisplayName("连接状态")]
        [Description("PLC连接状态")]
        public bool Connected
        {
            get => _connected;
            set
            {
                if (_connected == value) return;
                _connected = value;
                OnPropertyChanged(nameof(Connected));
            }
        }

        [Category("设备配置")]
        [DisplayName("PLC名称")]
        [Description("PLC设备名称")]
        public string PLCName
        {
            get => _plcName;
            set
            {
                if (_plcName == value) return;
                _plcName = value;
                OnPropertyChanged(nameof(PLCName));
            }
        }

        [Category("设备配置")]
        [DisplayName("PLC类型")]
        [Description("PLC通信协议类型")]
        public EnumPLCType PLCType
        {
            get => _plcType;
            set
            {
                if (_plcType == value) return;
                _plcType = value;
                OnPropertyChanged(nameof(PLCType));
            }
        }

        [Category("串口配置")]
        [DisplayName("COM端口")]
        [Description("串口号,如COM1")]
        public string COM
        {
            get => _com;
            set
            {
                if (_com == value) return;
                _com = value;
                OnPropertyChanged(nameof(COM));
            }
        }

        [Category("串口配置")]
        [DisplayName("波特率")]
        [Description("串口通信波特率")]
        public int BaudRate
        {
            get => _baudRate;
            set
            {
                if (_baudRate == value) return;
                _baudRate = value;
                OnPropertyChanged(nameof(BaudRate));
            }
        }

        [Category("串口配置")]
        [DisplayName("数据位")]
        [Description("数据位长度(5/6/7/8)")]
        public int DataBit
        {
            get => _dataBit;
            set
            {
                if (_dataBit == value) return;
                _dataBit = value;
                OnPropertyChanged(nameof(DataBit));
            }
        }

        [Category("串口配置")]
        [DisplayName("停止位")]
        [Description("停止位设置")]
        public StopBits StopBit
        {
            get => _stopBit;
            set
            {
                if (_stopBit == value) return;
                _stopBit = value;
                OnPropertyChanged(nameof(StopBit));
            }
        }

        [Category("串口配置")]
        [DisplayName("校验位")]
        [Description("奇偶校验方式")]
        public Parity Parity
        {
            get => _parity;
            set
            {
                if (_parity == value) return;
                _parity = value;
                OnPropertyChanged(nameof(Parity));
            }
        }

        [Category("网络配置")]
        [DisplayName("IP地址")]
        [Description("PLC网络地址")]
        public string IP
        {
            get => _ip;
            set
            {
                if (_ip == value) return;
                _ip = value;
                OnPropertyChanged(nameof(IP));
            }
        }

        [Category("网络配置")]
        [DisplayName("端口号")]
        [Description("网络通信端口")]
        public int Port
        {
            get => _port;
            set
            {
                if (_port == value) return;
                _port = value;
                OnPropertyChanged(nameof(Port));
            }
        }

        [Category("点位配置")]
        [DisplayName("点位配置")]
        [Description("点位配置")]
        public BindingList<PLCItem> PLCItemList
        {
            get => _PLCItemList;
            set
            {
                if (_PLCItemList == value) return;
                _PLCItemList = value;
                OnPropertyChanged(nameof(PLCItemList));
            }
        }

    
        public virtual bool PLCConnect()
        {
            Connected = true;
            return true;
        }

        public virtual bool PLCDisConnect()
        {
            Connected = false;
            return true;
        }

        public virtual Int16 ReadInt16(string address) { return 0; }

        public virtual Int32 ReadInt32(string address) { return 0; }

        public virtual UInt16 ReadUInt16(string address) { return 0; }

        public virtual UInt32 ReadUInt32(string address) { return 0; }
        public virtual float ReadFloat(string address) { return 0; }
        public virtual bool ReadBool(string address) { return false; }

        public virtual bool WriteInt16(string address, Int16 value, bool waitForReply = true) { return false; }
        public virtual bool WriteInt32(string address, Int32 value, bool waitForReply = true) { return false; }


        public virtual bool WriteUInt16(string address, UInt16 value, bool waitForReply = true) { return false; }
        public virtual bool WriteUInt32(string address, UInt32 value, bool waitForReply = true) { return false; }
   
        public virtual bool WriteFloat(string address, float value, bool waitForReply = true) { return false; }
        public virtual bool WriteBool(string address, bool value, bool waitForReply = true) { return false; }
    }


    public class PLCItem : NotifyProperty
    {
        private bool _selected;
        private string _name = string.Empty;
        private EnumPLCDataType _type;
        private string _value = string.Empty;
        private bool _startexecute;
        private int _startindex;
        private string _address;
        /// <summary>
        /// 是否选中
        /// </summary>
        public bool Selected
        {
            get => _selected;
            set
            {
                if (_selected != value)
                {
                    _selected = value;
                    OnPropertyChanged(nameof(Selected));
                }
            }
        }

        /// <summary>
        /// 参数名称
        /// </summary>
        public string Name
        {
            get => _name;
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged(nameof(Name));
                }
            }
        }
        public EnumPLCDataType Type
        {
            get => _type;
            set
            {
                if (_type != value)
                {
                    _type = value;
                    OnPropertyChanged(nameof(Type));
                }
            }
        }
        /// <summary>
        /// 参数类型
        /// </summary>
        public string Address
        {
            get => _address;
            set
            {
                if (_address != value)
                {
                    _address = value;
                    OnPropertyChanged(nameof(Address));
                }
            }
        }
 
        /// <summary>
        /// 参数值
        /// </summary>
        public string Value
        {
            get => _value;
            set
            {
                if (_value != value)
                {
                    _value = value;
                    OnPropertyChanged(nameof(Value));
                }
            }
        }

        /// <summary>
        /// 启用状态
        /// </summary>
        public bool StartExecute
        {
            get => _startexecute;
            set
            {
                if (_startexecute != value)
                {
                    _startexecute = value;
                    OnPropertyChanged(nameof(StartExecute));
                }
            }
        }





        /// <summary>
        /// 顺序
        /// </summary>
        public int StartIndex
        {
            get => _startindex;
            set
            {
                if (_startindex != value)
                {
                    _startindex = value;
                    OnPropertyChanged(nameof(StartIndex));
                }
            }
        }
       
        private CellLink[] cellLinks;
        [JsonIgnore]
        public CellLink[] CellLinks
        {
            get { return cellLinks; }
            set
            {
                if (cellLinks == value) return;
                cellLinks = value;
                OnPropertyChanged(nameof(CellLinks));
            }
        }
      
        //private CellTag[] cellTags;
        //[JsonIgnore]
        //public CellTag[] CellTags
        //{
        //    get { return cellTags; }
        //    set
        //    {
        //        if (cellTags == value) return;
        //        cellTags = value;
        //        OnPropertyChanged(nameof(CellTags));
        //    }
        //}
    }


}