using WCS.Core;
using PLCType = WCS.Core.PLCType;

namespace WCS.WorkEngineering.Extensions
{
    /// <summary>
    ///  设备扩展
    /// </summary>
    public static class DeviceExtension
    {
        #region 设备类型

        public static List<ProtocolInfo> ProtocolInfos = new List<ProtocolInfo>();

        /// <summary>
        ///  是否是巷道
        /// </summary>
        /// <param name="source">设备信息</param>
        /// <returns></returns>
        public static bool IsTunnel(this Device source)
        {
            return source.HasFlag(DeviceFlags.巷道);
        }

        #endregion 设备类型

        #region 标签判断

        public static bool HasFlag(this Device device, params DeviceFlags[] flag)
        {
            foreach (var item in flag)
            {
                if (device.HasFlag(item)) continue;
                return false;
            }
            return true;
        }

        #endregion 标签判断

        #region 协议操作扩展

        public static void AddProtocol<T>(this Device device, int position, ushort db, string ip)
        {
            var info = new ProtocolInfo
            {
                Position = position,
                DBInfo = new DBInfo
                {
                    No = db,
                    PLCInfo = new PLCInfo
                    {
                        IP = ip,
                        Port = 102,
                        Rack = 0,
                        Slot = 1,
                        Type = PLCType.Siemens
                    }
                }
            };
            ProtocolInfos.Add(info);
            device.AddProtocol<T>(info);
        }

        #endregion 协议操作扩展
    }

    /// <summary>
    ///  设备标签
    /// </summary>
    [Flags]
    public enum DeviceFlags

    {
        扫码 = 1 << 0,
        称重 = 1 << 1,
        外检 = 1 << 2,

        顶升 = 1 << 3,
        移栽 = 1 << 4,
        旋转 = 1 << 5,

        入库 = 1 << 6,
        出库 = 1 << 7,

        巷道口 = 1 << 8,
        RGV口 = 1 << 9,
        AGV口 = 1 << 10,

        直轨 = 1 << 11,
        弯轨 = 1 << 12,
        环轨 = 1 << 13,

        巷道 = 1 << 14,
        堆垛机 = 1 << 15,
        输送机 = 1 << 16,
        一楼出库口 = 1 << 17,
        二楼出库口 = 1 << 18,
        一楼入库口 = 1 << 19,
        二楼入库口 = 1 << 20,
    }

    /// <summary>
    /// 输送机段信息
    /// </summary>
    public class StationSegmentInfo
    {
        /// <summary>
        ///  构造函数
        /// </summary>
        /// <param name="start">起始设备号</param>
        /// <param name="end">结束设备号</param>
        /// <param name="ip">ip</param>
        public StationSegmentInfo(int start, int end, string ip)
        {
            Start = start;
            End = end;
            IP = ip;
        }

        /// <summary>
        /// 起始设备编号
        /// </summary>
        public int Start { get; set; }

        /// <summary>
        ///结束设备编号
        /// </summary>
        public int End { get; set; }

        /// <summary>
        ///  输送机段所属IP
        /// </summary>
        public string IP { get; set; }
    }

    /// <summary>
    ///  扫码器信息
    /// </summary>
    public class BcrInfo
    {
        /// <summary>
        ///  构造函数
        /// </summary>
        /// <param name="deviceNo">设备编号</param>
        /// <param name="ip">ip</param>
        public BcrInfo(int[] deviceNo, string ip)
        {
            DeviceNo = deviceNo;
            IP = ip;
        }

        /// <summary>
        ///  设备编号
        /// </summary>
        public int[] DeviceNo { get; set; }

        /// <summary>
        ///  ip
        /// </summary>
        public string IP { get; set; }
    }

    /// <summary>
    ///  外形信息
    /// </summary>
    public class ShapeInfo
    {
        /// <summary>
        ///  构造函数
        /// </summary>
        /// <param name="deviceNo">设备编号</param>
        /// <param name="ip">ip</param>
        public ShapeInfo(int[] deviceNo, string ip)
        {
            DeviceNo = deviceNo;
            IP = ip;
        }

        /// <summary>
        ///  设备编号
        /// </summary>
        public int[] DeviceNo { get; set; }

        /// <summary>
        ///  ip
        /// </summary>
        public string IP { get; set; }
    }

    /// <summary>
    /// 路径信息
    /// </summary>
    public class RouteInfo
    {
        /// <summary>
        ///  构造函数
        /// </summary>
        /// <param name="deviceCode">起始点设备号</param>
        /// <param name="nextList">下一个设备集合</param>
        public RouteInfo(string deviceCode, string[] nextList)
        {
            DeviceCode = deviceCode;
            NextList = nextList;
        }

        /// <summary>
        ///  设备号
        /// </summary>
        public string DeviceCode { get; set; }

        /// <summary>
        ///  下一个设备
        /// </summary>
        public string[] NextList { get; set; }
    }
}
