public async Task <CalendarAppointments> AddCalendarEvents(List <Appointment> calendarAppointments,
                                                                   bool addDescription,
                                                                   bool addReminder, bool addAttendees, bool attendeesToDescription,
                                                                   IDictionary <string, object> calendarSpecificData)
        {
            var addedAppointments = new CalendarAppointments();

            if (!calendarAppointments.Any())
            {
                addedAppointments.IsSuccess = true;
                return(addedAppointments);
            }

            CheckCalendarSpecificData(calendarSpecificData);

            var errorList = new Dictionary <int, Appointment>();
            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            if (calendarAppointments == null || string.IsNullOrEmpty(CalendarId))
            {
                addedAppointments.IsSuccess = false;
                return(addedAppointments);
            }
            try
            {
                if (calendarAppointments.Any())
                {
                    //Split the list of calendarAppointments by 1000 per list
                    var appts =
                        await AddCalendarEventsInternal(calendarAppointments, addDescription, addReminder, addAttendees,
                                                        attendeesToDescription, calendarService, errorList);

                    addedAppointments.AddRange(appts);
                    if (errorList.Count > 0)
                    {
                        var remaningList = errorList.Select(CreateAppointmentWithoutAttendees).ToList();
                        errorList.Clear();

                        appts = await AddCalendarEventsInternal(remaningList, addDescription, addReminder, addAttendees,
                                                                attendeesToDescription, calendarService, errorList);

                        addedAppointments.AddRange(appts);
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                addedAppointments.IsSuccess = false;
                return(addedAppointments);
            }
            addedAppointments.IsSuccess = true;
            return(addedAppointments);
        }
        public async Task<CalendarAppointments> GetCalendarEventsInRangeAsync(DateTime startDate, DateTime endDate,
            IDictionary<string, object> calendarSpecificData)
        {
            CheckCalendarSpecificData(calendarSpecificData);
            var calendarAppointments = new CalendarAppointments();

            var appointmentList =
                await
                    Task<List<Appointment>>.Factory.StartNew(
                        () => GetAppointments(startDate, endDate));

            if (appointmentList == null)
            {
                return null;
            }

            if (OutlookCalendar != null)
            {
                calendarAppointments.CalendarId = OutlookCalendar.EntryId;
            }

            calendarAppointments.AddRange(appointmentList);

            return calendarAppointments;
        }
        public async Task <CalendarAppointments> GetCalendarEventsInRangeAsync(DateTime startDate, DateTime endDate,
                                                                               IDictionary <string, object> calendarSpecificData)
        {
            CheckCalendarSpecificData(calendarSpecificData);

            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            var finalEventList = new List <Appointment>();

            Events result = null;

            var eventListRequest = calendarService.Events.List(CalendarId);

            // Add Filters to event List Request
            eventListRequest.TimeMin      = startDate;
            eventListRequest.TimeMax      = endDate;
            eventListRequest.MaxAttendees = 1000;
            eventListRequest.SingleEvents = true;
            try
            {
                result = eventListRequest.Execute();
                if (result != null)
                {
                    while (result.Items != null)
                    {
                        // Add events to list, Split recurring appointments
                        foreach (var eventItem in result.Items)
                        {
                            if (eventItem.Status == "cancelled")
                            {
                                continue;
                            }

                            var appointment = CreateAppointment(eventItem);
                            finalEventList.Add(appointment);
                        }

                        //If all pages are over break
                        if (result.NextPageToken == null)
                        {
                            break;
                        }

                        //Set the next page to pull from request
                        eventListRequest.PageToken = result.NextPageToken;

                        result = await eventListRequest.ExecuteAsync();
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch (GoogleApiException exception)
            {
                Logger.Error(exception);
                return(null);
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                return(null);
            }

            var calendarAppointments = new CalendarAppointments {
                CalendarId = CalendarId
            };

            calendarAppointments.AddRange(finalEventList);
            return(calendarAppointments);
        }
        public async Task<CalendarAppointments> GetCalendarEventsInRangeAsync(DateTime startDate, DateTime endDate,
            IDictionary<string, object> calendarSpecificData)
        {
            CheckCalendarSpecificData(calendarSpecificData);

            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            var finalEventList = new List<Appointment>();

            Events result = null;

            var eventListRequest = calendarService.Events.List(CalendarId);

            // Add Filters to event List Request
            eventListRequest.TimeMin = startDate;
            eventListRequest.TimeMax = endDate;
            eventListRequest.MaxAttendees = 1000;
            eventListRequest.SingleEvents = true;
            try
            {
                result = eventListRequest.Execute();
                if (result != null)
                {
                    while (result.Items != null)
                    {
                        // Add events to list, Split recurring appointments
                        foreach (var eventItem in result.Items)
                        {
                            if (eventItem.Status == "cancelled")
                            {
                                continue;
                            }

                            var appointment = CreateAppointment(eventItem);
                            finalEventList.Add(appointment);
                        }

                        //If all pages are over break
                        if (result.NextPageToken == null)
                        {
                            break;
                        }

                        //Set the next page to pull from request
                        eventListRequest.PageToken = result.NextPageToken;

                        result = await eventListRequest.ExecuteAsync();
                    }
                }
                else
                {
                    return null;
                }
            }
            catch (GoogleApiException exception)
            {
                Logger.Error(exception);
                return null;
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                return null;
            }

            var calendarAppointments = new CalendarAppointments { CalendarId = CalendarId };
            calendarAppointments.AddRange(finalEventList);
            return calendarAppointments;
        }
        public async Task<CalendarAppointments> AddCalendarEvents(List<Appointment> calendarAppointments,
            bool addDescription,
            bool addReminder, bool addAttendees, bool attendeesToDescription,
            IDictionary<string, object> calendarSpecificData)
        {
            var addedAppointments = new CalendarAppointments();
            if (!calendarAppointments.Any())
            {
                addedAppointments.IsSuccess = true;
                return addedAppointments;
            }

            CheckCalendarSpecificData(calendarSpecificData);

            var errorList = new Dictionary<int, Appointment>();
            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            if (calendarAppointments == null || string.IsNullOrEmpty(CalendarId))
            {
                addedAppointments.IsSuccess = false;
                return addedAppointments;
            }
            try
            {
                if (calendarAppointments.Any())
                {
                    //Split the list of calendarAppointments by 1000 per list
                    var appts =
                        await AddCalendarEventsInternal(calendarAppointments, addDescription, addReminder, addAttendees,
                            attendeesToDescription, calendarService, errorList);
                    addedAppointments.AddRange(appts);
                    if (errorList.Count > 0)
                    {
                        var remaningList = errorList.Select(CreateAppointmentWithoutAttendees).ToList();
                        errorList.Clear();

                        appts = await AddCalendarEventsInternal(remaningList, addDescription, addReminder, addAttendees,
                            attendeesToDescription, calendarService, errorList);
                        addedAppointments.AddRange(appts);
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                addedAppointments.IsSuccess = false;
                return addedAppointments;
            }
            addedAppointments.IsSuccess = true;
            return addedAppointments;
        }