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

namespace Engineer.Forms
{
    public partial class Update : Form
    {
        Updating update;

        public Update()
        {
            InitializeComponent();
        }

        public void BeingUpdate()
        {
            //Create a new updater
            update = new Updating();

            //Add our handlers and being the download
            update.AsyncCompletedEventHandler += new AsyncCompletedEventHandler(update_AsyncCompletedEventHandler);
            update.DownloadProgressChangedEventHandler += new System.Net.DownloadProgressChangedEventHandler(update_DownloadProgressChangedEventHandler);

            labelX2.Text = "Downloading Update...";
            Application.DoEvents();
            update.DownloadUpdate();
        }

        void update_DownloadProgressChangedEventHandler(object sender, System.Net.DownloadProgressChangedEventArgs e)
        {
            //Update our progress
            progressBarX1.Value = e.ProgressPercentage;
        }

        void update_AsyncCompletedEventHandler(object sender, AsyncCompletedEventArgs e)
        {
            //Reset the progress to 0
            progressBarX1.Value = 0;

            //Update our label
            labelX2.Text = "Unpacking Update...";
            Application.DoEvents();

            //We have completed the download so lets unpack now
            update.ByteProgressionEventHandler += new Xceed.FileSystem.ByteProgressionEventHandler(update_ByteProgressionEventHandler);
            update.ItemProgressionEventHandler += new Xceed.FileSystem.ItemProgressionEventHandler(update_ItemProgressionEventHandler);
            update.UnpackUpdate();

            //Now delete the update
            update.DeleteUpdate();

            //Now clear the settings if we must
            if (update.UpdatingInfo.CleanSettings)
                AppSettings.DeleteSettings();

            labelX2.Text = "Update Complete...";

            //Now lets restart the app so we can perform a cleanup
            DialogResult dr = MessageBox.Show("You must restart the Engineer for the updates to take effect.\nWould you like to restart now?", "Restart", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                System.Diagnostics.Process.Start(Application.StartupPath + "\\Engineer.exe");
                Application.Exit();
            }

            this.Close();
        }

        void update_ItemProgressionEventHandler(object sender, Xceed.FileSystem.ItemProgressionEventArgs e)
        {
            labelX2.Text = "Unpacking " + e.CurrentItem.FullName + "...";
            Application.DoEvents();
        }

        void update_ByteProgressionEventHandler(object sender, Xceed.FileSystem.ByteProgressionEventArgs e)
        {
            progressBarX1.Value = e.AllFilesBytes.Percent;
        }

        private void Update_Load(object sender, EventArgs e)
        {
            BeingUpdate();
        }

    }
}