using WCS.Core;
using WCS.Entity.Protocol.SRM;
using WCS.Entity.Protocol.Station;
using WCS.WorkEngineering.Extensions;
using WCS.WorkEngineering.Worlds;

namespace WCS.WorkEngineering.Systems
{
    /// <summary>
    ///  设备信息写入接口
    /// </summary>
    [BelongTo(typeof(MainWorld))]
    public class DeviceWriteSystem : ServiceSystem<DeviceWriteInfo>
    {
        /// <summary>
        /// 所有的站台
        /// </summary>
        private Dictionary<string, Station> Convs;

        private Dictionary<string, SRM> Srms;

        /// <summary>
        ///  构造函数
        /// </summary>
        public DeviceWriteSystem()
        {
            Convs = Device.All.Where(v => v.HasProtocol<IStation523>()).Select(v => new Station(v, this.World)).ToDictionary(v => v.Entity.Code, v => v);
            Srms = Device.All.Where(v => v.HasProtocol<ISRM520>()).Select(v => new SRM(v, this.World)).ToDictionary(v => v.Entity.Code, v => v);
        }

        protected override void Do(DeviceWriteInfo info)
        {
            Type? type = null;
            object? obj = null;
            switch (info.DeviceType)
            {
                case DeviceTypeEnum.SRM:
                    var srm = Srms[info.Code];
                    type = typeof(ISRM520).Assembly.GetTypes().Where(v => v.Name == info.Protocol).First();
                    obj = srm.Entity.Protocol(type, this.World);

                    break;

                case DeviceTypeEnum.DEV:
                    var conv = Convs[info.Code];
                    type = typeof(IStation523).Assembly.GetTypes().Where(v => v.Name == info.Protocol).First();
                    obj = conv.Entity.Protocol(type, this.World);

                    break;

                case DeviceTypeEnum.BCR:
                    break;

                case DeviceTypeEnum.RGV:
                    break;
            }
            var p = type.GetProperty(info.Property);
            if (p.PropertyType.IsEnum)
            {
                var value = Enum.Parse(p.PropertyType, info.Value);
                p.SetValue(obj, value);
            }
            else
            {
                var value = Convert.ChangeType(info.Value, p.PropertyType);
                p.SetValue(obj, value);
            }
        }
    }

    /// <summary>
    /// 设备写入信息
    /// </summary>
    public class DeviceWriteInfo
    {
        /// <summary>
        ///  设备类型
        /// </summary>
        public DeviceTypeEnum DeviceType { get; set; }

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

        /// <summary>
        ///  协议
        /// </summary>
        public string Protocol { get; set; }

        /// <summary>
        /// 字段明
        /// </summary>
        public string Property { get; set; }

        /// <summary>
        ///  值
        /// </summary>
        public string Value { get; set; }
    }

    /// <summary>
    ///  设备类型接口
    /// </summary>
    public enum DeviceTypeEnum
    {
        /// <summary>
        ///  堆垛机
        /// </summary>
        SRM = 1,

        /// <summary>
        ///  输送线
        /// </summary>
        DEV = 2,

        /// <summary>
        ///  扫码器
        /// </summary>
        BCR = 3,

        /// <summary>
        /// RGV小车
        /// </summary>
        RGV = 4,
    }
}
