public static Telegram.Api.WindowsPhone.Tuple <TLDecryptedMessageService, TLObject> GetDecryptedServiceMessageAndObject(TLDecryptedMessageActionBase action, TLEncryptedChat chat, TLInt currentUserId, ICacheService cacheService)
        {
            var mtProtoService = IoC.Get <IMTProtoService>();

            TLDecryptedMessageService decryptedMessage;
            TLObject decryptedObject;

            var randomId = TLLong.Random();

            var encryptedChat17 = chat as TLEncryptedChat17;

            if (encryptedChat17 != null && encryptedChat17.Layer.Value >= 17)
            {
                var cachedEncryptedChat17 = (TLEncryptedChat17)cacheService.GetEncryptedChat(encryptedChat17.Id);

                var inSeqNo  = TLUtils.GetInSeqNo(currentUserId, cachedEncryptedChat17);
                var outSeqNo = TLUtils.GetOutSeqNo(currentUserId, cachedEncryptedChat17);

                cachedEncryptedChat17.RawOutSeqNo = new TLInt(cachedEncryptedChat17.RawOutSeqNo.Value + 1);
                var decryptedMessage17 = new TLDecryptedMessageService17
                {
                    Action   = action,
                    RandomId = randomId,
                    //RandomBytes = TLString.Random(Telegram.Api.Constants.MinRandomBytesLength),

                    ChatId = chat.Id,
                    FromId = currentUserId,
                    Out    = new TLBool(true),
                    Unread = new TLBool(true),
                    Date   = TLUtils.DateToUniversalTimeTLInt(mtProtoService.ClientTicksDelta, DateTime.Now),
                    Status = MessageStatus.Sending,

                    TTL      = chat.MessageTTL ?? new TLInt(0),
                    InSeqNo  = inSeqNo,
                    OutSeqNo = outSeqNo
                };

                var decryptedMessageLayer17 = TLUtils.GetDecryptedMessageLayer(encryptedChat17.Layer, inSeqNo, outSeqNo, decryptedMessage17);

                decryptedMessage = decryptedMessage17;
                decryptedObject  = decryptedMessageLayer17;
            }
            else
            {
                var decryptedMessage8 = new TLDecryptedMessageService
                {
                    Action      = action,
                    RandomId    = randomId,
                    RandomBytes = TLString.Random(Telegram.Api.Constants.MinRandomBytesLength),

                    ChatId = chat.Id,
                    FromId = currentUserId,
                    Out    = new TLBool(true),
                    Unread = new TLBool(true),
                    Date   = TLUtils.DateToUniversalTimeTLInt(mtProtoService.ClientTicksDelta, DateTime.Now),
                    Status = MessageStatus.Sending,

                    TTL = chat.MessageTTL
                };

                decryptedMessage = decryptedMessage8;
                decryptedObject  = decryptedMessage;
            }

            return(new Telegram.Api.WindowsPhone.Tuple <TLDecryptedMessageService, TLObject>(decryptedMessage, decryptedObject));
        }
        public Telegram.Api.WindowsPhone.Tuple <TLDecryptedMessageBase, TLObject> GetDecryptedMessageAndObject(TLString text, TLDecryptedMessageMediaBase media, TLEncryptedChat chat, bool delaySeq = false)
        {
            TLDecryptedMessageBase decryptedMessage;
            TLObject decryptedObject;

            var randomId = TLLong.Random();

            var encryptedChat17 = chat as TLEncryptedChat17;

            if (encryptedChat17 != null)
            {
                var cachedEncryptedChat17 = (TLEncryptedChat17)CacheService.GetEncryptedChat(encryptedChat17.Id);

                TLInt inSeqNo;
                TLInt outSeqNo;
                if (!delaySeq)
                {
                    inSeqNo  = TLUtils.GetInSeqNo(MTProtoService.CurrentUserId, cachedEncryptedChat17);
                    outSeqNo = TLUtils.GetOutSeqNo(MTProtoService.CurrentUserId, cachedEncryptedChat17);

                    cachedEncryptedChat17.RawOutSeqNo = new TLInt(cachedEncryptedChat17.RawOutSeqNo.Value + 1);
                }
                else
                {
                    inSeqNo  = new TLInt(-1);
                    outSeqNo = new TLInt(-1);
                }

                TLDecryptedMessageBase decryptedMessageBase;

                if (encryptedChat17.Layer.Value >= 73)
                {
                    decryptedMessageBase = new TLDecryptedMessage73
                    {
                        Flags       = new TLInt(0),
                        Media       = media,
                        Message     = text,
                        RandomId    = randomId,
                        RandomBytes = TLString.Random(Telegram.Api.Constants.MinRandomBytesLength),
                        //Entities = new TLVector<TLMessageEntityBase>(),
                        //ViaBotName = TLString.Empty,
                        //ReplyToRandomMsgId = new TLLong(0),

                        ChatId = chat.Id,
                        FromId = new TLInt(StateService.CurrentUserId),
                        Out    = new TLBool(true),
                        Unread = new TLBool(true),
                        Date   = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                        Status = MessageStatus.Sending,

                        TTL      = chat.MessageTTL ?? new TLInt(0),
                        InSeqNo  = inSeqNo,
                        OutSeqNo = outSeqNo
                    };
                    media.TTLSeconds = decryptedMessageBase.TTL;

                    if (!(media is TLDecryptedMessageMediaEmpty))
                    {
                        ((TLDecryptedMessage45)decryptedMessageBase).SetMedia();
                    }
                }
                else if (encryptedChat17.Layer.Value >= 45)
                {
                    decryptedMessageBase = new TLDecryptedMessage45
                    {
                        Flags       = new TLInt(0),
                        Media       = media,
                        Message     = text,
                        RandomId    = randomId,
                        RandomBytes = TLString.Random(Telegram.Api.Constants.MinRandomBytesLength),
                        //Entities = new TLVector<TLMessageEntityBase>(),
                        //ViaBotName = TLString.Empty,
                        //ReplyToRandomMsgId = new TLLong(0),

                        ChatId = chat.Id,
                        FromId = new TLInt(StateService.CurrentUserId),
                        Out    = new TLBool(true),
                        Unread = new TLBool(true),
                        Date   = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                        Status = MessageStatus.Sending,

                        TTL      = chat.MessageTTL ?? new TLInt(0),
                        InSeqNo  = inSeqNo,
                        OutSeqNo = outSeqNo
                    };

                    if (!(media is TLDecryptedMessageMediaEmpty))
                    {
                        ((TLDecryptedMessage45)decryptedMessageBase).SetMedia();
                    }
                }
                else
                {
                    decryptedMessageBase = new TLDecryptedMessage17
                    {
                        Media       = media,
                        Message     = text,
                        RandomId    = randomId,
                        RandomBytes = TLString.Random(Telegram.Api.Constants.MinRandomBytesLength),

                        ChatId = chat.Id,
                        FromId = new TLInt(StateService.CurrentUserId),
                        Out    = new TLBool(true),
                        Unread = new TLBool(true),
                        Date   = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                        Status = MessageStatus.Sending,

                        TTL      = chat.MessageTTL ?? new TLInt(0),
                        InSeqNo  = inSeqNo,
                        OutSeqNo = outSeqNo
                    };
                }

                var decryptedMessageLayer17 = TLUtils.GetDecryptedMessageLayer(encryptedChat17.Layer, inSeqNo, outSeqNo, decryptedMessageBase);

                decryptedMessage = decryptedMessageBase;
                decryptedObject  = decryptedMessageLayer17;
            }
            else
            {
                var decryptedMessage8 = new TLDecryptedMessage
                {
                    Media       = media,
                    Message     = text,
                    RandomId    = randomId,
                    RandomBytes = TLString.Random(Telegram.Api.Constants.MinRandomBytesLength),

                    ChatId = chat.Id,
                    FromId = new TLInt(StateService.CurrentUserId),
                    Out    = new TLBool(true),
                    Unread = new TLBool(true),
                    Date   = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                    Status = MessageStatus.Sending,

                    TTL = chat.MessageTTL
                };

                decryptedMessage = decryptedMessage8;
                decryptedObject  = decryptedMessage;
            }

            return(new Telegram.Api.WindowsPhone.Tuple <TLDecryptedMessageBase, TLObject>(decryptedMessage, decryptedObject));
        }
        private void SendMessages(IList <Telegram.Api.WindowsPhone.Tuple <TLDecryptedMessageBase, TLObject> > messages, Action <IList <Telegram.Api.WindowsPhone.Tuple <TLDecryptedMessageBase, TLObject> > > callback)
        {
            var uploadService = IoC.Get <IUploadService>();

            var tuple        = messages.FirstOrDefault();
            var mediaMessage = tuple != null? tuple.Item1 as TLDecryptedMessage73 : null;
            var groupedId    = mediaMessage != null ? mediaMessage.GroupedId : null;

            if (groupedId != null)
            {
                var messageMediaGroup = new TLDecryptedMessageMediaGroup {
                    Group = new TLVector <TLDecryptedMessageBase>()
                };
                var message = new TLDecryptedMessage73
                {
                    Flags              = new TLInt(0),
                    Media              = messageMediaGroup,
                    Message            = TLString.Empty,
                    RandomId           = mediaMessage.RandomId,
                    RandomBytes        = TLString.Random(Telegram.Api.Constants.MinRandomBytesLength),
                    ChatId             = mediaMessage.ChatId,
                    FromId             = mediaMessage.FromId,
                    Out                = mediaMessage.Out,
                    Unread             = mediaMessage.Unread,
                    Date               = mediaMessage.Date,
                    TTL                = mediaMessage.TTL,
                    Status             = MessageStatus.Sending,
                    ReplyToRandomMsgId = mediaMessage.ReplyToRandomMsgId,
                    Reply              = mediaMessage.Reply,
                    GroupedId          = groupedId
                };
                message.SetMedia();

                for (var i = 0; i < messages.Count; i++)
                {
                    if (i % Constants.MaxGroupedMediaCount == 0)
                    {
                        if (messageMediaGroup.Group.Count > 0)
                        {
                            uploadService.AddGroup(message);
                            Items.Insert(0, message);
                        }

                        mediaMessage = messages[i].Item1 as TLDecryptedMessage73;
                        groupedId    = mediaMessage != null ? mediaMessage.GroupedId : null;

                        messageMediaGroup = new TLDecryptedMessageMediaGroup {
                            Group = new TLVector <TLDecryptedMessageBase>()
                        };
                        message = new TLDecryptedMessage73
                        {
                            Flags              = new TLInt(0),
                            Media              = messageMediaGroup,
                            Message            = TLString.Empty,
                            RandomId           = mediaMessage.RandomId,
                            RandomBytes        = TLString.Random(Telegram.Api.Constants.MinRandomBytesLength),
                            ChatId             = mediaMessage.ChatId,
                            FromId             = mediaMessage.FromId,
                            Out                = mediaMessage.Out,
                            Unread             = mediaMessage.Unread,
                            Date               = mediaMessage.Date,
                            TTL                = mediaMessage.TTL,
                            Status             = MessageStatus.Sending,
                            ReplyToRandomMsgId = mediaMessage.ReplyToRandomMsgId,
                            Reply              = mediaMessage.Reply,
                            GroupedId          = groupedId
                        };
                        message.SetMedia();
                    }

                    messageMediaGroup.Group.Add(messages[i].Item1);
                }

                if (messageMediaGroup.Group.Count > 0)
                {
                    uploadService.AddGroup(message);
                    Items.Insert(0, message);
                }
            }
            else
            {
                foreach (var message in messages)
                {
                    Items.Insert(0, message.Item1);
                }
            }

            //IsEmptyDialog = Items.Count == 0 && (_messages == null || _messages.Count == 0) && LazyItems.Count == 0;
            Reply = null;

            RaiseScrollToBottom();
            NotifyOfPropertyChange(() => DescriptionVisibility);

            BeginOnThreadPool(() => CacheService.SyncDecryptedMessages(messages, Chat, callback.SafeInvoke));
        }