using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections.Specialized;

namespace TFSWrappers
{
    public class SourceControlWrapper /*: IDisposable*/
    {
        /// <summary>
        /// Constractor for creating a wrapper object. Server name is defined in a configuration section.
        /// </summary>
        public SourceControlWrapper(ConfigurationType configurationType)
        {
            NameValueCollection appSettings = TFSFunctions.GetTFSAppSettingsSection(configurationType);
            string serverName = appSettings[TFSConstants.ServerAppSettingsValueName];
            tfs = TFSFunctions.GetServer(serverName);
            vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
        }

        /// <summary>
        /// Constractor for creating a wrapper object.
        /// </summary>
        /// <param name="serverName">The Team Foundation Server name</param>
        public SourceControlWrapper(string serverName)
        {
            tfs = TFSFunctions.GetServer(serverName);
            vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
        }

        public SourceControlWrapper(TeamFoundationServer tfs)
        {
            this.tfs = tfs;
            vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
        }

        /// <summary>
        /// Free all objects
        /// </summary>
        /*public void Dispose()
        {
            if (vcs != null)
            {
                vcs = null;
            }
            if (tfs != null)
            {
                tfs = null;
            }
        }*/

        private TeamFoundationServer tfs;

        /// <summary>
        /// The TeamFoundationServer object of the wrapper.
        /// </summary>
        public TeamFoundationServer TFS
        {
            get { return tfs; }
        }

        private VersionControlServer vcs;

        /// <summary>
        /// The VersionControlSever of the wrapper.
        /// </summary>
        public VersionControlServer VCS
        {
            get { return vcs; }
        }

        /// <summary>
        /// Creates a new workspace with the current authnticated user as the owner.
        /// </summary>
        /// <param name="workspaceName">Workspace name</param>
        /// <returns>Created workspace object</returns>
        public Workspace CreateWorkspace(string workspaceName)
        {
            return vcs.CreateWorkspace(workspaceName, vcs.AuthenticatedUser);
        }

        /// <summary>
        /// Deletes a workspace. The workspace owner is the current authenticated user.
        /// </summary>
        /// <param name="workspaceName">Workspace name</param>
        public void DeleteWorkspace(string workspaceName)
        {
            vcs.DeleteWorkspace(workspaceName, vcs.AuthenticatedUser);
        }

        public string[] GetFiles(string serverPath, string searchPattern, bool recursive)
        {
            RecursionType recursionType = RecursionType.OneLevel;
            if (recursive)
            {
                recursionType = RecursionType.Full;
            }
            ItemSet itemSet = vcs.GetItems(serverPath, VersionSpec.Latest, recursionType, DeletedState.NonDeleted, ItemType.File);
            Item[] items = itemSet.Items;
            string pattern = searchPattern.Replace("*", "");
            List<string> files = new List<string>();
            foreach (Item item in items)
            {
                if (item.ServerItem.EndsWith(pattern))
                {
                    files.Add(item.ServerItem);
                }
            }
            return files.ToArray();
        }

        public string[] GetFolders(string serverPath, bool recursive)
        {
            RecursionType recursionType = RecursionType.OneLevel;
            if (recursive)
            {
                recursionType = RecursionType.Full;
            }
            ItemSet itemSet = vcs.GetItems(serverPath, VersionSpec.Latest, recursionType, DeletedState.NonDeleted, ItemType.Folder);
            Item[] items = itemSet.Items;
            List<string> folders = new List<string>();
            foreach (Item item in items)
            {
                folders.Add(item.ServerItem);
            }
            return folders.ToArray();
        }

        public Change[] GetChangeSetChanges(int changeSet)
        {
            //vcs.GetChangeset().
            return null;
        }

        public Workspace CreateTempWorkspace()
        {
            return CreateWorkspace(Guid.NewGuid().ToString());
        }
    }
}
