using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace LiveUpdate
{
    public partial class frmUpdateFile : Form
    {
        ryCommon.clsStorage MyXml = new ryCommon.clsStorage();
        string xmlPath;
        public frmUpdateFile()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public double ConvertDouble(string Str, double defValue)
        {
            try
            {
                return Convert.ToDouble(Str);
            }
            catch
            {
                return defValue;
            }
        }
        public DateTime ConvertDateTime(string Str, DateTime defValue)
        {
            try
            {
                return Convert.ToDateTime(Str);
            }
            catch
            {
                return defValue;
            }
        }
        private void LoadDb(string filePath)
        {
            Text = "软件更新信息设置(" + System.IO.Path.GetFileName(filePath) + ")";
            int iResult = MyXml.LoadFromFile(filePath);
            if (iResult == 0) { MyXml.LoadFromXMLText("<root></root>"); }
            //软件版本
            MyXml.SelectNode2("id", "SoftVer");
            if (IsNumeric(MyXml.GetAttrValue("Value", "0")) == true)
            {
                cmbNewVer.Text = MyXml.GetAttrValue("Value", "0");
            }

            //软件版本
            MyXml.SelectNode2("id", "ReVer");
            if (IsNumeric(MyXml.GetAttrValue("Value", "0")) == true)
            {
                cmbNewReVer.Text = MyXml.GetAttrValue("Value", "0");
            }
            //数据库版本
            MyXml.SelectNode2("id", "DataVer");
            cmbNewDbVer.Text = ConvertDouble(MyXml.GetAttrValue("Value", "0"), 0).ToString();
            //软件更新地址
            MyXml.SelectNode2("id", "FileUpdateUrl");
            cmbProcUpdateUrl.Text = MyXml.GetAttrValue("Value", "");
            //数据库更新地址
            MyXml.SelectNode2("id", "DbUpdateUrl");
            cmbDbUpdateUrl.Text = MyXml.GetAttrValue("Value", "");
            //软件日期
            MyXml.SelectNode2("id", "SoftDate");
            dtNewProcDate.Value = ConvertDateTime(MyXml.GetAttrValue("Value", DateTime.Now.ToString()), DateTime.Now);
            //数据库日期
            MyXml.SelectNode2("id", "DataDate");
            dtNewDataDate.Value = ConvertDateTime(MyXml.GetAttrValue("Value", DateTime.Now.ToString()), DateTime.Now);
            //软件更新描述
            MyXml.SelectNode2("id", "SoftUpdateDes");
            txtUpdateDes.Text = MyXml.GetAttrValue("Value", "");
            //数据库更新描述
            MyXml.SelectNode2("id", "DataUpdateDes");
            txtDbUpdateDes.Text = MyXml.GetAttrValue("Value", "");

            MyXml.SelectNode2("id", "SettingXML");
            ryCommon.clsStorage tStor = new ryCommon.clsStorage(MyXml.GetAttrValue("Value", ""));
            //升级前要结束的进程
            tStor.SelectNode("id", "KillProcList");
            txtKillProcList.Text = tStor.GetAttrValue("Value", "");
            //要启动的主程序
            tStor.SelectNode("id", "MainProcName");
            txtMainProcName.Text = tStor.GetAttrValue("Value", "");
            LoadOem();
        }
        private void frmUpdateFile_Load(object sender, EventArgs e)
        {
            DirectoryInfo directory = new DirectoryInfo(Application.StartupPath);
            string  FileName="";
            comboBox1.Items.Clear();
            foreach (FileInfo info in directory.GetFiles("*.xml"))
            {
                comboBox1.Items.Add(info.Name);
            }
            if (comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedIndex = 0;
                xmlPath = Application.StartupPath + "\\" + comboBox1.Text; FileName = comboBox1.Text;
            }
            else
            {
                xmlPath = Application.StartupPath + "\\Update.xml"; FileName = "Update.xml";
            }
            LoadDb(xmlPath);

        }
        private bool IsNumeric(string Str)
        {
            try
            {
                Convert.ToDouble(Str);
                return true;
            }
            catch
            {
                return false;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (IsNumeric(cmbNewVer.Text) == false)
            {
                MessageBox.Show("【软件版本】必须为数字。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (IsNumeric(cmbNewReVer.Text) == false)
            {
                MessageBox.Show("【软件修正版本】必须为数字。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (IsNumeric(cmbNewDbVer.Text) == false)
            {
                MessageBox.Show("【数据库版本】必须为数字。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (cmbProcUpdateUrl.Text== "")
            {
                MessageBox.Show("【软件更新地址】不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (cmbDbUpdateUrl.Text == "")
            {
                MessageBox.Show("【数据库更新地址】不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            MyXml.LoadFromXMLText("<root></root>");
            MyXml.AddNode2("id", "SoftVer");
            MyXml.SetAttrValue("Value", cmbNewVer.Text);
            MyXml.AddNode2("id", "ReVer");
            MyXml.SetAttrValue("Value", cmbNewReVer.Text);
            MyXml.AddNode2("id", "DataVer");
            MyXml.SetAttrValue("Value", cmbNewDbVer.Text);
            MyXml.AddNode2("id", "FileUpdateUrl");
            MyXml.SetAttrValue("Value", cmbProcUpdateUrl.Text);
            MyXml.AddNode2("id", "DbUpdateUrl");
            MyXml.SetAttrValue("Value", cmbDbUpdateUrl.Text);
            MyXml.AddNode2("id", "SoftDate");
            MyXml.SetAttrValue("Value", dtNewProcDate.Value.Date.ToString());
            MyXml.AddNode2("id", "DataDate");
            MyXml.SetAttrValue("Value", dtNewDataDate.Value.Date.ToString());
            MyXml.AddNode2("id", "SoftUpdateDes");
            MyXml.SetAttrValue("Value", txtUpdateDes.Text);
            MyXml.AddNode2("id", "DataUpdateDes");
            MyXml.SetAttrValue("Value", txtDbUpdateDes.Text);
            ryCommon.clsStorage tStor = new ryCommon.clsStorage();
            tStor.AddNode2("id", "KillProcList");
            tStor.SetAttrValue("Value", txtKillProcList.Text);
            tStor.AddNode2("id", "MainProcName");
            tStor.SetAttrValue("Value", txtMainProcName.Text);
            MyXml.AddNode2("id", "SettingXML");
            MyXml.SetAttrValue("Value", tStor.GetXMLText());
            MyXml.SaveToFile(xmlPath);
            MessageBox.Show("保存信息成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void frmUpdateFile_Resize(object sender, EventArgs e)
        {
            tabControl1.Refresh();
        }
        private void LoadOem()
        {
            if (System.IO.File.Exists(Application.StartupPath + "\\otmy.dat"))
            {
                string fileContent = System.IO.File.ReadAllText(Application.StartupPath + "\\otmy.dat");
                fileContent = ryCommon.clsAES.Decode(fileContent, "otmy1234567");
                if (fileContent.IndexOf("otmy") == 0)//说明是正确的oem文件
                {
                    ryCommon.clsStorage xStor = new ryCommon.clsStorage(fileContent.Substring(4));
                    if (xStor.SelectNode("id", "Info") == 1)
                    {
                        txtVerName.Text = xStor.GetAttrValue("verName", "");
                        txtStatus.Text = xStor.GetAttrValue("StatusOem", "欢迎访问睿元网络官方网站");
                        txtStatusUrl.Text = xStor.GetAttrValue("StatusOemUrl", "http://www.itrycn.com");
                        txtStatusToolTip.Text = xStor.GetAttrValue("StatusOemToolTip", "");
                        txtAdsInfo.Text = xStor.GetAttrValue("AdsInfo", "");
                    }
                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            ryCommon.clsStorage tStor = new ryCommon.clsStorage();
            tStor.AddNode2("id", "Info");
            tStor.SetAttrValue("verName", txtVerName.Text);
            tStor.SetAttrValue("StatusOem", txtStatus.Text);
            tStor.SetAttrValue("StatusOemUrl", txtStatusUrl.Text);
            tStor.SetAttrValue("StatusOemToolTip", txtStatusToolTip.Text);
            tStor.SetAttrValue("AdsInfo", txtAdsInfo.Text);
            System.IO.File.WriteAllText(Application.StartupPath + "\\otmy.dat", ryCommon.clsAES.Encode("otmy" + tStor.GetXMLText(), "otmy1234567"));
            MessageBox.Show("保存信息成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            xmlPath = Application.StartupPath + "\\" + comboBox1.Text;;
            LoadDb(xmlPath);
        }
    }
}
