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 AssistClient.UI.PropExtend;
using static AssistClient.UI.UIAxisDev;

namespace AssistClient.UI
{
    //[TypeConverter(typeof(PropertySorter))] //此属性使类无法序列化和反序列化
    public class SmallAxisDevProp
    {
        public SmallAxisDevProp( string comName) {
            ComName = comName;
        }

        [PropertyOrder(1), Browsable(true), Category("1 设备"), DisplayName("端口号"), Description("端口号"), ReadOnly(true), JsonIgnore]
        public string ComName { get;set; }

        //
        [PropertyOrder(23), Browsable(true), Category("2 位置"), DisplayName("命令脉冲"), Description("命令脉冲") ]
        //public SmallAxCmdPos CmdPos { get; set; }
        public int CmdPos { get; set; }

        [PropertyOrder(11), Browsable(true), Category("3 状态"), DisplayName("3.1 运动中"), Description("运动中"), ReadOnly(true), JsonIgnore]
        public bool Moving { get; set; }
        [PropertyOrder(23), Browsable(true), Category("3 状态"), DisplayName("3.2 最大脉冲"), Description("最大脉冲"), ReadOnly(true), JsonIgnore]
        public int MaxPPU { get; set; } 
       
        [PropertyOrder(24), Browsable(true), Category("3 状态"), DisplayName("3.3 反馈脉冲"), Description("实际/反馈脉冲"), ReadOnly(true), JsonIgnore]
        public int ActualPPU { get; set; }

        [PropertyOrder(25), Browsable(true), Category("4 控制"), DisplayName("4.1 运行前Sleep(ms)"), Description("休息时间")]
        public uint SleepPre { get; set; }
        [PropertyOrder(26), Browsable(true), Category("4 控制"), DisplayName("4.2 运行后Sleep(ms)"), Description("休息时间")]
        public uint SleepLater { get; set; } 
        [PropertyOrder(27), Browsable(true), Category("4 控制"), DisplayName("4.4 禁用工序"), Description("禁用本工序")]
        public bool Disable { 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<SmallAxisDevProp>(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);
                }
            }  
        }
    }
     

}
