using Newtonsoft.Json.Linq;
using ProductionControl.Utils;
using System;
using System.Collections;
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 ProductionControl
{
    public partial class FrmGButtonSetting : Form
    {
        JObject joJson=new JObject();
        public FrmGButtonSetting()
        {
            InitializeComponent();

            ArrayList list = new ArrayList();
            list.Add(new DictionaryEntry("start", "启动按钮"));
            list.Add(new DictionaryEntry("pause", "暂停按钮"));
            list.Add(new DictionaryEntry("reset", "复位按钮"));
            list.Add(new DictionaryEntry("warning", "告警事件"));
            list.Add(new DictionaryEntry("iodefault", "I/O默认输出状态"));
            cobBreakButton.DisplayMember = "Value";
            cobBreakButton.ValueMember = "Key";
            cobBreakButton.DataSource = list;
            cobBreakButton.SelectedIndex = 0;
        }
      
        private void initData()
        {
            string configPath = Application.StartupPath + "\\GButtonConfig.json";
            string lsTmp = File.ReadAllText(configPath);
            if (string.IsNullOrEmpty(lsTmp))
                lsTmp = "{}";
            joJson = JObject.Parse(lsTmp); 
        }
        private void FrmSetParams_Load(object sender, EventArgs e)
        {
            try
            {
                uiIOCardDev0.init();
                initData();
                cobBreakButton_SelectedIndexChanged(null, null);    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }

        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string key = cobBreakButton.SelectedValue.ToString();
                var value = JObject.Parse(this.uiIOCardDev0.getParamsData());
                if (joJson.ContainsKey(key))
                    joJson[key] = value;
                else
                    joJson.Add(key, value);

                if (!joJson.ContainsKey("start")) throw new Exception("请设置启动按钮对应I/O指令！");
                if (!joJson.ContainsKey("pause")) throw new Exception("请设置暂停按钮对应I/O指令！");
                if (!joJson.ContainsKey("reset")) throw new Exception("请设置复位按钮对应I/O指令！");
                if (!joJson.ContainsKey("warning")) throw new Exception("请设置复位按钮对应I/O指令！");
                if (!joJson.ContainsKey("iodefault")) throw new Exception("请设置I/O默认输出状态！");

                string configPath = Application.StartupPath + "\\GButtonConfig.json";  
                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();
        }

        private string curBreakKey = "";
        private void cobBreakButton_SelectedIndexChanged(object sender, EventArgs e)
        {
            var value = JObject.Parse(this.uiIOCardDev0.getParamsData());
            if (curBreakKey != "")
            {
                if (joJson.ContainsKey(curBreakKey))
                    joJson[curBreakKey] = value;
                else
                    joJson.Add(curBreakKey, value);
            }

            // 
            string key = cobBreakButton.SelectedValue.ToString();
            //string key = ((System.Collections.DictionaryEntry)cobBreakButton.SelectedValue).Key.ToString();
            if (joJson.ContainsKey(key))
                this.uiIOCardDev0.setParamsData(joJson[key].ToString());
            curBreakKey = key;
        }
    }
}
