using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Build.Proxy;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace TFSBuildManager
{
    public partial class DeleteBuildsProgressForm : Form
    {
        public DeleteBuildsProgressForm(BuildController buildController, BuildData[] buildsData, 
            bool backupToZip, string backupDirectory)
        {
            InitializeComponent();
            this.buildController = buildController;
            this.buildsData = buildsData;
            this.backupToZip = backupToZip;
            this.backupDirectory = backupDirectory;
        }

        private BuildController buildController;
        private BuildData[] buildsData;
        private bool backupToZip;
        private string backupDirectory;
        private bool stop;

        private void DeleteBuild(BuildData buildData)
        {
            txtLog.Text += String.Format("Build {0}", buildData.BuildNumber) + Environment.NewLine;
            txtLog.Text += "-------------------------------------------------------" + Environment.NewLine;
            bool canDelete = true;
            if (backupToZip)
            {
                txtLog.Text += "Backing up build output..." + Environment.NewLine;
                Application.DoEvents();
                try
                {
                    FastZip fastZip = new FastZip();
                    fastZip.CreateEmptyDirectories = true;
                    fastZip.CreateZip(backupDirectory + @"\" + buildData.BuildNumber + ".zip",
                        buildData.DropLocation, true, String.Empty);
                }
                catch (Exception ex)
                {
                    canDelete = false;
                    txtLog.Text += String.Format("Error backing up build output. {0}", ex.Message) + Environment.NewLine;
                    Application.DoEvents();
                }
            }
            if (canDelete)
            {
                string failureMessage = String.Empty;
                txtLog.Text += "Deleting build..." + Environment.NewLine;
                Application.DoEvents();
                buildController.DeleteBuild(buildData.BuildUri, out failureMessage);
                if (failureMessage != String.Empty)
                {
                    txtLog.Text += failureMessage + Environment.NewLine;
                    Application.DoEvents();
                }
            }
            else
            {
                txtLog.Text += "Skipped delete operation since bakcup has failed." + Environment.NewLine;
            }
            txtLog.Text += "-------------------------------------------------------" + Environment.NewLine;
            txtLog.Text += Environment.NewLine;
        }

        private void DeleteBuildsProgressForm_Shown(object sender, EventArgs e)
        {
            progressBar.Maximum = buildsData.Length;
            try
            {
                foreach (BuildData buildData in buildsData)
                {
                    if (!stop)
                    {
                        DeleteBuild(buildData);
                        progressBar.PerformStep();
                    }
                    else
                    {
                        txtLog.Text += "Operation stopped.";
                        return;
                    }
                }
                txtLog.Text += "Operation finished.";
            }
            finally
            {
                btnStop.Enabled = false;
                btnClose.Enabled = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            stop = true;
        }
    }
}