using LJLib.Tools.DEncrypt;
using LJLib.Tools.File;
using System;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using LJLib.InstallHelper;
using JLHHJSvr;

namespace JLHHJSvr
{
    public partial class Form1 : Form
    {
        private string servername = "JLHHJSvr";
        private string displayname = "金龙恒核价后台服务";
        private string dscrp = "金龙恒核价后台服务";

        public Form1()
        {
#if DEBUG
            servername += "_DEBUG";
            displayname += "(DEBUG)";
            dscrp += "(DEBUG)";
#endif

            InitializeComponent();

            button4.Text = LJInstallHelper.Exitst(servername) ? "卸载服务" : "安装服务";

            var xmlconfig = new XmlConfig();
            var constr = xmlconfig.GetXmlFileValue(configFile, string.Empty, "ConnectionString", string.Empty);
            if (!string.IsNullOrEmpty(constr))
            {
                constr = DESEncrypt.Decrypt(constr, "4A61A8B51C3E42BCAE991F6C913A6A33");
                var builder = new SqlConnectionStringBuilder(constr);
                textBox1.Text = builder.DataSource;
                textBox2.Text = builder.InitialCatalog;
                textBox3.Text = builder.UserID;
                textBox4.Text = builder.Password;
            }

            textBox5.Text = xmlconfig.GetXmlFileValue(configFile, string.Empty, "port", "9075");
            textBox6.Text = xmlconfig.GetXmlFileValue(configFile, string.Empty, "httpport", "");
            textBox7.Text = xmlconfig.GetXmlFileValue(configFile, string.Empty, "L1Server", "");
            textBox10.Text = xmlconfig.GetXmlFileValue(configFile, string.Empty, "L1API", "");
            textBox8.Text = xmlconfig.GetXmlFileValue(configFile, string.Empty, "L1Usercode", "");
            textBox9.Text = DESEncrypt.Decrypt(xmlconfig.GetXmlFileValue(configFile, string.Empty, "L1Password", ""), "4A61A8B51C3E42BCAE991F6C913A6A33");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var builder = new SqlConnectionStringBuilder();
                builder.DataSource = textBox1.Text;
                builder.InitialCatalog = textBox2.Text;
                builder.UserID = textBox3.Text;
                builder.Password = textBox4.Text;
                using (var con = new SqlConnection(builder.ConnectionString))
                {
                    con.Open();
                }
                MessageBox.Show("连接成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private static string configFile
        {
            get { return GlobalVar.XmlFile; }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                var builder = new SqlConnectionStringBuilder();
                builder.DataSource = textBox1.Text;
                builder.InitialCatalog = textBox2.Text;
                builder.UserID = textBox3.Text;
                builder.Password = textBox4.Text;

                var xmlconfig = new XmlConfig();
                xmlconfig.SetXmlFileValue(configFile, string.Empty, "ConnectionString", DESEncrypt.Encrypt(builder.ConnectionString, "4A61A8B51C3E42BCAE991F6C913A6A33"));

                xmlconfig.SetXmlFileValue(configFile, string.Empty, "port", textBox5.Text);
                xmlconfig.SetXmlFileValue(configFile, string.Empty, "httpport", textBox6.Text);
                xmlconfig.SetXmlFileValue(configFile, string.Empty, "L1Server", textBox7.Text);
                xmlconfig.SetXmlFileValue(configFile, string.Empty, "L1API", textBox10.Text);
                xmlconfig.SetXmlFileValue(configFile, string.Empty, "L1Usercode", textBox8.Text);
                xmlconfig.SetXmlFileValue(configFile, string.Empty, "L1Password", DESEncrypt.Encrypt(textBox9.Text, "4A61A8B51C3E42BCAE991F6C913A6A33"));

                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (button4.Text == "安装服务")
            {
                MessageBox.Show(LJInstallHelper.Install(servername, displayname, dscrp, string.Empty));
            }
            else
            {
                MessageBox.Show(LJInstallHelper.Uninstall(servername));
            }
            button4.Text = LJInstallHelper.Exitst(servername) ? "卸载服务" : "安装服务";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var form = new Form2();
            form.ShowDialog();
        }
    }
}
