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

namespace TFSWrappers
{
    public class TFSEventServiceWrapper
    {
        public TFSEventServiceWrapper(ConfigurationType configurationType)
        {
            NameValueCollection appSettings = TFSFunctions.GetTFSAppSettingsSection(configurationType);
            string serverName = appSettings[TFSConstants.ServerAppSettingsValueName];
            tfs = TFSFunctions.GetServer(serverName);
            eventService = (IEventService)tfs.GetService(typeof(IEventService));
        }

        public TFSEventServiceWrapper(string serverName)
        {
            tfs = TFSFunctions.GetServer(serverName);
            eventService = (IEventService)tfs.GetService(typeof(IEventService));
        }

        public TFSEventServiceWrapper(TeamFoundationServer tfs)
        {
            this.tfs = tfs;
            eventService = (IEventService)tfs.GetService(typeof(IEventService));
        }

        private TeamFoundationServer tfs;

        public TeamFoundationServer TFS
        {
            get { return tfs; }
        }

        private IEventService eventService;

        public IEventService EventService
        {
            get { return eventService; }
        }

        public Subscription[] GetSubscriptions()
        {
            return eventService.EventSubscriptions(Environment.UserDomainName + @"\" + Environment.UserName);
        }

        public Subscription[] GetSubscriptions(string user)
        {
            return eventService.EventSubscriptions(user);
        }

        public static string[] GetDeliverySchedules()
        {
            return Enum.GetNames(typeof(Microsoft.TeamFoundation.Server.DeliverySchedule));
        }

        public static string[] GetDeliveryTypes()
        {
            return Enum.GetNames(typeof(Microsoft.TeamFoundation.Server.DeliveryType));
        }

        public void DeleteSubscription(Subscription subscription)
        {
            eventService.UnsubscribeEvent(subscription.ID);
        }

        public void SaveSubscription(Subscription subscription)
        {
            eventService.SubscribeEvent(subscription.Subscriber, subscription.EventType, subscription.ConditionString, subscription.DeliveryPreference, subscription.Tag);
        }
    }
}
