namespace Pluto.Api {
    using System;
    using RemObjects.SDK;
    using RemObjects.SDK.Types;
    using RemObjects.SDK.Server;
    using RemObjects.SDK.Server.ClassFactories;
    using PlutoServer.PracticeChoice;
    using System.Linq;

    [RemObjects.SDK.Server.ClassFactories.StandardClassFactory()]
    [RemObjects.SDK.Server.Service(Name = "Provider", InvokerClass = typeof(Provider_Invoker), ActivatorClass = typeof(Provider_Activator))]
    public class Provider : RemObjects.SDK.Server.Service, IProvider {
        private System.ComponentModel.Container components = null;
        public Provider() :
            base() {
            this.InitializeComponent();
        }
        private void InitializeComponent() {
        }
        protected override void Dispose(bool aDisposing) {
            if(aDisposing) {
                if((this.components != null)) {
                    this.components.Dispose();
                }
            }
            base.Dispose(aDisposing);
        }

        #region IProvider Members

        public MailboxFolderInfo1[] GetMailboxFoldersInfo(string apiKey, int providerId) {
            if(Utility.IsAPIKeyValid(apiKey)) {
                var authToken = (string)this.Session[SessionKeys.AuthToken];
                var provider = new PlutoServer.PracticeChoice.Core.Provider();
                var folderInfoList = provider.GetMailboxFoldersInfo(authToken, providerId);
                return folderInfoList.ToArray();
            } else {
                return null;
            }
        }

        public FolderItemInfo1[] GetMailboxItems(string apiKey, MailboxFolderTypeEnum folderType) {
            if(Utility.IsAPIKeyValid(apiKey)) {
                var authToken = (string)this.Session[SessionKeys.AuthToken];
                var provider = new PlutoServer.PracticeChoice.Core.Provider();
                var mailItemsList = provider.GetMailboxItems(authToken, folderType);
                return mailItemsList.ToArray();
            } else {
                return null;
            }
        }

        public MessageInfo1 GetMessage(string apiKey, long messageId) {
            if(Utility.IsAPIKeyValid(apiKey)) {
                var authToken = (string)this.Session[SessionKeys.AuthToken];
                var provider = new PlutoServer.PracticeChoice.Core.Provider();
                var message = provider.GetMessage(authToken, messageId);
                return message;
            } else {
                return null;
            }
        }

        public void SendMessage(string apiKey, MessageInfo1 message) {
            if(Utility.IsAPIKeyValid(apiKey)) {
                var authToken = (string)this.Session[SessionKeys.AuthToken];
                var provider = new PlutoServer.PracticeChoice.Core.Provider();
                provider.SendMessage(authToken, message);
            }
        }

        #endregion
    }
}