using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
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.UIAxisDev;

namespace ProductionControl.UI
{
    //[TypeConverter(typeof(PropertySorter))] //此属性使类无法序列化和反序列化
    public class AxisDevTagProp
    {   
        [PropertyOrder(1), Browsable(true), Category("1 标识"), DisplayName("1.1 引用标识"), Description("引用前面尺寸测量工序动态输出所对应标识数据.")]
        public string SizeTag { get; set; } = "";
        [PropertyOrder(1), Browsable(true), Category("1 标识"), DisplayName("1.2 引用数据索引"), Description("引用前面尺寸测量工序动态输出所对应标识数据中的索引（0-N）.")]
        public int UseIndex { get; set; }
        [PropertyOrder(1), Browsable(true), Category("1 位标"), DisplayName("1.3 运动模式"), Description("运动模式")]
        public AxMoveMode MoveMode { get; set; } = AxMoveMode.绝对位置;
        [PropertyOrder(1), Browsable(true), Category("1 标识"), DisplayName("1.4 对应轴索引"), Description("标识中数据索引对应轴索引(0-X0轴; 1-X1轴; 2-Y轴; 3-Z轴)")]
        public int[] AxisIndexList { get; set; } = new int[2] { 0, 2 };//默认对应0和2号AXIS
        //
        [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(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("非阻塞后续流程运行(True-是；False-否)")]
        //public bool AsynRun { get; set; }
        [PropertyOrder(28), Browsable(true), Category("4 控制"), DisplayName("4.4 禁用工序"), Description("禁用本工序(True-是；False-否)")]
        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<AxisDevTagProp>(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);
                }
            } 
        }
    }
     

}
