/// <summary>
        /// </summary>
        /// <param name="syncMetric"></param>
        /// <param name="accountName"></param>
        /// <returns>
        /// </returns>
        public async Task<bool> UploadSyncData(SyncMetric syncMetric, string accountName)
        {
            try
            {
                var analyticsService = new AnalyticsService(new BaseClientService.Initializer
                {
                    ApplicationName = ApplicationInfo.ProductName,
                    ApiKey = "AIzaSyBrpqcL6Nh1vVecfhIbxGVnyGHMZ8-aH6k"
                });
                var batchRequest = new BatchRequest(analyticsService);
                var metric = new CustomMetric
                {
                    Name = "SyncMetric",
                    Kind = "string"
                };

                var insertRequest = analyticsService.Management.CustomMetrics.Insert(metric, "", "");
                batchRequest.Queue<CustomMetric>(insertRequest, InsertMetricCallback);
                await batchRequest.ExecuteAsync();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return false;
            }
            return true;
        }
        public string SyncNow(CalendarSyncProfile syncProfile, SyncMetric syncMetric, SyncCallback syncCallback)
        {
            try
            {
                if (syncProfile.GoogleAccount == null || syncProfile.GoogleAccount.GoogleCalendar == null ||
                    !syncProfile.ValidateOutlookSettings())
                {
                    _messageService.ShowMessageAsync(
                        "Please configure Google and Outlook calendar in settings to continue.");
                    return "Invalid Settings";
                }
                ResetSyncData();

                var isSyncComplete = _calendarUpdateService.SyncCalendar(syncProfile, syncMetric, syncCallback);
                return isSyncComplete ? null : "Error Occurred";
            }
            catch (AggregateException exception)
            {
                var flattenException = exception.Flatten();
                _messageService.ShowMessageAsync(flattenException.Message);
                _applicationLogger.Error(exception);
                return flattenException.Message;
            }
            catch (Exception exception)
            {
                _messageService.ShowMessageAsync(exception.Message);
                _applicationLogger.Error(exception);
                return exception.Message;
            }
        }
        private void UploadAnalyticsData(CalendarSyncProfile syncProfile, bool isSuccess)
        {
            var syncMetric = new SyncMetric
            {
                IsSuccess = isSuccess,
            };

            AnalyticsService.UploadSyncData(syncMetric, syncProfile.GoogleAccount.Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="syncProfile"></param>
        /// <param name="syncMetric"></param>
        /// <param name="sourceCalendarSpecificData"></param>
        /// <param name="destinationCalendarSpecificData"></param>
        /// <returns></returns>
        private bool UpdateEntries(CalendarSyncProfile syncProfile, SyncMetric syncMetric,
            IDictionary<string, object> sourceCalendarSpecificData,
            IDictionary<string, object> destinationCalendarSpecificData)
        {
            var isSuccess = true;
            if (CalendarSyncEngine.SourceAppointmentsToUpdate.Any())
            {
                SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
                //Update status for reading entries to update
                SyncStatus = StatusHelper.GetMessage(SyncStateEnum.EntriesToUpdate,
                    CalendarSyncEngine.SourceAppointmentsToUpdate.Count,
                    SourceCalendarService.CalendarServiceName);
                var updatedAppointments = SourceCalendarService.UpdateCalendarEvents(CalendarSyncEngine.SourceAppointmentsToUpdate,
                    syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Description),
                    syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Reminders),
                    syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Attendees),
                    syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.AttendeesToDescription),
                    sourceCalendarSpecificData).Result;
                isSuccess = updatedAppointments.IsSuccess;
                SyncStatus =
                    StatusHelper.GetMessage(isSuccess
                        ? SyncStateEnum.UpdateEntriesSuccess
                        : SyncStateEnum.UpdateEntriesFailed);
                SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
                syncMetric.SourceMetric.UpdateCount = CalendarSyncEngine.SourceAppointmentsToUpdate.Count;
                syncMetric.SourceMetric.UpdateFailedCount =
                    CalendarSyncEngine.SourceAppointmentsToUpdate.Count - updatedAppointments.Count;
            }

            if (CalendarSyncEngine.DestAppointmentsToUpdate.Any())
            {
                SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
                //Update status for reading entries to update
                SyncStatus = StatusHelper.GetMessage(SyncStateEnum.EntriesToUpdate,
                    CalendarSyncEngine.DestAppointmentsToUpdate.Count,
                    DestinationCalendarService.CalendarServiceName);
                var updatedAppointments = DestinationCalendarService.UpdateCalendarEvents(CalendarSyncEngine.DestAppointmentsToUpdate,
                    syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Description),
                    syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Reminders),
                    syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Attendees),
                    syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.AttendeesToDescription),
                    destinationCalendarSpecificData).Result;
                isSuccess = updatedAppointments.IsSuccess;
                SyncStatus =
                    StatusHelper.GetMessage(isSuccess
                        ? SyncStateEnum.UpdateEntriesSuccess
                        : SyncStateEnum.UpdateEntriesFailed);
                SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
                syncMetric.DestMetric.UpdateCount = CalendarSyncEngine.DestAppointmentsToUpdate.Count;
                syncMetric.DestMetric.UpdateFailedCount =
                    CalendarSyncEngine.DestAppointmentsToUpdate.Count - updatedAppointments.Count;
            }

            return isSuccess;
        }
        /// <summary>
        ///     Delete appointments from source
        /// </summary>
        /// <param name="syncProfile"></param>
        /// <param name="sourceCalendarSpecificData"></param>
        /// <param name="syncCallback"></param>
        /// <returns>
        /// </returns>
        private bool DeleteSourceAppointments(CalendarSyncProfile syncProfile, SyncMetric syncMetric,
            IDictionary<string, object> sourceCalendarSpecificData, SyncCallback syncCallback)
        {
            if (syncProfile.SyncSettings.DisableDelete)
            {
                return true;
            }
            //Updating entry isDeleteOperation status
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.ReadingEntriesToDelete,
                SourceCalendarService.CalendarServiceName);
            //Getting appointments to isDeleteOperation
            CalendarSyncEngine.GetSourceEntriesToDelete(syncProfile, SourceAppointments, DestinationAppointments);
            var appointmentsToDelete = CalendarSyncEngine.SourceAppointmentsToDelete;
            //Updating Get entry isDeleteOperation status
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.EntriesToDelete, appointmentsToDelete.Count);
            if (appointmentsToDelete.Count == 0)
            {
                SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
                return true;
            }

            //Updating isDeleteOperation status
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.DeletingEntries,
                SourceCalendarService.CalendarServiceName);
            //Deleting entries
            var deletedAppointments =
                SourceCalendarService.DeleteCalendarEvents(appointmentsToDelete, sourceCalendarSpecificData).Result;
            var isSuccess = deletedAppointments.IsSuccess;
            //Update status if entries were successfully deleted
            SyncStatus =
                StatusHelper.GetMessage(isSuccess
                    ? SyncStateEnum.DeletingEntriesComplete
                    : SyncStateEnum.DeletingEntriesFailed);
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);

            if (isSuccess)
            {
                syncMetric.SourceMetric.DeleteCount = appointmentsToDelete.Count;
                syncMetric.SourceMetric.DeleteFailedCount = appointmentsToDelete.Count - deletedAppointments.Count;
                for (var index = 0; index < appointmentsToDelete.Count; index++)
                {
                    SourceAppointments.Remove(appointmentsToDelete[index]);
                }
            }
            return isSuccess;
        }
        /// <summary>
        ///     Delete appointments in destination
        /// </summary>
        /// <param name="syncProfile"></param>
        /// <param name="syncMetric"></param>
        /// <param name="destinationCalendarSpecificData"></param>
        /// <param name="syncCallback"></param>
        /// <returns>
        /// </returns>
        private bool DeleteDestinationAppointments(CalendarSyncProfile syncProfile, SyncMetric syncMetric,
            IDictionary<string, object> destinationCalendarSpecificData, SyncCallback syncCallback)
        {
            if (syncProfile.SyncSettings.DisableDelete)
            {
                return true;
            }
            //Updating entry isDeleteOperation status
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.ReadingEntriesToDelete,
                DestinationCalendarService.CalendarServiceName);
            //Getting appointments to isDeleteOperation
            CalendarSyncEngine.GetDestEntriesToDelete(syncProfile,
                SourceAppointments, DestinationAppointments);
            var appointmentsToDelete = CalendarSyncEngine.DestAppointmentsToDelete;

            if (syncProfile.SyncSettings.SyncMode == SyncModeEnum.OneWay)
            {
                if (syncProfile.SyncSettings.ConfirmOnDelete && syncCallback != null)
                {
                    var orphanEntries = Environment.NewLine +
                                        string.Join(Environment.NewLine, CalendarSyncEngine.DestOrphanEntries);
                    //Log Orphan Entries
                    ApplicationLogger.Warn("Orphan entries to delete: " + orphanEntries);

                    var message = string.Format("Are you sure you want to delete {0} orphan entries from {1}?{2}",
                        appointmentsToDelete.Count, DestinationCalendarService.CalendarServiceName,
                        orphanEntries);
                    var e = new SyncEventArgs(message, UserActionEnum.ConfirmDelete);

                    var task = syncCallback(e);
                    if (task.Result)
                    {
                        appointmentsToDelete.AddRange(CalendarSyncEngine.DestOrphanEntries);
                    }
                }
                else if (!syncProfile.SyncSettings.DisableDelete)
                {
                    appointmentsToDelete.AddRange(CalendarSyncEngine.DestOrphanEntries);
                }
            }

            //Updating Get entry isDeleteOperation status
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.EntriesToDelete, appointmentsToDelete.Count);

            if (appointmentsToDelete.Count == 0)
            {
                SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
                return true;
            }

            //Updating isDeleteOperation status
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.DeletingEntries,
                DestinationCalendarService.CalendarServiceName);

            //Deleting entries

            var deletedAppointments = DestinationCalendarService.DeleteCalendarEvents(appointmentsToDelete, destinationCalendarSpecificData)
                    .Result;
            var isSuccess = deletedAppointments.IsSuccess;
            //Update status if entries were successfully deleted
            SyncStatus =
                StatusHelper.GetMessage(isSuccess
                    ? SyncStateEnum.DeletingEntriesComplete
                    : SyncStateEnum.DeletingEntriesFailed);
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
            if (isSuccess)
            {
                syncMetric.DestMetric.DeleteCount = appointmentsToDelete.Count;
                syncMetric.DestMetric.DeleteFailedCount = appointmentsToDelete.Count - deletedAppointments.Count;
                for (var index = 0; index < appointmentsToDelete.Count; index++)
                {
                    DestinationAppointments.Remove(appointmentsToDelete[index]);
                }
            }

            return isSuccess;
        }
        /// <summary>
        ///     Add appointments to source
        /// </summary>
        /// <param name="syncProfile"></param>
        /// <param name="syncMetric"></param>
        /// <param name="sourceCalendarSpecificData"></param>
        /// <returns>
        /// </returns>
        private bool AddSourceAppointments(CalendarSyncProfile syncProfile, SyncMetric syncMetric,
            IDictionary<string, object> sourceCalendarSpecificData)
        {
            //Update status for reading entries to add
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.ReadingEntriesToAdd,
                SourceCalendarService.CalendarServiceName);
            //Get entries to add
            CalendarSyncEngine.GetSourceEntriesToAdd(syncProfile, SourceAppointments, DestinationAppointments);
            var appointmentsToAdd = CalendarSyncEngine.SourceAppointmentsToAdd;
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.EntriesToAdd, appointmentsToAdd.Count);
            if (appointmentsToAdd.Count == 0)
            {
                SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
                return true;
            }
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.AddingEntries, SourceCalendarService.CalendarServiceName);

            //Add entries to calendar
            var addedAppointments = SourceCalendarService.AddCalendarEvents(appointmentsToAdd,
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Description),
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Reminders),
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Attendees),
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.AttendeesToDescription),
                sourceCalendarSpecificData)
                .Result;
            var isSuccess = addedAppointments.IsSuccess;
            //Update status if entries were successfully added
            SyncStatus =
                StatusHelper.GetMessage(isSuccess ? SyncStateEnum.AddEntriesComplete : SyncStateEnum.AddEntriesFailed);
            SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);

            if (isSuccess)
            {
                syncMetric.SourceMetric.AddCount = appointmentsToAdd.Count;
                LoadSourceId(addedAppointments, DestinationAppointments.CalendarId);
                SourceAppointments.AddRange(addedAppointments);
                if (syncProfile.SyncSettings.SyncMode == SyncModeEnum.TwoWay)
                {
                    var updateDestList = UpdateWithChildId(addedAppointments, DestinationAppointments);
                    CalendarSyncEngine.DestAppointmentsToUpdate.AddRangeCompareForUpdate(updateDestList);
                }
            }

            return isSuccess;
        }
        public bool SyncCalendar(CalendarSyncProfile syncProfile, SyncMetric syncMetric, SyncCallback syncCallback)
        {
            InitiatePreSyncSetup(syncProfile);

            var isSuccess = false;
            if (syncProfile != null)
            {
                CalendarSyncEngine.Clear();
                //Add log for sync mode
                SyncStatus = string.Format("Calendar Sync : {0} {2} {1}", SourceCalendarService.CalendarServiceName,
                    DestinationCalendarService.CalendarServiceName,
                    syncProfile.SyncSettings.SyncMode == SyncModeEnum.TwoWay ? "<===>" : "===>");
                SyncStatus = StatusHelper.GetMessage(SyncStateEnum.Line);
                DateTime startDate, endDate;
                GetDateRange(syncProfile, out startDate, out endDate);
                //Add log for date range
                SyncStatus = string.Format("Date Range : {0} - {1}",
                    startDate.ToString("d"),
                    endDate.ToString("d"));

                //Load calendar specific data
                var sourceCalendarSpecificData =
                    GetCalendarSpecificData(syncProfile.SyncSettings.SourceCalendar, syncProfile);
                var destinationCalendarSpecificData =
                    GetCalendarSpecificData(syncProfile.SyncSettings.DestinationCalendar, syncProfile);

                //Get source and destination appointments
                isSuccess = LoadAppointments(startDate, endDate,
                    sourceCalendarSpecificData,
                    destinationCalendarSpecificData);

                if (isSuccess)
                {
                    syncMetric.SourceMetric.OriginalCount = SourceAppointments.Count;
                    syncMetric.DestMetric.OriginalCount = DestinationAppointments.Count;
                    LoadSourceId(DestinationAppointments, SourceAppointments.CalendarId);
                    LoadSourceId(SourceAppointments, DestinationAppointments.CalendarId);
                }

                if (isSuccess)
                {
                    //Delete destination appointments
                    isSuccess = DeleteDestinationAppointments(syncProfile, syncMetric, destinationCalendarSpecificData, syncCallback);
                }

                if (isSuccess)
                {
                    //Add appointments to destination
                    isSuccess = AddDestinationAppointments(syncProfile, syncMetric, destinationCalendarSpecificData);
                }

                if (isSuccess && syncProfile.SyncSettings.SyncMode == SyncModeEnum.TwoWay)
                {
                    //Delete destination appointments
                    isSuccess = DeleteSourceAppointments(syncProfile, syncMetric, sourceCalendarSpecificData, syncCallback);
                    if (isSuccess)
                    {
                        //If sync mode is two way... add events to source
                        isSuccess = AddSourceAppointments(syncProfile, syncMetric, sourceCalendarSpecificData);
                    }
                }

                if (isSuccess)
                {
                    isSuccess = UpdateEntries(syncProfile, syncMetric, sourceCalendarSpecificData, destinationCalendarSpecificData);
                }
            }
            syncMetric.IsSuccess = isSuccess;
            SourceAppointments = null;
            DestinationAppointments = null;
            SourceCalendarService = null;
            DestinationCalendarService = null;
            return isSuccess;
        }