using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Helper;
using DAL;
using System.Diagnostics;

namespace Update
{
    public partial class FrmUpdate : Form
    {
        public static string UpdateUrl { get; set; }
        public VersionInfo LocalVersionInfo { get; set; }
        public VersionInfo TargetVervionInfo { get; set; }

        public FrmUpdate(VersionInfo info)
        {
            InitializeComponent();
            txtLocalVersion.Text = info.ToString().Replace("\n", "\r\n");
            LocalVersionInfo = info;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Process.Start("explorer.exe", TargetVervionInfo.Url);
            this.DialogResult = DialogResult.Yes;
        }

        private void panTop_MouseDown(object sender, MouseEventArgs e)
        {
            Win32Helper.MoveWindow(this);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Yes;
            this.Close();
        }

        private void FrmUpdate_Load(object sender, EventArgs e)
        {
        start: try
            {
                string src = HttpHelper.Get(UpdateUrl);
                TargetVervionInfo = (VersionInfo)ConvertEx.Base64ToObject(src);
                if (TargetVervionInfo.DALVersion > LocalVersionInfo.DALVersion)
                {
                    HttpHelper.Down(Application.StartupPath + "\\DALUpdate\\DAL.dll", TargetVervionInfo.DALUrl);
                    MessageBox.Show("解析接口已更新,请关闭软件,将DAL.dll复制替换到程序目录,重启软件完成更新", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Process.Start("explorer.exe", Application.StartupPath + "\\DALUpdate");
                }
                if (TargetVervionInfo.UIVersion > LocalVersionInfo.UIVersion)
                {
                    txtUpdateVersion.Text = TargetVervionInfo.ToString().Replace("\n", "\r\n");
                    return;
                }
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            catch (Exception ex)
            {
                if (MessageBox.Show(ex.Message, "更新失败", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
                { goto start; }
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }
    }
}
