namespace Pluto.Api {
    using System;
    using RemObjects.SDK;
    using RemObjects.SDK.Types;
    using RemObjects.SDK.Server;
    using RemObjects.SDK.Server.ClassFactories;
    using PlutoServer.MSL;
    using System.Collections.Generic;
    using PlutoServer.MSL.Connectors;


    [RemObjects.SDK.Server.ClassFactories.StandardClassFactory()]
    [RemObjects.SDK.Server.Service(Name = "Authentication", InvokerClass = typeof(Authentication_Invoker), ActivatorClass = typeof(Authentication_Activator))]
    public class Authentication : RemObjects.SDK.Server.Service, IAuthentication {

        private System.ComponentModel.Container components = null;
        public Authentication() :
            base() {
            this.InitializeComponent();
        }

        private void InitializeComponent() {
            
        }

        protected override void Dispose(bool aDisposing) {
            if(aDisposing) {
                if((this.components != null)) {
                    this.components.Dispose();
                }
            }
            base.Dispose(aDisposing);
        }

        public virtual UserInfo1 Login1(string apiKey, PracticeInfo1 practiceInfo, string userId, string password) 
        {
            if(ProductType.IsKeyLytec(apiKey))
            {
                UserInfo1 info = LytecConnector.AuthenticateUserLogin(apiKey, userId, password);
                if (info == null)
                    MSLSpecific.Logger.Error("Lytec Authentication Failed for User:{0} into Practice:{1}", userId, practiceInfo.Name);
                return info;
            }
            else if (ProductType.IsKeyMedisoft(apiKey))
            {
                UserInfo1 info = MedisoftConnector.AuthenticateUserLogin(apiKey, userId, password);
                if (info == null)
                    MSLSpecific.Logger.Error("Medisoft Authentication Failed for User:{0} into Practice:{1}", userId, practiceInfo.Name);
                return info;
            }

            MSLSpecific.Logger.Error("Invalid Product");
            return null;
        }

        #region IAuthentication Members

        public PracticeInfo1[] GetPracticeList1(string apiKey) 
        { 
            MSLSpecific.Logger.Error("GetPracticeList1 is not implemented, shouldn't never be called");
            return null;
        }

        public virtual bool CreatePin1(string apiKey, PracticeInfo1 practiceInfo, string userId, string fourdigitpin)
        {
            if (ProductType.IsKeyLytec(apiKey))
            {
                return LytecConnector.CreatePin(apiKey, userId, fourdigitpin);
            }
            else if (ProductType.IsKeyMedisoft(apiKey))
            {
                return MedisoftConnector.CreatePin(apiKey, userId, fourdigitpin);
            }

            MSLSpecific.Logger.Error("Invalid Product");
            return false;            
        }

        public virtual UserInfo1 ValidatePin1(string apiKey, PracticeInfo1 practiceInfo, string userId, string pin)
        {
            if (ProductType.IsKeyLytec(apiKey))
            {
                return LytecConnector.AuthenticateUserPIN(apiKey, userId, pin);
            }
            else if (ProductType.IsKeyMedisoft(apiKey))
            {
                return MedisoftConnector.AuthenticateUserPIN(apiKey, userId, pin);
            }

            MSLSpecific.Logger.Error("Invalid Product");
            return null;
        }        

        public virtual bool PinIsSet1(string apiKey, PracticeInfo1 practiceInfo, string userId)
        {
            if (ProductType.IsKeyLytec(apiKey))
            {
                return LytecConnector.PinIsSet(apiKey, userId);
            }
            else if (ProductType.IsKeyMedisoft(apiKey))
            {
                return MedisoftConnector.PinIsSet(apiKey, userId);
            }

            MSLSpecific.Logger.Error("Invalid Product");
            return false;
        }

        public virtual string GetAPIVersion()
        {
            Version LocalVersion = Yaulw.Assembly.AssemblyW.GetAssemblyVersion(Yaulw.Assembly.AssemblyW.AssemblyST.Entry);
            return LocalVersion.ToString();
        }

        public virtual bool IsAPIOutOfDate()
        {
            bool bRetVal = AutoUpdate.IsLocalServiceVersionOutOfDate();
            if (bRetVal)
                MSLSpecific.Logger.Info("Local Service is out of Date");
            return bRetVal;
        }

        #endregion
    }
}
