using Microsoft.Win32;
using System;
using System.Windows.Forms;

namespace C_Windows_1
{
    public partial class MESConfig : Form
    {
        public MESConfig()
        {
            InitializeComponent();
        }

        private void MESConfig_Load(object sender, EventArgs e)
        {
            ReadINI();
            Read();
        }

        private void Config_Click(object sender, EventArgs e)
        {
            Form1.f1.appid.Text = appid.Text;
            Form1.f1.appkey.Text = appkey.Text;
            Form1.f1.method.Text = method.Text;
            Form1.f1.Dept.Text = Dept.Text;
            Form1.f1.Type.Text = Type.Text;
            Form1.f1.Machinemodel.Text = Machinemodel.Text;
            Form1.f1.MESStation.Text = MESStation.Text;
            Form1.f1.URL.Text = URL.Text;

            Set();
            this.Close();
        }




        //写入端口参数
        private void Set()
        {

            RegistryKey regName;

            regName = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set", true);

            if (regName is null)
            {
                regName = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set");
            }

            regName.SetValue("appid", appid.Text);
            regName.SetValue("appkey", appkey.Text);
            regName.SetValue("method", method.Text);
            regName.SetValue("dept", Dept.Text);
            regName.SetValue("type", Type.Text);
            regName.SetValue("model", Machinemodel.Text);
            regName.SetValue("teststation", MESStation.Text);
            regName.SetValue("url", URL.Text);

            regName.Close();
        }


        //读出站号、条码长度、通道数等参数
        private void Read()
        {

            RegistryKey regName;

            regName = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set", true);

            if (regName is null)
            {
                regName = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set");
            }

            regName.OpenSubKey("User");
            if (regName.GetValue("url") is null)
            {
                URL.Text = "https://xms.be.xiaomi.com/xmsapi";
            }
            else
            {
                URL.Text = regName.GetValue("url").ToString();

            }


            if (regName.GetValue("appid") is null)
            {
                appid.Text = "xm_1004";
            }
            else
            {
                appid.Text = regName.GetValue("appid").ToString();

            }
            if (regName.GetValue("appkey") is null)
            {
                appkey.Text = "1a3015121bb93b05de89128de42f98ca";
            }
            else
            {
                appkey.Text = regName.GetValue("appkey").ToString();

            }
            if (regName.GetValue("method") is null)
            {
                method.Text = "outrepair.importWRTresult";
            }
            else
            {
                method.Text = regName.GetValue("method").ToString();

            }
            if (regName.GetValue("dept") is null)
            {
                Dept.Text = "";
            }
            else
            {
                Dept.Text = regName.GetValue("dept").ToString();

            }
            if (regName.GetValue("type") is null)
            {
                Type.Text = "";
            }
            else
            {
                Type.Text = regName.GetValue("type").ToString();

            }
            if (regName.GetValue("model") is null)
            {
                Machinemodel.Text = "";
            }
            else
            {
                Machinemodel.Text = regName.GetValue("model").ToString();

            }
            if (regName.GetValue("teststation") is null)
            {
                MESStation.Text = "";
            }
            else
            {
                MESStation.Text = regName.GetValue("teststation").ToString();
            }

            regName.Close();

        }


        string dialog;
        //从ini读出测试物料类型、机型model参数
        private void ReadINI()
        {
            dialog = System.AppDomain.CurrentDomain.BaseDirectory + "MesParas.ini";
            ConfigINI mes = new ConfigINI(dialog);
            if (!mes.ExistINIFile())
            {
                mes.IniWriteValue("Type", "type1", "PHONE");
                mes.IniWriteValue("Type", "type2", "SA");
                mes.IniWriteValue("Type", "type3", "FC");
                mes.IniWriteValue("Type", "type4", "BC");
                mes.IniWriteValue("Machinemodel", "model1", "MI 11 Ultra");
                mes.IniWriteValue("Machinemodel", "model2", "MI 11 Pro");
                mes.IniWriteValue("Machinemodel", "model3", "Xiaomi 12S Ultra");
                mes.IniWriteValue("Machinemodel", "model4", "Test");
                mes.IniWriteValue("Machinemodel", "model5", "Test");
                mes.IniWriteValue("Machinemodel", "model6", "Test");
                mes.IniWriteValue("Machinemodel", "model7", "Test");
                mes.IniWriteValue("Machinemodel", "model8", "Test");
                mes.IniWriteValue("Machinemodel", "model9", "Test");
                mes.IniWriteValue("Machinemodel", "model10", "Test");

                mes.IniWriteValue("Machinemodel", "model11", "Test");
                mes.IniWriteValue("Machinemodel", "model12", "Test");
                mes.IniWriteValue("Machinemodel", "model13", "Test");
                mes.IniWriteValue("Machinemodel", "model14", "Test");
                mes.IniWriteValue("Machinemodel", "model15", "Test");
                mes.IniWriteValue("Machinemodel", "model16", "Test");
                mes.IniWriteValue("Machinemodel", "model17", "Test");
                mes.IniWriteValue("Machinemodel", "model18", "Test");
                mes.IniWriteValue("Machinemodel", "model19", "Test");
                mes.IniWriteValue("Machinemodel", "model20", "Test");

                mes.IniWriteValue("Machinemodel", "model21", "Test");
                mes.IniWriteValue("Machinemodel", "model22", "Test");
                mes.IniWriteValue("Machinemodel", "model23", "Test");
                mes.IniWriteValue("Machinemodel", "model24", "Test");
                mes.IniWriteValue("Machinemodel", "model25", "Test");
                mes.IniWriteValue("Machinemodel", "model26", "Test");
                mes.IniWriteValue("Machinemodel", "model27", "Test");
                mes.IniWriteValue("Machinemodel", "model28", "Test");
                mes.IniWriteValue("Machinemodel", "model29", "Test");
                mes.IniWriteValue("Machinemodel", "model30", "Test");
            }
            Type.Items.Add(mes.IniReadValue("Type", "type1"));
            Type.Items.Add(mes.IniReadValue("Type", "type2"));
            Type.Items.Add(mes.IniReadValue("Type", "type3"));
            Type.Items.Add(mes.IniReadValue("Type", "type4"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model1"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model2"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model3"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model4"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model5"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model6"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model7"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model8"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model9"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model10"));

            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model11"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model12"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model13"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model14"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model15"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model16"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model17"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model18"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model19"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model20"));


            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model21"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model22"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model23"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model24"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model25"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model26"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model27"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model28"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model29"));
            Machinemodel.Items.Add(mes.IniReadValue("Machinemodel", "model30"));
        }

    }
}
