public void ProcessRepliesAndAudio(IList <TLMessageBase> messages)
        {
            for (var i = 0; i < messages.Count; i++)
            {
                var message = messages[i] as TLMessage;
                if (message != null)
                {
                    var mediaAudio = message.Media as TLMessageMediaAudio;
                    if (mediaAudio != null)
                    {
                        var audio = mediaAudio.Audio as TLAudio;
                        if (audio == null)
                        {
                            return;
                        }

                        var store         = IsolatedStorageFile.GetUserStoreForApplication();
                        var audioFileName = audio.GetFileName();
                        if (TLString.Equals(audio.MimeType, new TLString("audio/mpeg"), StringComparison.OrdinalIgnoreCase))
                        {
                            if (!store.FileExists(audioFileName))
                            {
                                mediaAudio.IsCanceled          = false;
                                mediaAudio.DownloadingProgress = mediaAudio.LastProgress > 0.0 ? mediaAudio.LastProgress : 0.001;
                                BeginOnThreadPool(() =>
                                {
                                    DownloadAudioFileManager.DownloadFile(audio.DCId, audio.ToInputFileLocation(), message, audio.Size);
                                });
                                continue;
                            }
                        }

                        var wavFileName = Path.GetFileNameWithoutExtension(audioFileName) + ".wav";

                        if (!store.FileExists(wavFileName))
                        {
                            mediaAudio.IsCanceled          = false;
                            mediaAudio.DownloadingProgress = mediaAudio.LastProgress > 0.0 ? mediaAudio.LastProgress : 0.001;
                            BeginOnThreadPool(() =>
                            {
                                DownloadAudioFileManager.DownloadFile(audio.DCId, audio.ToInputFileLocation(), message, audio.Size);
                            });
                            continue;
                        }
                    }
                }
            }

            var replyToMsgIds = new TLVector <TLInt>();
            var replyToMsgs   = new List <TLMessage25>();

            for (var i = 0; i < messages.Count; i++)
            {
                var message25 = messages[i] as TLMessage25;
                if (message25 != null)
                {
                    if (message25.ReplyToMsgId != null)
                    {
                        var replyToMsgId = message25.ReplyToMsgId;
                        if (replyToMsgId != null &&
                            replyToMsgId.Value != 0)
                        {
                            TLInt channelId   = null;
                            var   peerChannel = message25.ToId as TLPeerChannel;
                            if (peerChannel != null)
                            {
                                channelId = peerChannel.Id;
                            }

                            var reply = CacheService.GetMessage(replyToMsgId, channelId);
                            if (reply != null)
                            {
                                messages[i].Reply = reply;
                            }
                            else
                            {
                                replyToMsgIds.Add(replyToMsgId);
                                replyToMsgs.Add(message25);
                            }
                        }
                    }

                    if (message25.NotListened)
                    {
                        if (message25.Media != null)
                        {
                            message25.Media.NotListened = true;
                        }
                    }
                }
            }

            if (replyToMsgIds.Count > 0)
            {
                var channel = With as TLChannel;
                if (channel != null)
                {
                    var firstReplyToMsg = replyToMsgs.FirstOrDefault();
                    var peerChat        = firstReplyToMsg != null ? firstReplyToMsg.ToId as TLPeerChat : null;
                    if (peerChat != null)
                    {
                        MTProtoService.GetMessagesAsync(
                            replyToMsgIds,
                            result =>
                        {
                            CacheService.AddChats(result.Chats, results => { });
                            CacheService.AddUsers(result.Users, results => { });

                            for (var i = 0; i < result.Messages.Count; i++)
                            {
                                for (var j = 0; j < replyToMsgs.Count; j++)
                                {
                                    var messageToReply = replyToMsgs[j];
                                    if (messageToReply != null &&
                                        messageToReply.ReplyToMsgId.Value == result.Messages[i].Index)
                                    {
                                        replyToMsgs[j].Reply = result.Messages[i];
                                    }
                                }
                            }

                            //Execute.BeginOnUIThread(() =>
                            //{
                            for (var i = 0; i < replyToMsgs.Count; i++)
                            {
                                replyToMsgs[i].NotifyOfPropertyChange(() => replyToMsgs[i].ReplyInfo);
                            }
                            //});
                        },
                            error =>
                        {
                            Execute.ShowDebugMessage("messages.getMessages error " + error);
                        });
                    }
                    else
                    {
                        MTProtoService.GetMessagesAsync(
                            channel.ToInputChannel(),
                            replyToMsgIds,
                            result =>
                        {
                            CacheService.AddChats(result.Chats, results => { });
                            CacheService.AddUsers(result.Users, results => { });

                            for (var i = 0; i < result.Messages.Count; i++)
                            {
                                for (var j = 0; j < replyToMsgs.Count; j++)
                                {
                                    var messageToReply = replyToMsgs[j];
                                    if (messageToReply != null &&
                                        messageToReply.ReplyToMsgId.Value == result.Messages[i].Index)
                                    {
                                        replyToMsgs[j].Reply = result.Messages[i];
                                    }
                                }
                            }

                            //Execute.BeginOnUIThread(() =>
                            //{
                            for (var i = 0; i < replyToMsgs.Count; i++)
                            {
                                replyToMsgs[i].NotifyOfPropertyChange(() => replyToMsgs[i].ReplyInfo);
                            }
                            //});
                        },
                            error =>
                        {
                            Execute.ShowDebugMessage("channels.getMessages error " + error);
                        });
                    }
                }
                else
                {
                    MTProtoService.GetMessagesAsync(
                        replyToMsgIds,
                        result =>
                    {
                        CacheService.AddChats(result.Chats, results => { });
                        CacheService.AddUsers(result.Users, results => { });

                        for (var i = 0; i < result.Messages.Count; i++)
                        {
                            for (var j = 0; j < replyToMsgs.Count; j++)
                            {
                                var messageToReply = replyToMsgs[j];
                                if (messageToReply != null &&
                                    messageToReply.ReplyToMsgId.Value == result.Messages[i].Index)
                                {
                                    replyToMsgs[j].Reply = result.Messages[i];
                                }
                            }
                        }

                        //Execute.BeginOnUIThread(() =>
                        //{
                        for (var i = 0; i < replyToMsgs.Count; i++)
                        {
                            replyToMsgs[i].NotifyOfPropertyChange(() => replyToMsgs[i].ReplyInfo);
                        }
                        //});
                    },
                        error =>
                    {
                        Execute.ShowDebugMessage("messages.getMessages error " + error);
                    });
                }
            }
        }
        public void OpenMedia(TLMessageBase messageBase)
#endif
        {
            if (messageBase == null)
            {
                return;
            }
            if (messageBase.Status == MessageStatus.Failed ||
                messageBase.Status == MessageStatus.Sending)
            {
                return;
            }

            var serviceMessage = messageBase as TLMessageService;

            if (serviceMessage != null)
            {
                var editPhotoAction = serviceMessage.Action as TLMessageActionChatEditPhoto;
                if (editPhotoAction != null)
                {
                    var photo = editPhotoAction.Photo;
                    if (photo != null)
                    {
                        StateService.CurrentPhoto = photo;
                        NavigationService.UriFor <ProfilePhotoViewerViewModel>().Navigate();
                    }
                }

                return;
            }

            var message = messageBase as TLMessage;

            if (message == null)
            {
                return;
            }

            var mediaPhoto = message.Media as TLMessageMediaPhoto;

            if (mediaPhoto != null)
            {
                StateService.CurrentPhotoMessage  = message;
                StateService.CurrentMediaMessages =
                    Items
                    .OfType <TLMessage>()
                    .Where(x => x.Media is TLMessageMediaPhoto || x.Media is TLMessageMediaVideo)
                    .ToList();

                OpenImageViewer();

                return;
            }

            var mediaWebPage = message.Media as TLMessageMediaWebPage;

            if (mediaWebPage != null)
            {
                var webPage = mediaWebPage.WebPage as TLWebPage;
                if (webPage != null && webPage.Type != null)
                {
                    if (webPage.Type != null)
                    {
                        var type = webPage.Type.ToString();
                        if (string.Equals(type, "photo", StringComparison.OrdinalIgnoreCase))
                        {
                            StateService.CurrentPhotoMessage = message;

                            OpenImageViewer();

                            return;
                        }
                        if (string.Equals(type, "video", StringComparison.OrdinalIgnoreCase))
                        {
                            if (!TLString.Equals(webPage.SiteName, new TLString("youtube"), StringComparison.OrdinalIgnoreCase) &&
                                !TLString.IsNullOrEmpty(webPage.EmbedUrl))
                            {
                                var launcher = new MediaPlayerLauncher
                                {
                                    Location    = MediaLocationType.Data,
                                    Media       = new Uri(webPage.EmbedUrl.ToString(), UriKind.Absolute),
                                    Orientation = MediaPlayerOrientation.Portrait
                                };
                                launcher.Show();
                                return;
                            }

                            if (!TLString.IsNullOrEmpty(webPage.Url))
                            {
                                var webBrowserTask = new WebBrowserTask();
                                webBrowserTask.Uri = new Uri(webPage.Url.ToString(), UriKind.Absolute);
                                webBrowserTask.Show();
                            }

                            return;
                        }
                    }
                }
            }

            var mediaGeo = message.Media as TLMessageMediaGeo;

            if (mediaGeo != null)
            {
                StateService.MediaMessage = message;
                NavigationService.UriFor <MapViewModel>().Navigate();
                return;
            }

            var mediaContact = message.Media as TLMessageMediaContact;

            if (mediaContact != null)
            {
                var phoneNumber = mediaContact.PhoneNumber.ToString();

                if (mediaContact.UserId.Value == 0)
                {
                    if (!string.IsNullOrEmpty(phoneNumber))
                    {
                        if (!phoneNumber.StartsWith("+"))
                        {
                            phoneNumber = "+" + phoneNumber;
                        }

                        var task = new PhoneCallTask();
                        task.DisplayName = mediaContact.FullName;
                        task.PhoneNumber = phoneNumber;
                        task.Show();
                    }
                }
                else
                {
                    var user = mediaContact.User;

                    OpenMediaContact(mediaContact.UserId, user, new TLString(phoneNumber));
                }
                return;
            }

            var mediaVideo = message.Media as TLMessageMediaVideo;

            if (mediaVideo != null)
            {
                var video = mediaVideo.Video as TLVideo;
                if (video == null)
                {
                    return;
                }

                var videoFileName = video.GetFileName();
                var store         = IsolatedStorageFile.GetUserStoreForApplication();
#if WP81
                var file = mediaVideo.File ?? await GetStorageFile(mediaVideo);
#endif


                if (!store.FileExists(videoFileName)
#if WP81
                    && file == null
#endif
                    )
                {
                    mediaVideo.IsCanceled          = false;
                    mediaVideo.DownloadingProgress = mediaVideo.LastProgress > 0.0? mediaVideo.LastProgress : 0.001;
                    DownloadVideoFileManager.DownloadFileAsync(
                        video.DCId, video.ToInputFileLocation(), message, video.Size,
                        progress =>
                    {
                        if (progress > 0.0)
                        {
                            mediaVideo.DownloadingProgress = progress;
                        }
                    });
                }
                else
                {
                    //ReadMessageContents(message);

#if WP81
                    //var localFile = await GetFileFromLocalFolder(videoFileName);
                    var videoProperties = await file.Properties.GetVideoPropertiesAsync();

                    var musicProperties = await file.Properties.GetMusicPropertiesAsync();

                    if (file != null)
                    {
                        Launcher.LaunchFileAsync(file);
                        return;
                    }
#endif

                    var launcher = new MediaPlayerLauncher
                    {
                        Location = MediaLocationType.Data,
                        Media    = new Uri(videoFileName, UriKind.Relative)
                    };
                    launcher.Show();
                }
                return;
            }

            var mediaAudio = message.Media as TLMessageMediaAudio;
            if (mediaAudio != null)
            {
                var audio = mediaAudio.Audio as TLAudio;
                if (audio == null)
                {
                    return;
                }

                var store         = IsolatedStorageFile.GetUserStoreForApplication();
                var audioFileName = audio.GetFileName();
                if (TLString.Equals(audio.MimeType, new TLString("audio/mpeg"), StringComparison.OrdinalIgnoreCase))
                {
                    if (!store.FileExists(audioFileName))
                    {
                        mediaAudio.IsCanceled          = false;
                        mediaAudio.DownloadingProgress = mediaAudio.LastProgress > 0.0 ? mediaAudio.LastProgress : 0.001;
                        BeginOnThreadPool(() =>
                        {
                            DownloadAudioFileManager.DownloadFile(audio.DCId, audio.ToInputFileLocation(), message, audio.Size);
                        });
                    }
                    else
                    {
                        ReadMessageContents(message as TLMessage25);
                    }

                    return;
                }

                var wavFileName = Path.GetFileNameWithoutExtension(audioFileName) + ".wav";

                if (!store.FileExists(wavFileName))
                {
                    mediaAudio.IsCanceled          = false;
                    mediaAudio.DownloadingProgress = mediaAudio.LastProgress > 0.0 ? mediaAudio.LastProgress : 0.001;
                    BeginOnThreadPool(() =>
                    {
                        DownloadAudioFileManager.DownloadFile(audio.DCId, audio.ToInputFileLocation(), message, audio.Size);
                    });
                }
                else
                {
                    ReadMessageContents(message as TLMessage25);
                }

                return;
            }

            var mediaDocument = message.Media as TLMessageMediaDocument;
            if (mediaDocument != null)
            {
                OpenDocumentCommon(message, StateService, DownloadDocumentFileManager,
                                   () =>
                {
                    StateService.CurrentPhotoMessage = message;

                    if (AnimatedImageViewer == null)
                    {
                        AnimatedImageViewer = new AnimatedImageViewerViewModel(StateService);
                        NotifyOfPropertyChange(() => AnimatedImageViewer);
                    }

                    Execute.BeginOnUIThread(() => AnimatedImageViewer.OpenViewer());
                });

                return;
            }

            Execute.ShowDebugMessage("tap on media");
        }
        public void OpenMedia(TLMessageBase messageBase)
#endif
        {
            if (messageBase == null)
            {
                return;
            }
            if (messageBase.Status == MessageStatus.Failed ||
                messageBase.Status == MessageStatus.Sending)
            {
                return;
            }

            var serviceMessage = messageBase as TLMessageService;

            if (serviceMessage != null)
            {
                var editPhotoAction = serviceMessage.Action as TLMessageActionChatEditPhoto;
                if (editPhotoAction != null)
                {
                    var photo = editPhotoAction.Photo;
                    if (photo != null)
                    {
                        //StateService.CurrentPhoto = photo;
                        //StateService.CurrentChat = With as TLChatBase;
                        //NavigationService.UriFor<ProfilePhotoViewerViewModel>().Navigate();
                    }
                }

                var phoneCallAction = serviceMessage.Action as TLMessageActionPhoneCall;
                if (phoneCallAction != null)
                {
                    TLUser user = null;
                    if (serviceMessage.Out.Value)
                    {
                        user = CacheService.GetUser(serviceMessage.ToId.Id) as TLUser;
                    }
                    else
                    {
                        user = serviceMessage.From as TLUser;
                    }

                    ShellViewModel.StartVoiceCall(user, IoC.Get <IVoIPService>(), IoC.Get <ICacheService>());
                }

                return;
            }

            var message = messageBase as TLMessage;

            if (message == null)
            {
                return;
            }

            var mediaPhoto = message.Media as TLMessageMediaPhoto;

            if (mediaPhoto != null)
            {
                if (!message.Out.Value && message.HasTTL())
                {
                    var ttlMessageMedia = mediaPhoto as ITTLMessageMedia;
                    if (ttlMessageMedia != null && ttlMessageMedia.TTLParams == null)
                    {
                        ttlMessageMedia.TTLParams = new TTLParams
                        {
                            StartTime = DateTime.Now,
                            IsStarted = true,
                            Total     = ttlMessageMedia.TTLSeconds.Value
                        };
                        mediaPhoto.NotifyOfPropertyChange(() => ttlMessageMedia.TTLParams);

                        AddToTTLQueue(message as TLMessage70, ttlMessageMedia.TTLParams,
                                      result =>
                        {
                            SplitGroupedMessages(new List <TLInt> {
                                message.Id
                            });
                        });
                    }

                    ReadMessageContents(message as TLMessage25);
                }

                var photo = mediaPhoto.Photo as TLPhoto;
                if (photo != null)
                {
                    var width = 311.0;

                    TLPhotoSize size  = null;
                    var         sizes = photo.Sizes.OfType <TLPhotoSize>();
                    foreach (var photoSize in sizes)
                    {
                        if (size == null ||
                            Math.Abs(width - size.W.Value) > Math.Abs(width - photoSize.W.Value))
                        {
                            size = photoSize;
                        }
                    }

                    if (size != null)
                    {
                        var location = size.Location as TLFileLocation;
                        if (location != null)
                        {
                            var fileName = String.Format("{0}_{1}_{2}.jpg",
                                                         location.VolumeId,
                                                         location.LocalId,
                                                         location.Secret);

                            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                            {
                                if (!store.FileExists(fileName))
                                {
                                    message.Media.IsCanceled          = false;
                                    message.Media.DownloadingProgress = 0.01;
                                    Execute.BeginOnThreadPool(() => DownloadFileManager.DownloadFile(location, photo, size.Size));

                                    return;
                                }
                            }
                        }
                    }
                }

                if (mediaPhoto.IsCanceled)
                {
                    mediaPhoto.IsCanceled = false;
                    mediaPhoto.NotifyOfPropertyChange(() => mediaPhoto.Photo);
                    mediaPhoto.NotifyOfPropertyChange(() => mediaPhoto.Self);

                    return;
                }

                StateService.CurrentPhotoMessage  = message;
                StateService.CurrentMediaMessages = message.HasTTL()
                    ? new List <TLMessage> {
                    message
                }
                    : UngroupEnumerator(Items).OfType <TLMessage>()
                .Where(x =>
                {
                    if (TLMessageBase.HasTTL(x.Media))
                    {
                        return(false);
                    }

                    return(x.Media is TLMessageMediaPhoto || x.Media is TLMessageMediaVideo || x.IsVideo());
                })
                .ToList();

                OpenImageViewer();

                return;
            }

            var mediaWebPage = message.Media as TLMessageMediaWebPage;

            if (mediaWebPage != null)
            {
                var webPage = mediaWebPage.WebPage as TLWebPage;
                if (webPage != null && webPage.Type != null)
                {
                    if (webPage.Type != null)
                    {
                        var type = webPage.Type.ToString();
                        if (string.Equals(type, "photo", StringComparison.OrdinalIgnoreCase))
                        {
                            StateService.CurrentPhotoMessage = message;

                            OpenImageViewer();

                            return;
                        }
                        if (string.Equals(type, "video", StringComparison.OrdinalIgnoreCase))
                        {
                            if (!TLString.Equals(webPage.SiteName, new TLString("youtube"), StringComparison.OrdinalIgnoreCase) &&
                                !TLString.IsNullOrEmpty(webPage.EmbedUrl))
                            {
                                var launcher = new MediaPlayerLauncher
                                {
                                    Location    = MediaLocationType.Data,
                                    Media       = new Uri(webPage.EmbedUrl.ToString(), UriKind.Absolute),
                                    Orientation = MediaPlayerOrientation.Portrait
                                };
                                launcher.Show();
                                return;
                            }

                            if (!TLString.IsNullOrEmpty(webPage.Url))
                            {
                                var webBrowserTask = new WebBrowserTask();
                                webBrowserTask.URL = HttpUtility.UrlEncode(webPage.Url.ToString());
                                webBrowserTask.Show();
                            }

                            return;
                        }
                        if (string.Equals(type, "gif", StringComparison.OrdinalIgnoreCase))
                        {
                            var webPage35 = webPage as TLWebPage35;
                            if (webPage35 != null)
                            {
                                var document = webPage35.Document as TLDocument;
                                if (document == null)
                                {
                                    return;
                                }

                                var documentLocalFileName = document.GetFileName();
                                var store = IsolatedStorageFile.GetUserStoreForApplication();
#if WP81
                                var documentFile = mediaWebPage.File ?? await GetStorageFile(mediaWebPage);
#endif

                                if (!store.FileExists(documentLocalFileName)
#if WP81
                                    && documentFile == null
#endif
                                    )
                                {
                                    mediaWebPage.IsCanceled          = false;
                                    mediaWebPage.DownloadingProgress = mediaWebPage.LastProgress > 0.0 ? mediaWebPage.LastProgress : 0.001;
                                    DownloadDocumentFileManager.DownloadFileAsync(document.FileName, document.DCId, document.ToInputFileLocation(), message, document.Size,
                                                                                  progress =>
                                    {
                                        if (progress > 0.0)
                                        {
                                            mediaWebPage.DownloadingProgress = progress;
                                        }
                                    });
                                }
                            }

                            return;
                        }
                    }
                }
            }

            var mediaGame = message.Media as TLMessageMediaGame;
            if (mediaGame != null)
            {
                var game = mediaGame.Game;
                if (game != null)
                {
                    var document = game.Document as TLDocument;
                    if (document == null)
                    {
                        return;
                    }

                    var documentLocalFileName = document.GetFileName();
                    var store = IsolatedStorageFile.GetUserStoreForApplication();
#if WP81
                    var documentFile = mediaGame.File ?? await GetStorageFile(mediaGame);
#endif

                    if (!store.FileExists(documentLocalFileName)
#if WP81
                        && documentFile == null
#endif
                        )
                    {
                        mediaGame.IsCanceled          = false;
                        mediaGame.DownloadingProgress = mediaGame.LastProgress > 0.0 ? mediaGame.LastProgress : 0.001;
                        DownloadDocumentFileManager.DownloadFileAsync(document.FileName, document.DCId, document.ToInputFileLocation(), message, document.Size,
                                                                      progress =>
                        {
                            if (progress > 0.0)
                            {
                                mediaGame.DownloadingProgress = progress;
                            }
                        });
                    }
                }

                return;
            }

            var mediaGeo = message.Media as TLMessageMediaGeo;
            if (mediaGeo != null)
            {
                OpenLocation(message);

                return;
            }

            var mediaContact = message.Media as TLMessageMediaContact;
            if (mediaContact != null)
            {
                var phoneNumber = mediaContact.PhoneNumber.ToString();

                if (mediaContact.UserId.Value == 0)
                {
                    OpenPhoneContact(mediaContact);
                }
                else
                {
                    var user = mediaContact.User;

                    OpenMediaContact(mediaContact.UserId, user, new TLString(phoneNumber));
                }
                return;
            }

            var mediaVideo = message.Media as TLMessageMediaVideo;
            if (mediaVideo != null)
            {
                var video = mediaVideo.Video as TLVideo;
                if (video == null)
                {
                    return;
                }

                var videoFileName = video.GetFileName();
                var store         = IsolatedStorageFile.GetUserStoreForApplication();
#if WP81
                var file = mediaVideo.File ?? await GetStorageFile(mediaVideo);
#endif


                if (!store.FileExists(videoFileName)
#if WP81
                    && file == null
#endif
                    )
                {
                    mediaVideo.IsCanceled          = false;
                    mediaVideo.DownloadingProgress = mediaVideo.LastProgress > 0.0 ? mediaVideo.LastProgress : 0.001;
                    DownloadVideoFileManager.DownloadFileAsync(
                        video.DCId, video.ToInputFileLocation(), message, video.Size,
                        progress =>
                    {
                        if (progress > 0.0)
                        {
                            mediaVideo.DownloadingProgress = progress;
                        }
                    });
                }
                else
                {
                    //ReadMessageContents(message);

#if WP81
                    //var localFile = await GetFileFromLocalFolder(videoFileName);
                    var videoProperties = await file.Properties.GetVideoPropertiesAsync();

                    var musicProperties = await file.Properties.GetMusicPropertiesAsync();

                    if (file != null)
                    {
                        Launcher.LaunchFileAsync(file);
                        return;
                    }
#endif

                    var launcher = new MediaPlayerLauncher
                    {
                        Location = MediaLocationType.Data,
                        Media    = new Uri(videoFileName, UriKind.Relative)
                    };
                    launcher.Show();
                }
                return;
            }

            var mediaAudio = message.Media as TLMessageMediaAudio;
            if (mediaAudio != null)
            {
                var audio = mediaAudio.Audio as TLAudio;
                if (audio == null)
                {
                    return;
                }

                var store         = IsolatedStorageFile.GetUserStoreForApplication();
                var audioFileName = audio.GetFileName();
                if (TLString.Equals(audio.MimeType, new TLString("audio/mpeg"), StringComparison.OrdinalIgnoreCase))
                {
                    if (!store.FileExists(audioFileName))
                    {
                        mediaAudio.IsCanceled          = false;
                        mediaAudio.DownloadingProgress = mediaAudio.LastProgress > 0.0 ? mediaAudio.LastProgress : 0.001;
                        BeginOnThreadPool(() =>
                        {
                            DownloadAudioFileManager.DownloadFile(audio.DCId, audio.ToInputFileLocation(), message, audio.Size);
                        });
                    }
                    else
                    {
                        ReadMessageContents(message as TLMessage25);
                    }

                    return;
                }

                var wavFileName = Path.GetFileNameWithoutExtension(audioFileName) + ".wav";

                if (!store.FileExists(wavFileName))
                {
                    mediaAudio.IsCanceled          = false;
                    mediaAudio.DownloadingProgress = mediaAudio.LastProgress > 0.0 ? mediaAudio.LastProgress : 0.001;
                    BeginOnThreadPool(() =>
                    {
                        DownloadAudioFileManager.DownloadFile(audio.DCId, audio.ToInputFileLocation(), message, audio.Size);
                    });
                }
                else
                {
                    ReadMessageContents(message as TLMessage25);
                }

                if (mediaAudio.IsCanceled)
                {
                    mediaAudio.IsCanceled = false;
                    mediaAudio.NotifyOfPropertyChange(() => mediaPhoto.ThumbSelf);

                    return;
                }

                return;
            }

            var mediaDocument = message.Media as TLMessageMediaDocument;
            if (mediaDocument != null)
            {
                var document = mediaDocument.Document as TLDocument22;
                if (document != null)
                {
                    if (TLMessageBase.IsSticker(document))
                    {
                        var inputStickerSet = document.StickerSet;
                        if (inputStickerSet != null && !(inputStickerSet is TLInputStickerSetEmpty))
                        {
                            AddToStickers(message);
                        }
                    }
                    else if (TLMessageBase.IsVoice(document))
                    {
                        var store         = IsolatedStorageFile.GetUserStoreForApplication();
                        var audioFileName = string.Format("audio{0}_{1}.mp3", document.Id, document.AccessHash);
                        if (TLString.Equals(document.MimeType, new TLString("audio/mpeg"), StringComparison.OrdinalIgnoreCase))
                        {
                            if (!store.FileExists(audioFileName))
                            {
                                mediaDocument.IsCanceled          = false;
                                mediaDocument.DownloadingProgress = mediaDocument.LastProgress > 0.0 ? mediaDocument.LastProgress : 0.001;
                                BeginOnThreadPool(() =>
                                {
                                    DownloadAudioFileManager.DownloadFile(document.DCId, document.ToInputFileLocation(), message, document.Size);
                                });
                            }
                            else
                            {
                                ReadMessageContents(message as TLMessage25);
                            }

                            return;
                        }

                        var wavFileName = Path.GetFileNameWithoutExtension(audioFileName) + ".wav";

                        if (!store.FileExists(wavFileName))
                        {
                            mediaDocument.IsCanceled          = false;
                            mediaDocument.DownloadingProgress = mediaDocument.LastProgress > 0.0 ? mediaDocument.LastProgress : 0.001;
                            BeginOnThreadPool(() =>
                            {
                                DownloadAudioFileManager.DownloadFile(document.DCId, document.ToInputFileLocation(), message, document.Size);
                            });
                        }
                        else
                        {
                            ReadMessageContents(message as TLMessage25);
                        }

                        if (mediaDocument.IsCanceled)
                        {
                            mediaDocument.IsCanceled = false;
                            mediaDocument.NotifyOfPropertyChange(() => mediaDocument.ThumbSelf);

                            return;
                        }
                    }
                    else if (TLMessageBase.IsVideo(document))
                    {
                        var video = mediaDocument.Document as TLDocument;
                        if (video == null)
                        {
                            return;
                        }

                        var videoFileName = video.GetFileName();
                        var store         = IsolatedStorageFile.GetUserStoreForApplication();
#if WP81
                        var file = mediaDocument.File ?? await GetStorageFile(mediaDocument);
#endif


                        if (!store.FileExists(videoFileName)
#if WP81
                            && file == null
#endif
                            )
                        {
                            mediaDocument.IsCanceled          = false;
                            mediaDocument.DownloadingProgress = mediaDocument.LastProgress > 0.0
                                ? mediaDocument.LastProgress
                                : 0.001;
                            DownloadVideoFileManager.DownloadFileAsync(
                                video.DCId, video.ToInputFileLocation(), message, video.Size,
                                progress =>
                            {
                                if (progress > 0.0)
                                {
                                    mediaDocument.DownloadingProgress = progress;
                                }
                            });
                        }
                        else
                        {
                            if (!message.Out.Value && message.HasTTL())
                            {
                                var ttlMessageMedia = mediaDocument as ITTLMessageMedia;
                                if (ttlMessageMedia != null && ttlMessageMedia.TTLParams == null)
                                {
                                    ttlMessageMedia.TTLParams = new TTLParams
                                    {
                                        StartTime = DateTime.Now,
                                        IsStarted = true,
                                        Total     = ttlMessageMedia.TTLSeconds.Value
                                    };
                                    mediaDocument.NotifyOfPropertyChange(() => ttlMessageMedia.TTLParams);

                                    AddToTTLQueue(message as TLMessage70, ttlMessageMedia.TTLParams,
                                                  result =>
                                    {
                                        SplitGroupedMessages(new List <TLInt> {
                                            message.Id
                                        });
                                    });
                                }

                                ReadMessageContents(message as TLMessage25);
                            }
                            else if (message.IsRoundVideo())
                            {
                                ReadMessageContents(message as TLMessage25);
                            }
#if WP81
                            if (file != null)
                            {
                                Launcher.LaunchFileAsync(file);
                                return;
                            }
#endif

                            var launcher = new MediaPlayerLauncher
                            {
                                Location = MediaLocationType.Data,
                                Media    = new Uri(videoFileName, UriKind.Relative)
                            };
                            launcher.Show();
                        }
                        return;
                    }
                    else
                    {
                        OpenDocumentCommon(message, StateService, DownloadDocumentFileManager,
                                           () =>
                        {
                            StateService.CurrentPhotoMessage = message;

                            if (AnimatedImageViewer == null)
                            {
                                AnimatedImageViewer = new AnimatedImageViewerViewModel(StateService);
                                NotifyOfPropertyChange(() => AnimatedImageViewer);
                            }

                            Execute.BeginOnUIThread(() => AnimatedImageViewer.OpenViewer());
                        });
                    }
                }
                else
                {
                    OpenDocumentCommon(message, StateService, DownloadDocumentFileManager,
                                       () =>
                    {
                        StateService.CurrentPhotoMessage = message;

                        if (AnimatedImageViewer == null)
                        {
                            AnimatedImageViewer = new AnimatedImageViewerViewModel(StateService);
                            NotifyOfPropertyChange(() => AnimatedImageViewer);
                        }

                        Execute.BeginOnUIThread(() => AnimatedImageViewer.OpenViewer());
                    });
                }

                return;
            }

            Execute.ShowDebugMessage("tap on media");
        }