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 System.Runtime.InteropServices;
namespace LiveUpdate
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmUpdate : Form
    {
        private ClsLiveUpdate myLiveUpdate;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_myLiveUpdate"></param>
        public FrmUpdate(ClsLiveUpdate _myLiveUpdate)
        {
            InitializeComponent();
            myLiveUpdate = _myLiveUpdate;
        }
        /// <summary>
        /// 提示文本
        /// </summary>
        [Description("提示文本")]
        public string T_Capion
        {
            get { return label1.Text; }
            set { label1.Text = value; }
        }
        /// <summary>
        /// 更新日期
        /// </summary>
        [Description("更新日期")]
        public string T_UpdateDate
        {
            get { return label3.Text; }
            set { label3.Text = value; }
        }
        /// <summary>
        /// 更新版本
        /// </summary>
        [Description("更新版本")]
        public string T_UpdateVer
        {
            get { return label5.Text; }
            set { label5.Text = value; }
        }
        /// <summary>
        /// 更新描述
        /// </summary>
        [Description("更新描述")]
        public string T_UpdateDes
        {
            get { return labelTx1.Text; }
            set { labelTx1.Text = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public string ConvertDateStr(string Str, string defValue)
        {
            try
            {
                return Convert.ToDateTime(Str).ToString("yyyy-MM-dd");
            }
            catch
            {
                return defValue;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        public void SetUpdateInfo(UpdateInfo e)
        {
            if (e.UpdateType == "soft")
            {
                T_Capion = "发现软件存在更新的版本，升级到最新版本，可以体验新功能和更完善的软件。";
            }
            else
            {
                T_Capion = "发现更新的数据库，升级到最新版数据库，可以获取更全的显示内容。";
            }
            T_UpdateDate = ConvertDateStr(e.UpdateDate, "未知");
            T_UpdateVer = e.UpdateVer;
            if (e.UpdateDes_Url != "" && e.UpdateDes_Url.IndexOf("http", StringComparison.OrdinalIgnoreCase) == 0)
            {
                extendedWebBrowser1.Visible = true;
                labelTx1.Visible = false;
                extendedWebBrowser1.Navigate(e.UpdateDes_Url.Replace("<%rd%>",DateTime.Now.ToString("yyyyMMddHHmmss")));
            }
            else
            {
                extendedWebBrowser1.Visible = false ;
                labelTx1.Visible = true;
                T_UpdateDes = e.UpdateDes;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        [Description("设置多长时间后进行下次更新")]
        public int T_UpdateAfterTime
        {
            get
            {
                switch (comboBox1.SelectedIndex)
                {
                    case 0:
                        return 0;
                    case 1:
                        return 1;
                    case 2:
                        return 7;
                    default:
                        return 0;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public bool canClose = false;
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要不升级吗？不升级的话无法获得更好的体验？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
            {
                return;
            }
            ryCommon.Storage MyXml = new ryCommon.Storage();
            MyXml.LoadFromFile(myLiveUpdate.SettingPath);
            MyXml.SelectNode2("id", "UpdateAfterTime");
            MyXml.SetAttrValue("Value", T_UpdateAfterTime.ToString());
            MyXml.SaveToFile(myLiveUpdate.SettingPath);
            canClose = true;
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(int hWnd);
        private void FrmUpdate_Load(object sender, EventArgs e)
        {
            this.MinimumSize = Size;
            comboBox1.SelectedIndex = 0;
            ryCommon.RyForm.RemoveXButton(this.Handle);
            ryCommon.RyForm.BringToTop(this.Handle);
        }

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                btnUpdate.Enabled = false;
                btnCancel.Enabled = false;
                frmStartUpdate frm = new frmStartUpdate();
                ryCommon.RyFiles.DeleteFile(Application.StartupPath + "\\Update",false);
                Directory.CreateDirectory(Application.StartupPath + "\\Update");
                this.Hide();
                frm.T_myLiveUpdate = myLiveUpdate;
                frm.OnAppExit += Frm_OnAppExit;
                frm.ShowDialog(this);
                if (frm.DialogResult == DialogResult.OK)
                {
                    canClose = true;
                    Application.Exit();
                    Close();
                }
                else
                {
                    btnUpdate.Enabled = true;
                    btnCancel.Enabled = true;
                    this.Show();
                }
            }
            catch
            {
                MessageBox.Show("升级发生错误，升级失败。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public event LiveUpdate.OnAppExit OnAppExit;
        private void Frm_OnAppExit(object sender, EventArgs e)
        {
            OnAppExit?.Invoke(sender, e);
        }

        private void FrmUpdate_Resize(object sender, EventArgs e)
        {
            //labelTx1.Width = panelEx1.Width - labelTx1.Left- 1;
            panelEx1.Refresh();
        }

        private void FrmUpdate_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(!canClose) { e.Cancel = true; }
        }
    }
}
