using CCWin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
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 MOKA_Factory_Tools
{
    public partial class SanhuaMES : Skin_Color
    {
        public SanhuaMES()
        {
            InitializeComponent();
        }

        private void SanhuaMES_Load(object sender, EventArgs e)
        {
            Text_MES_URL.Text = SanhuaMethod.sanhuaLoginInfo.url;
            Text_StaffCode.Text = SanhuaMethod.sanhuaLoginInfo.staffCode;
            Text_ResuorceCode.Text = SanhuaMethod.sanhuaLoginInfo.resourceCode;
            Text_MES_Password.Text = SanhuaMethod.sanhuaLoginInfo.password;
            Text_JigEdtion.Text = SanhuaMethod.sanhuaLoginInfo.jigEdtion;
        }

        private void Btn_MES_Login_Click(object sender, EventArgs e)
        {
            SanhuaMethod.sanhuaLoginInfo.url = Text_MES_URL.Text;
            SanhuaMethod.sanhuaLoginInfo.staffCode = Text_StaffCode.Text;
            SanhuaMethod.sanhuaLoginInfo.resourceCode = Text_ResuorceCode.Text;
            SanhuaMethod.sanhuaLoginInfo.password = Text_MES_Password.Text;
            SanhuaMethod.sanhuaLoginInfo.jigEdtion = Text_JigEdtion.Text;

            if ( !SanhuaMethod.sanhuaLoginInfo.url.EndsWith("/") )
            {
                SanhuaMethod.sanhuaLoginInfo.url += "/";
            }

            SaveLoginInfo();

            string message = string.Empty;
            if ( SanhuaMethod.CheckLogin(out message) )
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(string.Format("登录失败:{0}", message));
                DialogResult = DialogResult.Cancel;
            }
        }

        private void SaveLoginInfo()
        {
            JObject jObject;
            using (StreamReader file = File.OpenText(LocalPath.localpath + "\\Config.json"))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jObject = (JObject)JToken.ReadFrom(reader);
                    reader.Close();
                    if (jObject["SanhuaLoginInfo"] == null)
                    {
                        JObject item = new JObject();
                        item.Add("url", SanhuaMethod.sanhuaLoginInfo.url);
                        item.Add("staffCode", SanhuaMethod.sanhuaLoginInfo.staffCode);
                        item.Add("resourceCode", SanhuaMethod.sanhuaLoginInfo.resourceCode);
                        item.Add("password", SanhuaMethod.sanhuaLoginInfo.password);
                        item.Add("jigEdtion", SanhuaMethod.sanhuaLoginInfo.jigEdtion);

                        jObject.Add("SanhuaLoginInfo", item);
                    }
                    else
                    {
                        jObject["SanhuaLoginInfo"]["url"] = SanhuaMethod.sanhuaLoginInfo.url;
                        jObject["SanhuaLoginInfo"]["staffCode"] = SanhuaMethod.sanhuaLoginInfo.staffCode;
                        jObject["SanhuaLoginInfo"]["resourceCode"] = SanhuaMethod.sanhuaLoginInfo.resourceCode;
                        jObject["SanhuaLoginInfo"]["password"] = SanhuaMethod.sanhuaLoginInfo.password;
                        jObject["SanhuaLoginInfo"]["jigEdtion"] = SanhuaMethod.sanhuaLoginInfo.jigEdtion;
                    }
                }
            }
            using (StreamWriter writer = new StreamWriter(LocalPath.localpath + "\\Config.json"))
            {
                writer.Write(jObject.ToString());
            }
        }
    }
}
