using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Advantech.Motion;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using ProductionControl.UI.PropExtend;
using static ProductionControl.Device.Scanner;
using static ProductionControl.UI.UIAxis;

namespace ProductionControl.UI
{
    //[TypeConverter(typeof(PropertySorter))] //此属性使类无法序列化和反序列化
    public class ScannerProp
    {
        public ScannerProp(ScannerType devType) {
            DeviceType=devType;
        }

        [PropertyOrder(1), Browsable(true), Category("设备"), DisplayName("设备类型"), Description("设备类型"), ReadOnly(true)]
        public ScannerType DeviceType { get;set; }
        [PropertyOrder(1), Browsable(true), Category("图像"), DisplayName("图像名称"), Description("图像名称"), ReadOnly(true), JsonIgnore]
        public string[] ImageList { get; set; }
        //
        [PropertyOrder(11), Browsable(true), Category("参数"), DisplayName("曝光"), Description("曝光")]
        public float ExposureTime { get; set; }
        [PropertyOrder(12), Browsable(true), Category("参数"), DisplayName("增益"), Description("增益")]
        public float Gain { get; set; }
        [PropertyOrder(13), Browsable(true), Category("参数"), DisplayName("帧率"), Description("帧率")]
        public float ResultingFrameRate { get; set; }


        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string serialize()
        {  
            return JsonConvert.SerializeObject(this);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public void deserialize(string json)
        {
            var o=  JsonConvert.DeserializeObject<ScannerProp>(json);
            Type type = o.GetType();
            System.Reflection.PropertyInfo[] properties = type.GetProperties(); 
            foreach (System.Reflection.PropertyInfo property in properties)
            {
                string name = property.Name;
                if (!type.GetProperty(name).IsDefined(typeof(JsonIgnoreAttribute), true))
                {
                    var value = property.GetValue(o);
                    this.GetType().GetProperty(name).SetValue(this, value);
                }
            }  
        }
    }
     

}
