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.UI.UIAxis;

namespace ProductionControl.UI
{
    //[TypeConverter(typeof(PropertySorter))] //此属性使类无法序列化和反序列化
    public class AxisProp
    {
        [PropertyOrder(1), Browsable(true), Category("设备"), DisplayName("轴索引"), Description("轴索引")]
        public AxisName AxisIndex { get; set; }
        [PropertyOrder(2), Browsable(true), Category("设备"), DisplayName("类型"), Description("类型")]
        public AxisType DeviceType { get; set; }

        //
        [PropertyOrder(11), Browsable(true), Category("速度"), DisplayName("起始速度"), Description("起始速度")]
        public double VelLow { get; set; }
        [PropertyOrder(12), Browsable(true), Category("速度"), DisplayName("运行速度"), Description("运行速度")]
        public double VelHigh { get; set; }
        [PropertyOrder(13), Browsable(true), Category("速度"), DisplayName("加速度"), Description("加速度")]
        public double Acc { get; set; }
        [PropertyOrder(14), Browsable(true), Category("速度"), DisplayName("减速度"), Description("减速度")]
        public double Dec { get; set; }

        //
        [PropertyOrder(21), Browsable(true), Category("位置"), DisplayName("绝对位置"), Description("绝对:true 相对:false")]
        public bool IsAbsPos { get; set; }
        [PropertyOrder(22), Browsable(true), Category("位置"), DisplayName("运动距离PPU"), Description("移动量")]
        public int Value { get; set; }

        [PropertyOrder(23), Browsable(true), Category("位置"), DisplayName("命令位置"), Description("命令位置"), ReadOnly(true), JsonIgnore]
        public double CmdPos { get; set; }
        [PropertyOrder(24), Browsable(true), Category("位置"), DisplayName("反馈位置"), Description("实际/反馈位置"), ReadOnly(true), JsonIgnore]
        public double ActualPos { get; set; }

        //
        [PropertyOrder(31), Browsable(true), Category("状态"), DisplayName("轴状态"), Description("轴状态"), ReadOnly(true), JsonIgnore]
        public AxisState AxState { get; set; }
        [PropertyOrder(32), Browsable(false), Category("状态"), DisplayName("轴IO状态"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public uint AxIOStatus { get; set; }
        [PropertyOrder(33), Browsable(true), Category("状态"), DisplayName("轴运动状态"), Description("轴运动状态"), ReadOnly(true), JsonIgnore]
        public uint AxMotionState { get; set; }

        [PropertyOrder(35), Browsable(true), Category("状态"), DisplayName("ALM"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public bool ALM { get; set; }
        [PropertyOrder(35), Browsable(true), Category("状态"), DisplayName("EMG"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public bool EMG { get; set; }
        [PropertyOrder(36), Browsable(true), Category("状态"), DisplayName("LMT+"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public bool LMTP { get; set; }
        [PropertyOrder(37), Browsable(true), Category("状态"), DisplayName("LMT-"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public bool LMTN { get; set; }
        [PropertyOrder(38), Browsable(true), Category("状态"), DisplayName("ORG"), Description("轴IO状态"), ReadOnly(true), JsonIgnore] 
        public bool ORG { get; set; }
        [PropertyOrder(39), Browsable(true), Category("状态"), DisplayName("SVON"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public bool SVON { 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<IOCardProp>(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);
                }
            } 
        }
    }
     

}
