using WCS.Core;
using WCS.Entity.Protocol.DataStructure;
using WCS.Entity.Protocol.Protocol.DataStructure;
using WCS.WorkEngineering.Systems;

namespace WCS.WorkEngineering
{
    public class ProtocolProxy : ProtocolProxyBase
    {
        public static DeviceDataPack DataPack { get; set; } = new DeviceDataPack();

        public ProtocolProxy(Device dev, ProtocolInfo info, Type protocolType, World world) : base(dev, info, protocolType, world)
        {
        }

        protected override void DataChanged()
        {
            //if (Device.Code == "RGV1" && Info.DBInfo.No == 520)
            //{
            //    var a = Items;
            //    //var dev=Device.Protocol(ProtocolType)
            //    var b = a;

            //}

            try
            {
                var datas = DataCollectionSysyem.AllDatas;
                if (Device.Code.All(char.IsNumber))
                {
                    if (!datas.ContainsKey(Device.Code)) datas[Device.Code] = new StationData { Code = Device.Code };
                }
                else if (Device.Code.Contains("SRM"))
                {
                    if (!datas.ContainsKey(Device.Code)) datas[Device.Code] = new SRMData { Code = Device.Code };
                }
                //else if (Device.Code.Contains("Truss"))
                //{
                //    if (!datas.ContainsKey(Device.Code)) datas[Device.Code] = new TrussData() { Code = Device.Code };
                //}
                else if (Device.Code.Contains("Robot"))
                {
                    if (!datas.ContainsKey(Device.Code)) datas[Device.Code] = new RobotData() { Code = Device.Code };
                }
                else if (Device.Code.Contains("RGV"))
                {
                    if (!datas.ContainsKey(Device.Code)) datas[Device.Code] = new RGVData { Code = Device.Code };
                }

                if (!datas.TryGetValue(Device.Code, out var data)) return;
                data.Frame = DateTime.Now;
                var p = data.GetType().GetProperties().FirstOrDefault(v => v.PropertyType == ProtocolDataType);
                if (p == null)
                {
                    Console.WriteLine("类型" + data.GetType().Name + "不包含类型为" + ProtocolType.Name + "的属性");
                }
                else
                {
                    p.SetValue(data, DictionaryToEntity(ProtocolDataType, Items));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public object DictionaryToEntity(Type type, Dictionary<string, PlcItem> items)
        {
            var a = Activator.CreateInstance(type);
            foreach (var ty in type.GetProperties())
            {
                foreach (var item in items.Where(item => ty.Name == item.Key))
                {
                    ty.SetValue(a, item.Value.Value);
                    break;
                }
            }

            return a;
        }
    }
}
