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.IO;

namespace TFSBuildManager
{
    public partial class SelectSourceControlFilesForm : Form
    {
        public SelectSourceControlFilesForm(TeamFoundationServer tfs, string title, string description, 
            string serverPath, string[] patterns, bool multiSelect)
        {
            InitializeComponent();
            Text = title;
            lblDescription.Text = description;
            lvFiles.MultiSelect = multiSelect;
            lvFiles.CheckBoxes = multiSelect;
            SourceControlWrapper sourceControlWrapper = new SourceControlWrapper(tfs);
            foreach (string pattern in patterns)
            {
                string[] files = sourceControlWrapper.GetFiles(serverPath, pattern, true);
                foreach (string file in files)
                {
                    ListViewItem listViewItem = lvFiles.Items.Add(Path.GetFileNameWithoutExtension(file) + "(" + file + ")");
                    listViewItem.Tag = file;
                }
            }
        }

        private string[] selectedFiles;

        public string[] SelectedFiles
        {
            get { return selectedFiles; }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            List<string> files = new List<string>();
            if (lvFiles.MultiSelect)
            {
                foreach (ListViewItem listViewItem in lvFiles.CheckedItems)
                {
                    files.Add((string)listViewItem.Tag);
                }
            }
            else
            {
                files.Add((string)lvFiles.SelectedItems[0].Tag);
            }
            selectedFiles = files.ToArray();
        }
    }
}