public static bool IsVideo(TLDecryptedMessageMediaDocument45 document)
        {
#if WP8
            var document22 = document;
            if (document22 != null)
            {
                return(IsVideo(document22, document22.Size));
            }
#endif

            return(false);
        }
        private static TLDecryptedMessageMediaBase GetDecryptedMediaDocument(StorageFile file, TLEncryptedChat chat, TLPhotoSize thumb, ulong size, Telegram.Api.WindowsPhone.Tuple <TLString, TLString> keyIV, TLEncryptedFile fileLocation)
        {
            TLDecryptedMessageMediaBase decryptedMediaDocument;
            var chat17 = chat as TLEncryptedChat17;

            if (chat17 != null)
            {
                if (chat17.Layer.Value >= Constants.MinSecretChatWithCaptionsLayer)
                {
                    decryptedMediaDocument = new TLDecryptedMessageMediaDocument45
                    {
                        Thumb    = thumb != null ? thumb.Bytes : TLString.Empty,
                        ThumbW   = thumb != null ? thumb.W : new TLInt(0),
                        ThumbH   = thumb != null ? thumb.H : new TLInt(0),
                        FileName = new TLString(Path.GetFileName(file.Name)),
                        MimeType = new TLString(file.ContentType),
                        Size     = new TLInt((int)size),
                        Key      = keyIV.Item1,
                        IV       = keyIV.Item2,
                        Caption  = TLString.Empty,

                        File              = fileLocation,
                        StorageFile       = file,
                        UploadingProgress = 0.001
                    };
                }
                else
                {
                    decryptedMediaDocument = new TLDecryptedMessageMediaDocument
                    {
                        Thumb             = thumb != null ? thumb.Bytes : TLString.Empty,
                        ThumbW            = thumb != null ? thumb.W : new TLInt(0),
                        ThumbH            = thumb != null ? thumb.H : new TLInt(0),
                        FileName          = new TLString(Path.GetFileName(file.Name)),
                        MimeType          = new TLString(file.ContentType),
                        Size              = new TLInt((int)size),
                        Key               = keyIV.Item1,
                        IV                = keyIV.Item2,
                        File              = fileLocation,
                        StorageFile       = file,
                        UploadingProgress = 0.001
                    };
                }
            }
            else
            {
                decryptedMediaDocument = new TLDecryptedMessageMediaDocument
                {
                    Thumb             = thumb != null ? thumb.Bytes : TLString.Empty,
                    ThumbW            = thumb != null ? thumb.W : new TLInt(0),
                    ThumbH            = thumb != null ? thumb.H : new TLInt(0),
                    FileName          = new TLString(Path.GetFileName(file.Name)),
                    MimeType          = new TLString(file.ContentType),
                    Size              = new TLInt((int)size),
                    Key               = keyIV.Item1,
                    IV                = keyIV.Item2,
                    File              = fileLocation,
                    StorageFile       = file,
                    UploadingProgress = 0.001
                };
            }
            return(decryptedMediaDocument);
        }
        private static TLDecryptedMessageMediaBase GetDecryptedMediaDocument(Photo p, TLEncryptedChat chat, TLString thumb, TLInt thumbW, TLInt thumbH, TLString mimeType, Telegram.Api.WindowsPhone.Tuple <TLString, TLString> keyIV, TLEncryptedFile fileLocation)
        {
            TLDecryptedMessageMediaBase decryptedMediaDocument;
            var chat17 = chat as TLEncryptedChat17;

            if (chat17 != null)
            {
                if (chat17.Layer.Value >= Constants.MinSecretChatWithCaptionsLayer)
                {
                    decryptedMediaDocument = new TLDecryptedMessageMediaDocument45
                    {
                        Thumb    = thumb,
                        ThumbW   = thumbW,
                        ThumbH   = thumbH,
                        FileName = new TLString(Path.GetFileName(p.FileName)),
                        MimeType = mimeType,
                        Size     = new TLInt(p.Bytes.Length),
                        Key      = keyIV.Item1,
                        IV       = keyIV.Item2,
                        Caption  = TLString.Empty,

                        File = fileLocation,
                        UploadingProgress = 0.001
                    };
                }
                else
                {
                    decryptedMediaDocument = new TLDecryptedMessageMediaDocument
                    {
                        Thumb    = thumb,
                        ThumbW   = thumbW,
                        ThumbH   = thumbH,
                        FileName = new TLString(Path.GetFileName(p.FileName)),
                        MimeType = mimeType,
                        Size     = new TLInt(p.Bytes.Length),
                        Key      = keyIV.Item1,
                        IV       = keyIV.Item2,

                        File = fileLocation,
                        UploadingProgress = 0.001
                    };
                }
            }
            else
            {
                decryptedMediaDocument = new TLDecryptedMessageMediaDocument
                {
                    Thumb    = thumb,
                    ThumbW   = thumbW,
                    ThumbH   = thumbH,
                    FileName = new TLString(Path.GetFileName(p.FileName)),
                    MimeType = mimeType,
                    Size     = new TLInt(p.Bytes.Length),
                    Key      = keyIV.Item1,
                    IV       = keyIV.Item2,

                    File = fileLocation,
                    UploadingProgress = 0.001
                };
            }
            return(decryptedMediaDocument);
        }
        public void SendAudio(AudioEventArgs args)
        {
            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(args.OggFileName))
            {
                return;
            }

            var dcId       = TLInt.Random();
            var id         = TLLong.Random();
            var accessHash = TLLong.Random();

            var oggFileName = String.Format("audio{0}_{1}.mp3", id, accessHash);
            var wavFileName = Path.GetFileNameWithoutExtension(oggFileName) + ".wav";

            long size = 0;

            using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                storage.MoveFile(args.OggFileName, oggFileName);
                using (var file = storage.OpenFile(oggFileName, FileMode.Open, FileAccess.Read))
                {
                    size = file.Length;
                }

                var wavStream = Wav.GetWavAsMemoryStream(args.PcmStream, 16000, 1, 16);
                using (var file = new IsolatedStorageFileStream(wavFileName, FileMode.OpenOrCreate, storage))
                {
                    wavStream.Seek(0, SeekOrigin.Begin);
                    wavStream.CopyTo(file);
                    file.Flush();
                }
            }

            var fileLocation = new TLEncryptedFile
            {
                Id             = id,
                AccessHash     = accessHash,
                DCId           = dcId,
                Size           = new TLInt((int)size),
                KeyFingerprint = new TLInt(0),
                FileName       = new TLString(Path.GetFileName(oggFileName))
            };

            var keyIV = GenerateKeyIV();
            TLDecryptedMessageMediaBase decryptedMediaAudio;
            var encryptedChat17 = chat as TLEncryptedChat17;

            if (encryptedChat17 != null)
            {
                if (encryptedChat17.Layer.Value >= Constants.MinSecretChatWithAudioAsDocumentsLayer)
                {
                    var opus          = new TelegramClient_Opus.WindowsPhoneRuntimeComponent();
                    var bytes         = opus.GetWaveform(ApplicationData.Current.LocalFolder.Path + "\\" + oggFileName);
                    var resultSamples = bytes.Length;
                    var bites2        = new BitArray(5 * bytes.Length);
                    var count         = 0;
                    for (var i = 0; i < bytes.Length; i++)
                    {
                        var result = bytes[i];
                        var bit1   = result >> 0 & 0x1;
                        var bit2   = result >> 1 & 0x1;
                        var bit3   = result >> 2 & 0x1;
                        var bit4   = result >> 3 & 0x1;
                        var bit5   = result >> 4 & 0x1;
                        bites2[count]     = Convert.ToBoolean(bit1);
                        bites2[count + 1] = Convert.ToBoolean(bit2);
                        bites2[count + 2] = Convert.ToBoolean(bit3);
                        bites2[count + 3] = Convert.ToBoolean(bit4);
                        bites2[count + 4] = Convert.ToBoolean(bit5);
                        count             = count + 5;
                    }

                    var bytesCount    = (resultSamples * 5) / 8 + (((resultSamples * 5) % 8) == 0 ? 0 : 1);
                    var waveformBytes = new byte[bytesCount];
                    bites2.CopyTo(waveformBytes, 0);
                    var waveform = waveformBytes != null?TLString.FromBigEndianData(waveformBytes) : TLString.Empty;

                    var audioAttribute = new TLDocumentAttributeAudio46
                    {
                        Flags    = new TLInt((int)DocumentAttributeAudioFlags.Voice),
                        Duration = new TLInt((int)args.Duration)
                    };

                    if (waveformBytes != null)
                    {
                        audioAttribute.Waveform = waveform;
                    }

                    var attributes = new TLVector <TLDocumentAttributeBase>
                    {
                        audioAttribute
                    };

                    decryptedMediaAudio = new TLDecryptedMessageMediaDocument45
                    {
                        Thumb       = TLString.Empty,
                        ThumbW      = new TLInt(0),
                        ThumbH      = new TLInt(0),
                        MimeType    = new TLString("audio/ogg"),
                        Size        = new TLInt((int)size),
                        Key         = keyIV.Item1,
                        IV          = keyIV.Item2,
                        Attributes  = attributes,
                        Caption     = TLString.Empty,
                        NotListened = true,

                        File = fileLocation,

                        UploadingProgress = 0.001
                    };
                }
                else
                {
                    decryptedMediaAudio = new TLDecryptedMessageMediaAudio17
                    {
                        Duration = new TLInt((int)args.Duration),
                        MimeType = new TLString("audio/ogg"),
                        Size     = new TLInt((int)size),
                        Key      = keyIV.Item1,
                        IV       = keyIV.Item2,

                        UserId = new TLInt(StateService.CurrentUserId),
                        File   = fileLocation,

                        UploadingProgress = 0.001
                    };
                }
            }
            else
            {
                decryptedMediaAudio = new TLDecryptedMessageMediaAudio
                {
                    Duration = new TLInt((int)args.Duration),
                    //MimeType = new TLString("audio/ogg"),
                    Size = new TLInt((int)size),
                    Key  = keyIV.Item1,
                    IV   = keyIV.Item2,

                    UserId = new TLInt(StateService.CurrentUserId),
                    File   = fileLocation,

                    UploadingProgress = 0.001
                };
            }

            var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaAudio, chat, true);

            //var message45 = decryptedTuple.Item1 as TLDecryptedMessage45;
            //if (message45 != null && message45.IsVoice())
            //{
            //    message45.NotListened = true;
            //}

            BeginOnUIThread(() =>
            {
                InsertSendingMessage(decryptedTuple.Item1);
                NotifyOfPropertyChange(() => DescriptionVisibility);
            });

            BeginOnThreadPool(() =>
                              CacheService.SyncDecryptedMessage(decryptedTuple.Item1, chat,
                                                                cachedMessage => SendAudioInternal(decryptedTuple.Item2)));
        }