/// <summary>
        /// Process the notification trigger
        /// </summary>
        /// <param name="nextTriggerDetails"></param>
        /// <returns></returns>
        public async Task ProcessNotifications(IAccessoryNotificationTriggerDetails nextTriggerDetails)
        {
            System.Diagnostics.Debug.WriteLine(nextTriggerDetails.AppDisplayName);
            byte[] Cookie = new byte[4];

            bool KeepConnectionAlive = false;
            bool Talking             = false;

            await Connect();

            while (nextTriggerDetails != null || KeepConnectionAlive)
            {
                if (nextTriggerDetails != null)
                {
                    try
                    {
                        AccessoryNotificationType accessoryNotificationType = nextTriggerDetails.AccessoryNotificationType;

                        System.Diagnostics.Debug.WriteLine("Notification: " + accessoryNotificationType.ToString());

                        switch (accessoryNotificationType)
                        {
                        case AccessoryNotificationType.Email:

                            await SendEmailMessage(nextTriggerDetails);

                            break;

                        case AccessoryNotificationType.Toast:

                            await ProcessToastMessage(nextTriggerDetails);

                            break;

                        case AccessoryNotificationType.BatterySaver:

                            await SendBatterySaverMessage(nextTriggerDetails);

                            break;

                        case AccessoryNotificationType.Alarm:

                            await SendAlarmMessage((AlarmNotificationTriggerDetails)nextTriggerDetails);

                            break;

                        case AccessoryNotificationType.CortanaTile:

                            await SendCortanaMessage((CortanaTileNotificationTriggerDetails)nextTriggerDetails);

                            break;

                        case AccessoryNotificationType.Phone:

                            PhoneNotificationTriggerDetails entd = (PhoneNotificationTriggerDetails)nextTriggerDetails;

                            if (entd.PhoneNotificationType != PhoneNotificationType.LineChanged)
                            {
                                System.Diagnostics.Debug.WriteLine("Phone - " + entd.CallDetails.State.ToString());

                                switch (entd.CallDetails.State)
                                {
                                case PhoneCallState.Ringing:

                                    Cookie = BitConverter.GetBytes(_pc.GetNextMessageIdentifier());

                                    await _pc.Pebble.PhoneCallAsync(entd.CallDetails.ContactName, entd.CallDetails.PhoneNumber, Cookie);

                                    KeepConnectionAlive = true;
                                    // _pc.Pebble._protocol.StartRun();

                                    break;

                                case PhoneCallState.Ended:

                                    await _pc.Pebble.EndCallAsync(Cookie);

                                    KeepConnectionAlive = false;

                                    break;

                                case PhoneCallState.Talking:

                                    if (!Talking)
                                    {
                                        await _pc.Pebble.StartCallAsync(Cookie);

                                        Talking = true;
                                    }

                                    break;

                                default:

                                    KeepConnectionAlive = false;

                                    break;
                                }
                            }

                            break;

                        default:

                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine("ProcessNotifications Exception: " + e.Message);
                    }

                    /*if (KeepConnectionAlive)
                     * {
                     *  Task.Delay(500).Wait();
                     *
                     *  P3bble.P3bbleMessage msg = await _pc.Pebble._protocol.ReceiveMessage(0);
                     *
                     *  if (msg != null)
                     *  {
                     *      String abc = msg.Endpoint.ToString();
                     *  }
                     *  else
                     *  {
                     *      System.Diagnostics.Debug.WriteLine("ReceiveMessage: none");
                     *  }
                     * }*/

                    AccessoryManager.ProcessTriggerDetails(nextTriggerDetails);
                }

                nextTriggerDetails = AccessoryManager.GetNextTriggerDetails();
            }

            if (!KeepConnectionAlive)
            {
                Disconnect();
            }

            LastNotifications = DateTime.Now;
        }
        /// <summary>
        /// Construct and send a weather forecast message to the Pebble
        /// </summary>
        /// <param name="MessageNumber"></param>
        /// <param name="Forecast"></param>
        /// <param name="SunsetSunrise"></param>
        /// <param name="Location"></param>
        /// <param name="Time"></param>
        /// <returns></returns>
        private async Task SendWeatherMessage(int MessageNumber, JsonObject Forecast, String SunsetSunrise, String Location, DateTime Time)
        {
            try
            {
                double     TempMin = 0;
                double     TempMax = 0;
                IJsonValue jvTempMin;
                IJsonValue jvTempMax;

                //Send Sunrise to Pebble
                jvTempMin = Forecast.GetObject()["temp"];
                if (jvTempMin.ValueType != JsonValueType.Null)
                {
                    TempMin = jvTempMin.GetNumber();
                }

                jvTempMax = Forecast.GetObject()["hi"];
                if (jvTempMax.ValueType != JsonValueType.Null)
                {
                    TempMax = jvTempMax.GetNumber();
                }

                P3bble.Messages.TimeLineWeatherMessage _tlwm = new P3bble.Messages.TimeLineWeatherMessage(_PebbleConnector.GetNextMessageIdentifier(),
                                                                                                          MessageNumber,
                                                                                                          ConvertWeatherType2Icon((int)Forecast.GetObject()["icon_code"].GetNumber()),
                                                                                                          (int)TempMin,
                                                                                                          (int)TempMax,
                                                                                                          SunsetSunrise,
                                                                                                          Forecast.GetObject()["narrative"].GetString(),
                                                                                                          Location,
                                                                                                          Time);

                _tlwm.ToBuffer();
                await _PebbleConnector.Pebble.WriteTimeLineCalenderAsync(_tlwm);

                Log.Add(String.Format("{0}: {1}", SunsetSunrise, Time.ToString()));
            }
            catch (Exception e)
            {
                Log.Add(String.Format("An error occured with {0}: {1}", SunsetSunrise, Time.ToString()));
            }
        }
        /// <summary>
        /// Add the calender item to the pebble
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Description"></param>
        /// <param name="Location"></param>
        /// <param name="Time"></param>
        /// <param name="Duration"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        private async Task <bool> AddCalenderItem(String ID, String Description, String Location, DateTime Time, int Duration, String Details, TimeSpan?Reminder)
        {
            try
            {
                //Create a Guid or select the present one
                Guid MessageId = Guid.NewGuid();
                System.Diagnostics.Debug.WriteLine(String.Format("Roaming ID: {0}", ID));
                System.Diagnostics.Debug.WriteLine(String.Format("New guid: {0}", MessageId.ToString()));

                foreach (var item in  PreviousSynchronizedItems.Where(x => x.RoamingID == ID))
                {
                    System.Diagnostics.Debug.WriteLine(String.Format("Old guid: {0}", item.CalenderItemID));

                    MessageId           = Guid.Parse(item.CalenderItemID);
                    item.CalenderItemID = "";
                }

                //Add item to list of synchronized/send items
                CalenderItem _newItem = new CalenderItem()
                {
                    RoamingID = ID, Time = Time, CalenderItemID = MessageId.ToString(), ReminderID = ""
                };
                SynchronizedItems.Add(_newItem);

                //Send item to Pebble
                P3bble.Messages.TimeLineCalenderMessage _tlcm = new P3bble.Messages.TimeLineCalenderMessage(_PebbleConnector.GetNextMessageIdentifier(), MessageId, Description, Location, Time, Duration, Details);
                _tlcm.ToBuffer();
                await _PebbleConnector.Pebble.WriteTimeLineCalenderAsync(_tlcm);

                //Send reminder
                if (Reminders && Reminder.HasValue)
                {
                    await AddCalenderItemReminder(
                        _newItem,
                        ID,
                        MessageId,
                        Description,
                        Location,
                        Time,
                        Reminder.Value,
                        Details);

                    Log.Add("Reminder: " + Reminder);
                }

                return(true);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception: " + Description);
            }

            return(false);
        }