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

namespace TFSWrappers
{
    public class ServerWrapper /*: IDisposable*/
    {
        public ServerWrapper(TeamFoundationServer tfs)
        {
            this.tfs = tfs;
        }

        public ServerWrapper(string serverName)
        {
            this.tfs = TFSFunctions.GetServer(serverName);
        }

        public ServerWrapper(ConfigurationType configurationType)
        {
            NameValueCollection appSettings = TFSFunctions.GetTFSAppSettingsSection(configurationType);
            string serverName = appSettings[TFSConstants.ServerAppSettingsValueName];
            this.tfs = TFSFunctions.GetServer(serverName);
        }

        private TeamFoundationServer tfs;

        public TeamFoundationServer TFS
        {
            get { return tfs; }
        }

        /*public void Dispose()
        {
            if (tfs != null)
            {
                tfs = null;
            }
        }*/

        public void SubscribeToWorkItemsChangeEvent(string userName, string project, WorkItemChangeType changeType, DeliveryType type, DeliverySchedule schedule, string address)
        {
            DeliveryPreference preference = new DeliveryPreference();
            preference.Schedule = schedule;
            preference.Type = type;
            preference.Address = address;
            IEventService eventService = (IEventService)tfs.GetService(typeof(IEventService));
            StringBuilder filterExpression = new StringBuilder();
            filterExpression.Append("PortfolioProject = '" + project + "'");
            if (changeType == WorkItemChangeType.Added)
            {
                filterExpression.Append(" AND ChangeType = 'New'");
            }
            else if (changeType == WorkItemChangeType.Changed)
            {
                filterExpression.Append(" AND ChangeType = 'Change'");
            }
            eventService.SubscribeEvent(userName, "WorkItemChangedEvent", filterExpression.ToString(), preference);
        }

        public ProjectInfo GetProject(string name)
        {
            ICommonStructureService commonStructureService = (ICommonStructureService)tfs.GetService(typeof(ICommonStructureService));
            return commonStructureService.GetProjectFromName(name);
        }

        public string[] GetProjectNames()
        {
            ICommonStructureService commonStructureService = (ICommonStructureService)tfs.GetService(typeof(ICommonStructureService));
            ProjectInfo[] projects = commonStructureService.ListProjects();
            List<string> projectNames = new List<string>();
            foreach (ProjectInfo project in projects)
            {
                projectNames.Add(project.Name);
            }
            return projectNames.ToArray();
        }
    }

    public enum WorkItemChangeType
    {
        Both,
        Added,
        Changed
    }
}
