using PlcSiemens.Core.Extension;
using ServiceCenter;
using WCS.Core;
using WCS.Entity.Protocol.BCR;
using WCS.Entity.Protocol.SRM;
using WCS.Entity.Protocol.Station;
using WCS.WorkEngineering.Extensions;
using DeviceFlags = WCS.WorkEngineering.Extensions.DeviceFlags;

namespace WCS.WorkEngineering
{
    /// <summary>
    /// 业务工程配置信息
    /// </summary>
    public static class WorkStart
    {
        /// <summary>
        ///  初始化 设备信息
        /// </summary>

        public static void InitializeDeviceInfo()
        {
            #region 设置设备IP集合

            ServiceHub.DeviceIPList = new List<string>() { };

            #endregion 设置设备IP集合

            #region 初始化输送机相关信息

            #region 基本信息

            List<StationSegmentInfo> segmentInfo = new List<StationSegmentInfo>
            {
                new StationSegmentInfo(1011, 1026, "10.30.36.51")
            };

            foreach (var item in segmentInfo)
            {
                for (int i = item.Start; i <= item.End; i++)
                {
                    var conv = new Device(i.ToString());
                    conv.AddFlag(DeviceFlags.输送机);
                    conv.AddProtocol<IStation520>((i - item.Start) * 14, 520, item.IP);
                    conv.AddProtocol<IStation521>((i - item.Start) * 16, 521, item.IP);
                    conv.AddProtocol<IStation523>((i - item.Start) * 12, 523, item.IP);
                }
            }

            #endregion 基本信息

            #region 扫码器

            List<BcrInfo> bcrInfo = new List<BcrInfo>
            {
                new BcrInfo(new int[] { 1011, 1013, 1015 }, "10.30.36.51")
            };

            foreach (var item in bcrInfo)
            {
                for (int i = 0; i < item.DeviceNo.Length; i++)
                {
                    var conv = new Device("BCR" + item.DeviceNo[i]);
                    conv.AddFlag(DeviceFlags.扫码);
                    int pos = i * 20;
                    conv.AddProtocol<IBCR81>(pos, 81, item.IP);
                }
            }

            #endregion 扫码器

            #region 外检信息

            List<ShapeInfo> shapeInfo = new List<ShapeInfo>
            {
                new ShapeInfo(new int[] { 1011, 1013, 1015,1021,1023, 1025 }, "10.30.36.51")
            };

            foreach (var item in shapeInfo)
            {
                for (int i = 0; i < item.DeviceNo.Length; i++)
                {
                    var conv = Device.All.FirstOrDefault(x => x.Code == item.DeviceNo[i].ToString());
                    conv.AddFlag(DeviceFlags.外检);
                    int pos = i * 14;
                    conv.AddProtocol<IStation91>(pos, 91, item.IP);
                }
            }

            #endregion 外检信息

            #endregion 初始化输送机相关信息

            #region 初始化堆垛机相关信息

            for (int i = 1; i <= 3; i++)
            {
                var srm = new Device($"SRM{i}");
                srm.AddFlag(DeviceFlags.堆垛机);

                //三台堆垛机IP主机位分别是 21 31 41
                srm.AddProtocol<ISRM520>(0, 520, $"10.30.36.{(i * 10) + 10 + 1}");
                srm.AddProtocol<ISRM521>(0, 521, $"10.30.36.{(i * 10) + 10 + 1}");
                srm.AddProtocol<ISRM537>(0, 537, $"10.30.36.{(i * 10) + 10 + 1}");

                //增加巷道
                var tunnel = new Device($"TY{i}");
                tunnel.AddFlag(DeviceFlags.巷道);
            }

            #endregion 初始化堆垛机相关信息

            #region 配置路径信息

            List<RouteInfo> routeInfos = new List<RouteInfo>
            {
                new RouteInfo("SRM1", new string[] { "TY1" }),
                new RouteInfo("SRM2", new string[] { "TY2" }),
                new RouteInfo("SRM3", new string[] { "TY3" }),
                new RouteInfo("TY1", new string[] { "SRM1" }),
                new RouteInfo("TY2", new string[] { "SRM2" }),
                new RouteInfo("TY3", new string[] { "SRM3" }),
                new RouteInfo("TY1", new string[] { "1011", "1021" }),
                new RouteInfo("TY2", new string[] { "1013", "1023" }),
                new RouteInfo("TY3", new string[] { "1015", "1025" }),
                new RouteInfo("1012", new string[] { "TY1" }),
                new RouteInfo("1022", new string[] { "TY1" }),
                new RouteInfo("1014", new string[] { "TY2" }),
                new RouteInfo("1024", new string[] { "TY2" }),
                new RouteInfo("1016", new string[] { "TY3" }),
                new RouteInfo("1026", new string[] { "TY3" })
            };

            foreach (var item in routeInfos)
            {
                var device = Device.All.FirstOrDefault(x => x.Code == item.DeviceCode);
                device.AddTarget(item.NextList);

                foreach (var next in item.NextList)
                {
                    var dev = Device.All.FirstOrDefault(x => x.Code == next);
                    dev.Parent = device;
                }
            }

            #endregion 配置路径信息

            #region 标签配置

            Dictionary<DeviceFlags, List<string>> devices = new Dictionary<DeviceFlags, List<string>>
            {
                { DeviceFlags.巷道口, new List<string>() { "1011", "1012", "1013", "1014", "1015", "1016", "1021", "1022", "1023", "1024", "1025", "1026" } },
                { DeviceFlags.入库, new List<string>() { "1011", "1013", "1015", "1021", "1023", "1025" } },
                { DeviceFlags.出库, new List<string>() { "1012", "1014", "1016", "1022", "1024", "1026" } },
                { DeviceFlags.扫码, new List<string>() { "1011", "1013", "1015" } },
                { DeviceFlags.一楼出库口, new List<string>() { "1012", "1014", "1016" } },
                { DeviceFlags.二楼出库口, new List<string>() { "1022", "1024", "1026" } },
                { DeviceFlags.一楼入库口, new List<string>() { "1011", "1013", "1015" } },
                { DeviceFlags.二楼入库口, new List<string>() { "1021", "1023", "1025" } },
                { DeviceFlags.称重, new List<string>() { "1011", "1013", "1015", "1025" } }
            };

            devices.ForEach(item =>
            {
                item.Value.ForEach(code =>
                {
                    var device = Device.All.FirstOrDefault(v => v.Code == code);
                    device.AddFlag(item.Key);
                });
            });

            #endregion 标签配置

          
        }
    }
}
