string ParseMessageText(UcwaResourceMessage msgRes)
 {
     if (msgRes.HtmlMessage != null)
     {
         return(Windows.Data.Html.HtmlUtilities.ConvertToText(
                    WebUtility.UrlDecode(msgRes.HtmlMessage)));
     }
     return(WebUtility.UrlDecode(msgRes.PlainMessage));
 }
        async void ProcessConversationEvents(IEnumerable <UcwaEvent> events)
        {
            this.ShowProcessProgress("\r\nevents href='" + events.First().EventsHref);
            foreach (var e in events)
            {
                this.ShowProcessProgress(e.Name + e.Type + " event by " + e.Sender + ":");
                switch (e.Name)
                {
                case "message":
                    var msgRes = new UcwaResourceMessage(e);
                    ShowProcessProgress("\tDirection:" + msgRes.Direction + "\r\n\tparticipantUri:" + msgRes.participantResourceUri);
                    if (msgRes.Direction.ToLower() == "incoming")
                    {
                        var status = msgRes.Status;
                        if (!participantNames.ContainsKey(msgRes.participantResourceUri))
                        {
                            var dn = await GetContactName(msgRes.contactResourceUri);

                            if (!string.IsNullOrEmpty(dn))
                            {
                                participantNames.Add(msgRes.participantResourceUri, dn);
                            }
                        }
                        var      participantName = participantNames[msgRes.participantResourceUri];
                        string   timeStamp;
                        DateTime dateTimeStamp;
                        if (DateTime.TryParse(msgRes.TimeStamp, out dateTimeStamp))
                        {
                            timeStamp = dateTimeStamp.ToString();
                        }
                        else
                        {
                            timeStamp = "";
                        }
                        string msgText = ParseMessageText(msgRes);
                        NotifyReceivedMessage(status, timeStamp, msgText, participantName);
                    }
                    break;

                case "participant":
                    ShowProcessProgress(e.Type + " participant, " + e.Uri);

                    if (e.Type.ToLower() == "deleted")
                    {
                        if (participantNames.ContainsKey(e.Uri))
                        {
                            participantNames.Remove(e.Uri);
                        }
                        else
                        {
                            ShowProcessProgress("\tNot in cache.");
                        }
                    }
                    else
                    {
                        if (!participantNames.ContainsKey(e.Uri))
                        {
                            var pName = await GetParticipantName(e.Uri);

                            if (!string.IsNullOrEmpty(pName) && !participantNames.ContainsKey(e.Uri))
                            {
                                participantNames.Add(e.Uri, pName);
                            }
                            else
                            {
                                ShowProcessProgress("\tNot added to cache. " + pName);
                            }
                        }
                    }
                    break;

                case "messaging":
                    messaging = new UcwaResourceMessaging(e);
                    NotifyResourceStateChange(messaging.State.ToLower(), messaging.Name);

                    ShowProcessProgress("convUri:" + messaging.conversationUri
                                        + "\r\n\tsendMessageUri:" + messaging.sendMessageUri + "\r\n\tsetIsTypingUri:" + messaging.setIsTypingUri
                                        + "\r\n\ttypingParticipantUri:" + messaging.typingParticipantsUri + "\r\n\tstopMessagingUri:" + messaging.stopMessagingUri
                                        + "\r\n\tmessagingUri:" + messaging.Uri + "\r\n\tState:" + messaging.State
                                        );

                    if (messaging.State.ToLower() == "connected")
                    {
                        var result = await this.ucwaApp.Transport.GetResourceAsync(messaging.conversationUri);

                        if (result.StatusCode == HttpStatusCode.OK)
                        {
                            this.conversation = new UcwaResourceConversation(result.Resource);
                            NotifyResourceStateChange(conversation.State.ToLower(), conversation.Name);
                        }
                        else
                        {
                            this.ShowProcessProgress("Failed to GET conversation after a successful messaging invitation: " + result.Exception.Message);
                        }
                    }
                    break;

                default:
                    break;
                }
            }
        }