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;
using ryCommon;
using UpdateEdit;

namespace LiveUpdate
{
    public partial class frmUpdateFile : Form
    {
        ryCommon.Storage MyXml = new ryCommon.Storage();
        string xmlPath;
        public frmUpdateFile()
        {
            InitializeComponent();
        }

        private void BtnCancel_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;
            }
        }
        public string Base64ToStr(string input)
        {
            try
            {
                byte[] bpath = Convert.FromBase64String(input);
                return System.Text.Encoding.Default.GetString(bpath);
            }
            catch { return ""; }
        }
        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");
            cmbNewVer.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", "FirstDownList");
            TxtFirstDownList.Text = MyXml.GetAttrValue("Value", "");
            MyXml.SelectNode2("id", "SettingXML");
            ryCommon.Strings cstr = new ryCommon.Strings();
            ryCommon.Storage tStor = new ryCommon.Storage(MyXml.GetAttrValue("Value", ""));
            //升级前要结束的进程
            tStor.SelectNode("id", "KillProcList2");
            txtKillProcList.Text = cstr.Base64ToStr(tStor.GetAttrValue("Value", ""));
            //升级前要删除的文件
            tStor.SelectNode("id", "DelFileList");
            txtKillFiles.Text = cstr.Base64ToStr(tStor.GetAttrValue("Value", ""));
            //要启动的主程序
            tStor.SelectNode("id", "MainProcName");
            txtMainProcName.Text = tStor.GetAttrValue("Value", "");
            tStor.SelectNode("id", "MainProcPram");
            txtMainProcPram.Text = tStor.GetAttrValue("Value", "");
        }
        private void FrmUpdateFile_Load(object sender, EventArgs e)
        {
            string  FileName="";
            cbbFiles.Items.Clear();
            DirectoryInfo directory = new DirectoryInfo(Application.StartupPath);
            foreach (FileInfo info in directory.GetFiles("*.xml"))
            {
                cbbFiles.Items.Add(info.Name);
            }
            if (cbbFiles.Items.Count > 0)
            {
                cbbFiles.SelectedIndex = 0;
                xmlPath = Application.StartupPath + "\\" + cbbFiles.Text; FileName = cbbFiles.Text;
            }
            else
            {
                xmlPath = Application.StartupPath + "\\Update.xml"; FileName = "Update.xml";
            }
            LoadDb(xmlPath);

        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            //if (IsNumeric(cmbNewVer.Text) == false)
            //{
            //    MessageBox.Show("【软件版本】必须为数字。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            if (cmbNewDbVer.Text.IsDouble() == 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", "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);
            MyXml.AddNode2("id", "FirstDownList");
            TxtFirstDownList.Text=GetDownList();
            MyXml.SetAttrValue("Value", GetDownList());
            ryCommon.Storage tStor = new ryCommon.Storage();
            tStor.AddNode2("id", "KillProcList2");
            tStor.SetAttrValue("Value", txtKillProcList.Text.ToBase64());
            tStor.AddNode2("id", "DelFileList");
            tStor.SetAttrValue("Value", txtKillFiles.Text.ToBase64());
            tStor.AddNode2("id", "MainProcName");
            tStor.SetAttrValue("Value", txtMainProcName.Text);
            tStor.AddNode2("id", "MainProcPram");
            tStor.SetAttrValue("Value", txtMainProcPram.Text);
            MyXml.AddNode2("id", "SettingXML");
            MyXml.SetAttrValue("Value", tStor.GetXMLText());
            MyXml.SaveToFile(xmlPath);
            SaveDownList();
            MessageBox.Show("保存信息成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void FrmUpdateFile_Resize(object sender, EventArgs e)
        {
            tabControl1.Refresh();
        }

        private void CbbFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            xmlPath = Application.StartupPath + "\\" + cbbFiles.Text;
            LoadDb(xmlPath);
            LoadDownList();
        }
        public void LoadDownList()
        {
            string xml_path= Application.StartupPath + "\\" +System.IO.Path.GetFileNameWithoutExtension(cbbFiles.Text)+".lst";
            ryCommon.Storage myStor = new Storage();
            myStor.LoadFromFile(xml_path);
            var list= myStor.GetList();
            listView1.Items.Clear();
            for (int i = 0; i < list.Count; i++)
            {
                myStor.SelectNode3(list[i]);
                var item = listView1.Items.Add(myStor.GetAttrValue("file_id"));
                item.Tag = new DownItem()
                {
                    File_Id = myStor.GetAttrValue("file_id"),
                    File_Ver = myStor.GetAttrValue("file_ver"),
                    Path = myStor.GetAttrValue("file_path"),
                    File_Url = myStor.GetAttrValue("file_url"),
                    File_SavePath = myStor.GetAttrValue("file_savepath")
                };
                item.SubItems.Add(myStor.GetAttrValue("file_ver"));
                item.SubItems.Add(myStor.GetAttrValue("file_url"));
                item.SubItems.Add(myStor.GetAttrValue("file_savepath"));
            }
        }
        public void SaveDownList()
        {
            string xml_path = Application.StartupPath + "\\" + System.IO.Path.GetFileNameWithoutExtension(cbbFiles.Text) + ".lst";
            ryCommon.Storage myStor = new Storage();
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                var item = (DownItem)listView1.Items[i].Tag;
                myStor.SelectNode2("file_id", item.File_Id);
                myStor.SetAttrValue("file_ver", item.File_Ver);
                myStor.SetAttrValue("file_path", item.Path);
                myStor.SetAttrValue("file_url", item.File_Url);
                myStor.SetAttrValue("file_savepath", item.File_SavePath);
            }
            myStor.SaveToFile(xml_path);
        }
        public string GetDownList()
        {
            string line = "";
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                var item = (DownItem)listView1.Items[i].Tag;
                if (line != "") { line += "\r\n"; }
                line += item.File_Id+ "|"+ item.File_Ver+"|"+RyFiles.GetFileSize(item.Path)+"|"+ item.File_Url+"|"+ item.File_SavePath;
            }
            return line;
        }
        private void 添加文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAddDownFile frm = new FrmAddDownFile();
            frm.Icon = Icon;
            if (frm.ShowDialog()==DialogResult.OK)
            {
                var item = listView1.Items.Add(frm.TxtFile_Id.Text);
                item.Tag = new DownItem()
                {
                    File_Id = frm.TxtFile_Id.Text,
                    File_Ver = frm.TxtFile_Ver.Text,
                    Path = frm.TxtPath.Text,
                    File_Url = frm.TxtFile_Url.Text,
                    File_SavePath = frm.TxtSavePath.Text
                };
                item.SubItems.Add(frm.TxtFile_Ver.Text);
                item.SubItems.Add(frm.TxtFile_Url.Text);
                item.SubItems.Add(frm.TxtSavePath.Text);
            }
        }

        private void 修改文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) { return; }
            FrmAddDownFile frm = new FrmAddDownFile();
            frm.Icon = Icon;
            var item = (DownItem)listView1.SelectedItems[0].Tag;
            frm.TxtFile_Id.Text = item.File_Id;
            frm.TxtFile_Ver.Text = item.File_Ver;
            frm.TxtPath.Text = item.Path;
            frm.TxtFile_Url.Text = item.File_Url;
            frm.TxtSavePath.Text = item.File_SavePath;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                listView1.SelectedItems[0].Tag = new DownItem()
                {
                    File_Id = frm.TxtFile_Id.Text,
                    File_Ver = frm.TxtFile_Ver.Text,
                    Path = frm.TxtPath.Text,
                    File_Url = frm.TxtFile_Url.Text,
                    File_SavePath = frm.TxtSavePath.Text
                };
                listView1.SelectedItems[0].SubItems[columnHeader2.Index].Text=frm.TxtFile_Ver.Text;
                listView1.SelectedItems[0].SubItems[columnHeader3.Index].Text = frm.TxtFile_Url.Text;
                listView1.SelectedItems[0].SubItems[columnHeader4.Index].Text = frm.TxtSavePath.Text;
            }
        }

        private void 删除文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) { return; }
            listView1.SelectedItems[0].Remove();
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            修改文件ToolStripMenuItem.PerformClick();
        }
    }
    public class DownItem
    {
        public string Path="";
        public string File_Id = "";
        public string File_Ver = "";
        public string File_Url = "";
        public string File_SavePath = "";
    }
}
