using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pluto.Api;
using McKesson.PPS.Fusion.Schedule.Business;
using McKesson.PPS.Fusion.Authentication.Business;
using McKesson.PPS.Fusion.Business.Common.Context;

namespace PlutoServer.PracticeChoice.Scheduling {
    public class Calendar {
        public IList<AppointmentInfo1> GetAppointments(string authToken, ProviderInfo1 providerInfo, int calendarId, DateTime startDateTime, DateTime endDateTime) {
            IList<AppointmentInfo1> apptList = 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 providerId = (providerInfo == null ? identity.Name : providerInfo.UserId);
                
                if(calendarId < 1) {
                    calendarId = this.GetProviderDefaultCalendar(providerId);
                }
                if(calendarId > 0) {
                    var criteria = new CalendarDataCriteria() { CalendarID = calendarId, StartDateTime = startDateTime, EndDateTime = endDateTime };
                    var apptInfoList = AppointmentList.GetAppointmentList(criteria);
                    if(null != apptInfoList) {
                        apptList = (from ai in apptInfoList
                                where ai.CalendarId == calendarId
                                select new AppointmentInfo1() {
                                    EndDateTime = ai.EndDate, 
                                    Id = ai.Id,
                                    Reason = ai.Reason,
                                    StartDateTime = ai.StartDate,
                                    Patient = new PatientInfo1() {
                                        DOB = DateTime.Parse(ai.Patient.DOB),
                                        FirstName = ai.Patient.FirstName,
                                        Id = ai.Patient.PersonID,
                                        LastName = ai.Patient.LastName,
                                        ChartId = ai.Patient.ChartID
                                    }
                                }).ToList<AppointmentInfo1>();
                    }
                }
            }
            return apptList;
        }

        private int GetProviderDefaultCalendar(string providerId) {
            int calendarId = 0;
            var calInfolist = CalendarInfoList.GetCalendarInfoList(pageIndex: 1, pageSize: 100);
            if(null != calInfolist) {
                //foreach(var calInfo in calInfolist) {
                //    System.Diagnostics.Debug.WriteLine(string.Format("Calendar: {0}, Default Provider Id: {1}", calInfo.CalendarName, calInfo.DefaultProviderId));
                //}
                var calendar = (from ci in calInfolist
                                            where (string.Compare(ci.DefaultProviderId, providerId, StringComparison.InvariantCultureIgnoreCase) == 0)
                                            select ci).FirstOrDefault();
                if(calendar != null) {
                    calendarId = calendar.Id;
                }
            }
            return calendarId;
        }
    }
}
