using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace TZONE.Tools
{
    public partial class frmTools : Form
    {
        public frmTools()
        {
            InitializeComponent();
        }
        #region


        string configfile = "TZONE.SocketService.exe.config";
        System.Diagnostics.Process ota_process, upgrade_process;

        #endregion

        private void btnRead_Click(object sender, EventArgs e)
        {
            GetAppSettingsValue("");
        }

        private string GetAppSettingsValue(string key)
        {
            string xmlPath = string.Format("{0}\\{1}", Application.StartupPath, configfile);
            XDocument root = XDocument.Load(xmlPath);
            var v = root.Elements("configuration").Elements("appSettings").Elements("add").Single(c => c.Attribute("key").Value == key).Attribute("value").Value;
            return v;
        }
        private void SetAppSettingsValue(string key, string value)
        {
            string xmlPath = string.Format("{0}\\{1}", Application.StartupPath, configfile);
            XDocument root = XDocument.Load(xmlPath);
            root.Elements("configuration").Elements("appSettings").Elements("add").Single(c => c.Attribute("key").Value == key).Attribute("value").Value = value;
            root.Save(xmlPath);
        }

        private string GetConnectionStringsValue(string key)
        {
            string xmlPath = string.Format("{0}\\{1}", Application.StartupPath, configfile);
            XDocument root = XDocument.Load(xmlPath);
            var v = root.Elements("configuration").Elements("connectionStrings").Elements("add").Single(c => c.Attribute("name").Value == key).Attribute("connectionString").Value;
            return v;
        }
        private void SetConnectionStringsValue(string key, string value)
        {
            string xmlPath = string.Format("{0}\\{1}", Application.StartupPath, configfile);
            XDocument root = XDocument.Load(xmlPath);
            root.Elements("configuration").Elements("connectionStrings").Elements("add").Single(c => c.Attribute("name").Value == key).Attribute("connectionString").Value = value;
            root.Save(xmlPath);
        }

        private string GetSuperSocketServer(string key)
        {
            string xmlPath = string.Format("{0}\\{1}", Application.StartupPath, configfile);
            XDocument root = XDocument.Load(xmlPath);
            var port = root.Elements("configuration").Elements("superSocket").Elements("servers").Elements("server").First().Attribute(key).Value;
            return port;
        }

        private void SetSuperSocketServer(string key, string value)
        {
            string xmlPath = string.Format("{0}\\{1}", Application.StartupPath, configfile);
            XDocument root = XDocument.Load(xmlPath);
            root.Elements("configuration").Elements("superSocket").Elements("servers").Elements("server").First().Attribute(key).Value = value;
            root.Save(xmlPath);
        }

        private string GetEndpoint(string key)
        {
            string xmlPath = string.Format("{0}\\{1}", Application.StartupPath, configfile);
            XDocument root = XDocument.Load(xmlPath);
            var port = root.Elements("configuration").Elements("system.serviceModel").Elements("services").Elements("service").Elements("endpoint").Single(c => c.Attribute("contract").Value == key).Attribute("address").Value;
            return port;
        }

        private void SetEndpoint(string key, string value)
        {
            string xmlPath = string.Format("{0}\\{1}", Application.StartupPath, configfile);
            XDocument root = XDocument.Load(xmlPath);
            root.Elements("configuration").Elements("system.serviceModel").Elements("services").Elements("service").Elements("endpoint").Single(c => c.Attribute("contract").Value == key).Attribute("address").Value = value;
            root.Save(xmlPath);
        }

        private string GetBaseAddress()
        {
            string xmlPath = string.Format("{0}\\{1}", Application.StartupPath, configfile);
            XDocument root = XDocument.Load(xmlPath);
            var port = root.Elements("configuration").Elements("system.serviceModel").Elements("services").Elements("service").Elements("host").Elements("baseAddresses").Elements("add").First().Attribute("baseAddress").Value;
            return port;
        }
        private void SetBaseAddress(string value)
        {
            string xmlPath = string.Format("{0}\\{1}", Application.StartupPath, configfile);
            XDocument root = XDocument.Load(xmlPath);
            root.Elements("configuration").Elements("system.serviceModel").Elements("services").Elements("service").Elements("host").Elements("baseAddresses").Elements("add").First().Attribute("baseAddress").Value = value;
            root.Save(xmlPath);
        }
        private void tsb_Read_Click(object sender, EventArgs e)
        {
            try
            {
                this.txt_OTAFile.Text = GetAppSettingsValue("ota");
                this.txt_Picture.Text = GetAppSettingsValue("img");
                if (GetAppSettingsValue("isoriginal").Equals("1"))
                    this.chk_Isoriginal.Checked = true;
                else
                    this.chk_Isoriginal.Checked = false;
                if (GetAppSettingsValue("isUPG").Equals("1"))
                    this.chk_IsUPG.Checked = true;
                else
                    this.chk_IsUPG.Checked = false;
                this.txt_ConnectionStrings.Text = GetConnectionStringsValue("DB");

                this.txt_Port.Text = GetSuperSocketServer("port");
                this.txt_MaxConnectionNumber.Text = GetSuperSocketServer("maxConnectionNumber");
                this.txt_ReceiveBufferSize.Text = GetSuperSocketServer("receiveBufferSize");
                this.txt_SendBufferSize.Text = GetSuperSocketServer("sendBufferSize");
                this.txt_ClearIdleSessionInterval.Text = GetSuperSocketServer("clearIdleSessionInterval");
                this.txt_CommandAssembly.Text = GetSuperSocketServer("commandAssembly");
                if (Convert.ToBoolean(GetSuperSocketServer("clearIdleSession")))
                    this.chk_ClearIdleSession.Checked = true;
                else
                    this.chk_ClearIdleSession.Checked = false;

                this.txt_Endpoint.Text = GetEndpoint("TZONE.Contracts.IDown");
                this.txt_BaseAddress.Text = GetBaseAddress();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsb_Save_Click(object sender, EventArgs e)
        {
            try
            {
                SetAppSettingsValue("ota", this.txt_OTAFile.Text);
                SetAppSettingsValue("img", this.txt_Picture.Text);
                if (this.chk_Isoriginal.Checked)
                    SetAppSettingsValue("isoriginal", "1");
                else
                    SetAppSettingsValue("isoriginal", "0");
                if (this.chk_IsUPG.Checked)
                    SetAppSettingsValue("isUPG", "1");
                else
                    SetAppSettingsValue("isUPG", "0");
                SetConnectionStringsValue("DB", this.txt_ConnectionStrings.Text);

                SetSuperSocketServer("port", this.txt_Port.Text);
                SetSuperSocketServer("maxConnectionNumber", this.txt_MaxConnectionNumber.Text);
                SetSuperSocketServer("receiveBufferSize", this.txt_ReceiveBufferSize.Text);
                SetSuperSocketServer("sendBufferSize", this.txt_SendBufferSize.Text);
                SetSuperSocketServer("clearIdleSessionInterval", this.txt_ClearIdleSessionInterval.Text);
                SetSuperSocketServer("commandAssembly", this.txt_CommandAssembly.Text);
                if (this.chk_ClearIdleSession.Checked)
                    SetSuperSocketServer("clearIdleSession", "true");
                else
                    SetSuperSocketServer("clearIdleSession", "false");
                SetEndpoint("TZONE.Contracts.IDown", this.txt_Endpoint.Text);
                SetBaseAddress(this.txt_BaseAddress.Text);
                MessageBox.Show("Saved Successfully!");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_OTAFile_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.txt_OTAFile.Text = path.SelectedPath;
        }

        private void btn_Picture_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.txt_Picture.Text = path.SelectedPath;
        }

        private void tsb_OTA_Click(object sender, EventArgs e)
        {
            if (ota_process == null)
            {
                ota_process = new System.Diagnostics.Process();
                ota_process.StartInfo.FileName = "TZONE.OTA.exe";
                ota_process.Start();
            }
            else
            {
                if (ota_process.HasExited)  //是否正在运行
                    ota_process.Start();
            }
        }

        private void tsb_Upgrade_Click(object sender, EventArgs e)
        {
            if (upgrade_process == null)
            {
                upgrade_process = new System.Diagnostics.Process();
                upgrade_process.StartInfo.FileName = "TZONE.Upgrade.exe";
                upgrade_process.Start();
            }
            else
            {
                if (upgrade_process.HasExited)  //是否正在运行
                    upgrade_process.Start();
            }
        }

    }
}
