using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using DeviceLibrary.manager;
namespace DeviceLibrary
{
    public class Node
    {
        /// <summary>
        /// 节点ID
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 节点名称
        /// </summary>
        public string Name { get; set; }
        public NodeType Type { get; set; }
        /// <summary>
        /// 所属区域
        /// </summary>
        public Area Area { get; set; }
        /// <summary>
        /// 节点IP
        /// </summary>
        public string IP { get; set; }
        /// <summary>
        /// 在线状态
        /// </summary>
        public bool Online { get; set; }
        /// <summary>
        /// 是否可调用
        /// </summary>
        public bool IsUse
        {
            set
            {
                if (!isuse.Equals(value))
                {
                    isuse = value;
                    XmlConfigOpManager.SetNodeIsUse(this);
                }
            }
            get { return isuse; }
        }
        private bool isuse;
        /// <summary>
        /// 节点状态
        /// </summary>
        public NodeStatus nodeStatus { get; set; }
        protected string rfid = "00";

        /// <summary>
        /// RFID
        /// </summary>
        public string RFID
        {
            set
            {
                if (value.Length < 2)
                    rfid = value.PadLeft(2, '0');
                else
                    rfid = value;
            }
            get
            {
                return rfid;
            }
        }

        public ClientLevel ClientLevel { get; set; } = ClientLevel.Low;
        /// <summary>
        /// 别名
        /// </summary>
        public string AliceName { get; set; }
        /// <summary>
        /// 警告信息
        /// </summary>
        public string WarnMsg { get; set; } = "";
        public Node() { }
        public Node(int id, string name, string ip, NodeType nodeType, Area area, bool isUse = false)
        {
            Id = id;
            Name = name;
            IP = ip;
            Online = false;
            IsUse = isUse;
            Type = nodeType;
            Area = area;
            nodeStatus = NodeStatus.None;
        }

        public Node(string name)
        {
            Name = name;
        }
        /// <summary>
        /// 状态比较
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool StateEquals(NodeStatus obj)
        {
            return nodeStatus.Equals(obj);
        }
        /// <summary>
        /// 获取当前状态
        /// </summary>
        /// <returns></returns>
        public NodeStatus GetState()
        {
            return nodeStatus;
        }
        /// <summary>
        /// 更新节点状态
        /// </summary>
        /// <param name="status"></param>
        public void UpdateNodeStatus(NodeStatus status)
        {
            nodeStatus = status;
        }
        /// <summary>
        /// 客户端节点
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ip"></param>
        /// <param name="isUse"></param>
        public Node(int id, string name, string ip, string aliceName, NodeType nodeType, Area area, bool isUse)
        {
            Id = id;
            Name = name;
            IP = ip;
            Type = nodeType;
            Area = area;
            IsUse = isUse;
            AliceName = aliceName;
            Online = false;
        }
        /// <summary>
        /// 客户端节点
        /// </summary>
        /// <param name="name"></param>
        /// <param name="rfid"></param>
        /// <param name="action"></param>
        /// <param name="level"></param>
        public Node(string name, string rfid = "", NodeStatus status = NodeStatus.None)
        {
            Name = name;
            RFID = rfid;
            nodeStatus = status;
        }

        /// <summary>
        /// 节点状态的文本形式
        /// </summary>
        /// <returns></returns>
        public string StatetText()
        {
            string s = string.Format("[Name={0}, NodeStatus={1}, RFID={2},ClientLevel ={3}]", Name, nodeStatus.ToString(), RFID, ClientLevel.ToString());
            return s;
        }

        public string[] ToRow()
        {
            //节点,IP,动作,RFID,AGV名称,在线,调用,清除AGV
            List<string> vs = new List<string>();
            vs.Add(AliceName);
            vs.Add(IP);
            vs.Add(nodeStatus.ToString());
            vs.Add(ClientLevel.ToString());
            vs.Add(RFID);
            vs.Add(Online ? "在线" : "离线");
            vs.Add(IsUse ? "是" : "否");

            return vs.ToArray();
        }
        /// <summary>
        /// 脱机
        /// </summary>
        public void Offline()
        {
            RFID = "00";
            nodeStatus = NodeStatus.None;
            Online = false;
        }
        public Node ToCopy()
        {
            PropertyInfo[] infos = this.GetType().GetProperties();
            Node node = new Node();
            PropertyInfo[] infos2 = node.GetType().GetProperties();
            for (int i = 0; i < infos2.Length; i++)
            {
                infos2[i].SetValue(node, Convert.ChangeType(infos[i].GetValue(this), infos[i].PropertyType));
            }
            return node;
        }
        public override string ToString()
        {
            return $" {AliceName}[{Area}] ";
        }
    }
    /// <summary>
    /// 节点状态
    /// </summary>
    public enum NodeStatus : byte
    {
        /// <summary>
        /// 没有动作
        /// </summary>
        None = 0,
        /// <summary>
        /// 需要7寸D料架
        /// </summary>
        NeedD = 1,
        /// <summary>
        /// 需要大尺寸C料架
        /// </summary>
        NeedC = 2,
        /// <summary>
        /// 需要进入料架
        /// </summary>
        NeedEnter = 3,
        /// <summary>
        /// 需要出去料架
        /// </summary>
        NeedLeave = 4,
        /// <summary>
        /// 需要进入离开料架
        /// </summary>
        NeedEnterLeave = 5,
        /// <summary>
        /// 准备进入料架,服务器发送
        /// </summary>
        ReadyEnter = 6,
        /// <summary>
        /// 可以进入料架
        /// </summary>
        MayEnter = 7,
        /// <summary>
        /// 完成进入料架
        /// </summary>
        FinishEnter = 8,
        /// <summary>
        /// 准备离开料架,服务器发送
        /// </summary>
        ReadyLeave = 9,
        /// <summary>
        /// 可以出去料架
        /// </summary>
        MayLeave = 10,
        /// <summary>
        /// 完成出去料架
        /// </summary>
        FinishLeave = 11,

    }

    /// <summary>
    /// 客户端的优先级
    /// </summary>
    public enum ClientLevel : byte
    {
        /// <summary>
        /// 低
        /// </summary>
        Low = 0,
        /// <summary>
        /// 中等
        /// </summary>
        Middle = 1,
        /// <summary>
        /// 高
        /// </summary>
        High = 2
    }
    public enum Area
    {
        D,
        C,
        Air_C,
    }
    public enum NodeType
    {
        AutoCharge,
        Standby,
        Node
    }
}