using Newtonsoft.Json.Linq;
using AssistClient.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AssistClient
{
    public partial class FrmPTSetting : Form
    {
        public FrmPTSetting()
        {
            InitializeComponent();
        }
      
        private void initData()
        {
            string configPath = Application.StartupPath + "\\PTConfig.json";
            string lsTmp = File.ReadAllText(configPath);
            JObject joJson = JObject.Parse(lsTmp);
            JArray arrItem; 
            if (joJson.ContainsKey("initPT"))
            {
                arrItem = joJson.Value<JArray>("initPT"); 
                this.numAxis0_InitPT.Value= (decimal)arrItem[0];
                this.numAxis1_InitPT.Value = (decimal)arrItem[1];
                this.numAxis2_InitPT.Value = (decimal)arrItem[2];
                this.numAxis3_InitPT.Value = (decimal)arrItem[3];
            }
            if (joJson.ContainsKey("upPT"))
            {
                arrItem = joJson.Value<JArray>("upPT");
                this.numAxis0_UpPT.Value = (decimal)arrItem[0];
                this.numAxis1_UpPT.Value = (decimal)arrItem[1];
                this.numAxis2_UpPT.Value = (decimal)arrItem[2];
                this.numAxis3_UpPT.Value = (decimal)arrItem[3];
            }
            if (joJson.ContainsKey("downPT"))
            {
                arrItem = joJson.Value<JArray>("downPT");
                this.numAxis0_DownPT.Value = (decimal)arrItem[0];
                this.numAxis1_DownPT.Value = (decimal)arrItem[1];
                this.numAxis2_DownPT.Value = (decimal)arrItem[2];
                this.numAxis3_DownPT.Value = (decimal)arrItem[3];
            }
        }
        private void FrmSetParams_Load(object sender, EventArgs e)
        {
            try
            {
                initData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }

        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string configPath = Application.StartupPath + "\\PTConfig.json";

                JObject joJson = new JObject();
                decimal axis0Value, axis1Value, axis2Value, axis3Value;
                axis0Value = (decimal)this.numAxis0_InitPT.Value;
                axis1Value = (decimal)this.numAxis1_InitPT.Value;
                axis2Value = (decimal)this.numAxis2_InitPT.Value;
                axis3Value = (decimal)this.numAxis3_InitPT.Value;
                joJson.Add("initPT", new JArray(axis0Value, axis1Value, axis2Value, axis3Value));

                axis0Value = (decimal)this.numAxis0_UpPT.Value;
                axis1Value = (decimal)this.numAxis1_UpPT.Value;
                axis2Value = (decimal)this.numAxis2_UpPT.Value;
                axis3Value = (decimal)this.numAxis3_UpPT.Value;
                joJson.Add("upPT", new JArray(axis0Value, axis1Value, axis2Value, axis3Value));

                axis0Value = (decimal)this.numAxis0_DownPT.Value;
                axis1Value = (decimal)this.numAxis1_DownPT.Value;
                axis2Value = (decimal)this.numAxis2_DownPT.Value;
                axis3Value = (decimal)this.numAxis3_DownPT.Value;
                joJson.Add("downPT", new JArray(axis0Value, axis1Value, axis2Value, axis3Value));

                File.WriteAllText(configPath, joJson.ToString());

                //
                this.Hide();
                MessageBox.Show("保存成功，生效需重启程序！");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
         
    }
}
