using System.Collections.Generic;
using McKesson.PPS.Fusion.Authentication.Business;
using McKesson.PPS.Fusion.Business.Common.Context;
using McKesson.PPS.Fusion.ProviderSearch.Business;
using Pluto.Api;
using McKesson.PPS.Fusion.Persons.Business;
using McKesson.PPS.Fusion.Communications.Business.FolderContents.Recepients;

namespace PlutoServer.PracticeChoice.Core {
    public class Practice {
        public IList<ProviderInfo1> GetProviderList(string authToken) {
            IList<ProviderInfo1> providersList = null;
            MagicIdentity identity = MagicIdentity.FromCookieString(authToken);
            if(identity.IsAuthenticated) {
                MagicPrincipal principal = new MagicPrincipal(identity);

                //Logger.Write(string.Format("MagicIdentity authenticated is {0}", identity.IsAuthenticated));
                Csla.ApplicationContext.User = principal;
                var userContext = UserContext.GetInstance();

                var providers = ReadOnlyProviderList.GetReadOnlyProviderList(pageIndex: 0,
                                                                    pageSize: 250,
                                                                    lastName: string.Empty,
                                                                    firstName: string.Empty,
                                                                    specialty: string.Empty,
                                                                    city: string.Empty,
                                                                    zip: string.Empty,
                                                                    status: "active",
                                                                    gender: string.Empty,
                                                                    providerFlag: 1);
                if(providers != null) {
                    providersList = new List<ProviderInfo1>();
                    foreach(var provider in providers) {
                        providersList.Add(new ProviderInfo1() {
                            FirstName = provider.FirstName,
                            Id = provider.Id,
                            LastName = provider.LastName,
                            MiddleName = string.Empty,
                            UserId = provider.UserID
                        });
                    }
                }
            }
            return providersList;
        }

        public IList<PatientInfo1> GetPatientList(string authToken, string lastName, string firstName, string dateOfBirth) {
            IList<PatientInfo1> patientList = null;
            MagicIdentity identity = MagicIdentity.FromCookieString(authToken);
            if(identity.IsAuthenticated) {
                MagicPrincipal principal = new MagicPrincipal(identity);
                Csla.ApplicationContext.User = principal;
                var criteria = new PersonInfoCriteria() {
                    LastName = lastName, FirstName = firstName, PageSize = 25
                }; 
                var patients = PersonInfoList.GetPersonInfoList(criteria);
                if(patients != null) {
                    patientList = new List<PatientInfo1>();
                    foreach(var patient in patients) {
                        patientList.Add(new PatientInfo1() {
                            Id = patient.PersonID,
                            LastName = patient.LastName,
                            FirstName = patient.FirstName,
                            DOB = patient.DateOfBirth.Value,
                            ChartId = patient.ChartID
                        });
                        
                    }
                }
            }
            return patientList;
        }

        public IList<MessageRecipientInfo1> GetMessageRecipients(string authToken) {
            IList<MessageRecipientInfo1> recipientList = null;
            MagicIdentity identity = MagicIdentity.FromCookieString(authToken);
            if(identity.IsAuthenticated) {
                MagicPrincipal principal = new MagicPrincipal(identity);
                Csla.ApplicationContext.User = principal;
                var list = RecepientList.GetRecipientList(new RecipientListCriteria() { 
                    Filter = SelectionFilter.Person
                });
                if(list != null) {
                    recipientList = new List<MessageRecipientInfo1>();
                    foreach(var item in list) {
                        recipientList.Add(new MessageRecipientInfo1() { 
                            Id = item.ID,
                            Name = item.DisplayName
                        });
                    }
                }
            }
            return recipientList;
        }
    }
}
