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

namespace FantasySolution.FantasyCorrector
{
  public partial class UpdaterForm : Form
  {
    private IList UpdateLists;
    private ReleaseList LocalRelease = null;
    private ReleaseList RemoteRelease = null;
    private bool newRelaes = false;

    public UpdaterForm()
    {
      InitializeComponent();
    }

    private void UpdaterForm_Load(object sender, EventArgs e)
    {
      this.LocalRelease = new ReleaseList(Application.StartupPath + @"\" + GlobalVariable.Instance.ReleaseFileName);
      DownloadTools dt = new DownloadTools();

      textBox1.Text = this.LocalRelease.ReleaseVersion;
      textBox2.Text = this.LocalRelease.ReleaseDate;

      bool flg = dt.IsDownloadFile(GlobalVariable.Instance.LocalPath, GlobalVariable.Instance.ReleaseURL, GlobalVariable.Instance.ReleaseFileName);
      if (flg)
      {
        newRelaes = this.CheckUpdate();
        textBox3.Text = this.RemoteRelease.ReleaseVersion;
        textBox4.Text = this.RemoteRelease.ReleaseDate;
        textBox5.Text = this.RemoteRelease.ReleaseReason;
      }
      else
      {
        textBox3.Text = "未知";
        textBox4.Text = "未知";
        textBox5.Text = "未知";
      }
    }

    private bool CheckUpdate()
    {
      try
      {
        string fileNameAll = Application.StartupPath + "\\" + GlobalVariable.Instance.LocalPath + "\\" + GlobalVariable.Instance.ReleaseFileName;
        this.RemoteRelease = new ReleaseList(fileNameAll);
        if (this.LocalRelease==null||((this.LocalRelease != null) && (this.RemoteRelease != null)) && !this.LocalRelease.ReleaseDate.Equals(this.RemoteRelease.ReleaseDate))
        {
          return true;
        }
      }
      catch (Exception exception)
      {
        MessageBox.Show(exception.Message);
      }
      return false;
    }

    private void CloseButtonOnClick(object sender, EventArgs e)
    {
      this.Close();
    }

    private void UpdateButtonOnClick(object sender, EventArgs e)
    {
      if (this.newRelaes)
      {
        this.Init();
        if (this.StartDownload())
        {
          DialogResult result = MessageBox.Show("新版本更新完成，是否现在启动新程序？", Application.ProductName, MessageBoxButtons.YesNo);
          if (result == DialogResult.Yes)
          {
            this.Close();
            Application.Exit();
            GC.Collect();
            Process.Start(Application.StartupPath + @"\" + GlobalVariable.Instance.ApplicationStart);
          }
        }
      }
      else
      {
        MessageBox.Show("没有新版本，或者远程服务器无法连接！");
      }
    }

    public void Init()
    {
      this.textStatusLabel.Visible = true;
      this.progressBar.Visible = true;
      this.progressBar.Value = 0;
      this.statusStrip1.Update();

      IDictionary dictionary = new Hashtable();
      foreach (ReleaseFile file1 in this.LocalRelease.ReleaseFiles)
      {
        dictionary.Add(file1.FileName, file1);
      }
      this.UpdateLists = new ArrayList();
      foreach (ReleaseFile file2 in this.RemoteRelease.ReleaseFiles)
      {
        if (file2.FileName.Equals(GlobalVariable.Instance.ApplicationUpdater)) continue;

        ReleaseFile file3 = dictionary[file2.FileName] as ReleaseFile;
        if (file3 != null)
        {
          if (!file3.ReleaseDate.Equals(file2.ReleaseDate))
          {
            this.UpdateLists.Add(file2);
          }
          continue;
        }
        this.UpdateLists.Add(file2);
      }
    }

    private bool StartDownload()
    {
      string path = Application.StartupPath + @"\" + GlobalVariable.Instance.LocalPath;
      if (!Directory.Exists(path))
      {
        Directory.CreateDirectory(path);
      }
      IList list = new ArrayList();
      bool flag = true;
      this.progressBar.Maximum = 20 * this.UpdateLists.Count;
      int num = 20;
      foreach (ReleaseFile file in this.UpdateLists)
      {
        if (file == null)
        {
          continue;
        }
        string text3 = new DownloadTools().DownloadFile(path, GlobalVariable.Instance.ReleaseURL, file.FileName);
        if (text3 != null)
        {
          list.Add(text3);
          this.progressBar.Value += num;
          continue;
        }
        flag = false;
        break;
      }
      if (flag)
      {
        foreach (string text4 in list)
        {
          string sourceFileName = path + @"\" + text4;
          string destFileName = Application.StartupPath + @"\" + text4;
          if (text4.IndexOf(".exe.xml") >= 0)
          {
            destFileName = Application.StartupPath + @"\" + text4.Replace(".exe.xml",".exe.config");
          }
          File.Copy(sourceFileName, destFileName, true);
          File.Delete(sourceFileName);
        }
        string filename = Application.StartupPath + @"\" + GlobalVariable.Instance.ReleaseFileName;
        this.RemoteRelease.Save(filename);
        this.progressBar.Value = 20 * this.UpdateLists.Count;
        return true;
      }
      return false;
    }
  }
}