using Advantech.Motion;
using Newtonsoft.Json;
using ProductionControl.Device;
using ProductionControl.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq; 

namespace ProductionControl.UI
{
    public partial class UIForLib : UserControl
    {
        SynchronizationContext SyncContext = null;
          
        public Action<string> GetParamsEvent;
        //
        private ForProp prop = new ForProp(); 
        public UIForLib()
        {
            InitializeComponent();
            propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged; 

            //获取UI线程同步上下文
            SyncContext = SynchronizationContext.Current;
            init();
        }
        public void init()
        {
            this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
            txtLog.Text = "";
            this.propertyGrid1.SelectedObject = prop; 
        }
        public string getParamsData()
        {
            return prop.serialize();
        }
        public void setParamsData(string json)
        {
            if (json == "") return;
            prop.deserialize(json);
            this.propertyGrid1.Refresh();
        }
     
        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            //其中包含了两个重要的属性：OldValue和ChangeItem。
            //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。 
            //而通过ChangeItem的Label，则可以查看到当前在属性编辑输入框中显示的值。  

            string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
            string propertyName = e.ChangedItem.PropertyDescriptor.Name;
            var oldValue = e.OldValue;
            var newValue = e.ChangedItem.Value;
            switch (propertyName)
            {
                case "GotoStepIndex":
                    if ((int)newValue < 1)
                    {
                        prop.GotoStepIndex = 1;
                        this.refreshUI();
                    } 
                    break;
                case "LimitNum":
                    if ((int)newValue < 1)
                    {
                        prop.LimitNum = 1;
                        this.refreshUI();
                    }
                    break;
            }
        }
        private void refreshUI()
        {
            SyncContext.Post(m =>
            {
                var result = m as string;
                propertyGrid1.Refresh();
                //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
                //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
            }, "异步操作完成结果");


        }
        private void tbtnSave_Click(object sender, EventArgs e)
        {
            GetParamsEvent?.Invoke(prop.serialize());
        }

        private void tbtnExport_Click(object sender, EventArgs e)
        {
            prop.UniqueId = DateTime.Now.Ticks;
            string filePath = FileUtil.saveAsFile($"For配置.json", "JSON|*.json");
            if (filePath != "")
            {
                string jsonText = prop.serialize();
                File.WriteAllText(filePath, jsonText);
                MessageBox.Show("保存成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void tbtnImport_Click(object sender, EventArgs e)
        {
            string filePath = FileUtil.selectFile("JSON|*.json");
            if (filePath != "")
            {
                string jsonText = File.ReadAllText(filePath);
                prop.deserialize(jsonText);
                this.propertyGrid1.Refresh();
                //this.propertyGrid1.SelectedObject= prop;
                //MessageBox.Show("加载成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
         
    }
}
