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 AxisDevProp
    {
        [PropertyOrder(1), Browsable(true), Category("1 设备"), DisplayName("1.1 设置编号"), Description("设置编号"), ReadOnly(true), JsonIgnore]
        public string DeviceNum { get; set; }
        [PropertyOrder(1), Browsable(true), Category("1 设备"), DisplayName("1.2 轴索引"), Description("轴索引")]
        public AxisName AxisIndex { get; set; }
        //[PropertyOrder(2), Browsable(true), Category("设备"), DisplayName("类型"), Description("类型")]
        //public AxisType DeviceType { get; set; }
        //[PropertyOrder(2), Browsable(true), Category("设备"), DisplayName("回原点模式"), Description("回原点模式"), ReadOnly(true), JsonIgnore]
        //public AxitHomeMode HomeMode { get; set; }
        //
        [PropertyOrder(11), Browsable(true), Category("2 速度"), DisplayName("2.1 起始速度(mm/s)"), Description("起始速度")]
        public double VelLow { get; set; }
        [PropertyOrder(12), Browsable(true), Category("2 速度"), DisplayName("2.2 运行速度(mm/s)"), Description("运行速度")]
        public double VelHigh { get; set; }
        [PropertyOrder(13), Browsable(true), Category("2 速度"), DisplayName("2.3 加速度(mm/s)"), Description("加速度")]
        public double Acc { get; set; }
        [PropertyOrder(14), Browsable(true), Category("2 速度"), DisplayName("2.4 减速度(mm/s)"), Description("减速度")]
        public double Dec { get; set; }

        //
        [PropertyOrder(21), Browsable(true), Category("3 位置"), DisplayName("3.1 运动模式"), Description("运动模式")]
        public AxMoveMode MoveMode { get; set; } = AxMoveMode.MODE2_Rel;
        [PropertyOrder(22), Browsable(true), Category("3 位置"), DisplayName("3.2 运动距离(mm)"), Description("移动量")]
        public double Value { get; set; }

        [PropertyOrder(23), Browsable(true), Category("3 位置"), DisplayName("3.3 命令位置(mm)"), Description("命令位置"), ReadOnly(true), JsonIgnore]
        public double CmdPos { get; set; }
        [PropertyOrder(24), Browsable(true), Category("3 位置"), DisplayName("3.4 反馈位置(mm)"), Description("实际/反馈位置"), ReadOnly(true), JsonIgnore]
        public double ActualPos { 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.3 异步运行"), Description("非阻塞后续流程运行")]
        public bool AsynRun { get; set; }
        [PropertyOrder(28), Browsable(true), Category("4 控制"), DisplayName("4.4 禁用工序"), Description("禁用本工序")]
        public bool Disable { get; set; }

        //[PropertyOrder(27), Browsable(true), Category("模式"), DisplayName("脉冲输出模式"), Description("脉冲输出模式")]
        //public AxisPulseOutMode PulseOutMode { get; set; } = AxisPulseOutMode.O_CW_CCW;
        //
        [PropertyOrder(31), Browsable(true), Category("5 状态"), DisplayName("5.1 轴状态"), Description("轴状态"), ReadOnly(true), JsonIgnore]
        public AxisState AxState { get; set; }
        [PropertyOrder(32), Browsable(false), Category("5 状态"), DisplayName("5.2 轴IO状态"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public uint AxIOStatus { get; set; }
        [PropertyOrder(33), Browsable(true), Category("5 状态"), DisplayName("5.2 轴运动状态"), Description("轴运动状态"), ReadOnly(true), JsonIgnore]
        public uint AxMotionState { get; set; }

        [PropertyOrder(35), Browsable(true), Category("5 状态"), DisplayName("5.3 ALM"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public bool ALM { get; set; }
        [PropertyOrder(35), Browsable(true), Category("5 状态"), DisplayName("5.4 EMG"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public bool EMG { get; set; }
        [PropertyOrder(36), Browsable(true), Category("5 状态"), DisplayName("5.5 LMT+"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public bool LMTP { get; set; }
        [PropertyOrder(37), Browsable(true), Category("5 状态"), DisplayName("5.6 LMT-"), Description("轴IO状态"), ReadOnly(true), JsonIgnore]
        public bool LMTN { get; set; }
        [PropertyOrder(38), Browsable(true), Category("5 状态"), DisplayName("5.7 ORG"), Description("轴IO状态"), ReadOnly(true), JsonIgnore] 
        public bool ORG { get; set; }
        [PropertyOrder(38), Browsable(true), Category("5 状态"), DisplayName("5.8 EZ"), Description("轴EZ状态"), ReadOnly(true), JsonIgnore]
        public bool EZ { get; set; }
        [PropertyOrder(39), Browsable(true), Category("5 状态"), DisplayName("5.9 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<AxisDevProp>(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);
                }
            } 
        }
    }
     

}
