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.Client;
using TFSWrappers;
using System.Xml;
using System.IO;

namespace TFSBuildManager
{
    public partial class SelectTestListsForm : Form
    {
        public SelectTestListsForm(TeamFoundationServer tfs, string testFileServerPath/*, string[] testListsToSelect*/)
        {
            InitializeComponent();
            InitTestLists(tfs, testFileServerPath/*, testListsToSelect*/);
        }

        private ListViewItem GetListViewItem(string text)
        {
            foreach (ListViewItem listViewItem in lvFiles.Items)
            {
                if (listViewItem.Text == text)
                {
                    return listViewItem;
                }
            }
            return null;
        }

        private void InitTestLists(TeamFoundationServer tfs, string testFileServerPath/*, string[] testListsToSelect*/)
        {
            SourceControlWrapper sourceControlWrapper = new SourceControlWrapper(tfs);
            string testFileLocalPath = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), Path.GetFileName(testFileServerPath));
            WorkspaceWrapper workspaceWrapper = new WorkspaceWrapper(tfs, sourceControlWrapper.CreateTempWorkspace().Name);
            try
            {
                workspaceWrapper.GetLatestVersion(testFileServerPath, testFileLocalPath);
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(testFileLocalPath);
                XmlNodeList xmlNodes = xmlDocument.SelectNodes("//TestCategory/name");
                foreach (XmlNode xmlNode in xmlNodes)
                {
                    if (String.Compare(xmlNode.InnerText, "List of Tests", true) != 0)
                    {
                        lvFiles.Items.Add(xmlNode.InnerText);
                    }
                }
                /*foreach (string testListToSelect in testListsToSelect)
                {
                    ListViewItem listViewItem = GetListViewItem(testListToSelect);
                    if (listViewItem != null)
                    {
                        listViewItem.Checked = true;
                    }
                }*/
            }
            finally
            {
                workspaceWrapper.ClearFileMapping(testFileServerPath);
                sourceControlWrapper.DeleteWorkspace(workspaceWrapper.WS.Name);
            }
        }

        private string[] selectedTestLists;

        public string[] SelectedTestLists
        {
            get { return selectedTestLists; }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            List<string> checkedLists = new List<string>();
            foreach (ListViewItem listViewItem in lvFiles.CheckedItems)
            {
                checkedLists.Add(listViewItem.Text);
            }
            selectedTestLists = checkedLists.ToArray();
        }
    }
}