"""File generated by TLObjects' generator. All changes will be ERASED"""
import struct
from .. import TLObject, types, custom

class MessageEmpty(custom.message.Message):
    CONSTRUCTOR_ID = 0x83e5de54
    SUBCLASS_OF_ID = 0x790009e3
    def to_dict(self):
        return {
            '_': 'MessageEmpty',
            'id': self.id
        }

    def _bytes(self):
        return b''.join((
            b'T\xde\xe5\x83',
            struct.pack('<i', self.id),
        ))

    @classmethod
    def from_reader(cls, reader):
        _id = reader.read_int()
        return cls(id=_id)

types.MessageEmpty = MessageEmpty

class Message(custom.message.Message):
    CONSTRUCTOR_ID = 0x58ae39c9
    SUBCLASS_OF_ID = 0x790009e3
    def to_dict(self):
        return {
            '_': 'Message',
            'id': self.id,
            'peer_id': self.peer_id.to_dict() if isinstance(self.peer_id, TLObject) else self.peer_id,
            'date': self.date,
            'message': self.message,
            'out': self.out,
            'mentioned': self.mentioned,
            'media_unread': self.media_unread,
            'silent': self.silent,
            'post': self.post,
            'from_scheduled': self.from_scheduled,
            'legacy': self.legacy,
            'edit_hide': self.edit_hide,
            'from_id': self.from_id.to_dict() if isinstance(self.from_id, TLObject) else self.from_id,
            'fwd_from': self.fwd_from.to_dict() if isinstance(self.fwd_from, TLObject) else self.fwd_from,
            'via_bot_id': self.via_bot_id,
            'reply_to': self.reply_to.to_dict() if isinstance(self.reply_to, TLObject) else self.reply_to,
            'media': self.media.to_dict() if isinstance(self.media, TLObject) else self.media,
            'reply_markup': self.reply_markup.to_dict() if isinstance(self.reply_markup, TLObject) else self.reply_markup,
            'entities': [] if self.entities is None else [x.to_dict() if isinstance(x, TLObject) else x for x in self.entities],
            'views': self.views,
            'forwards': self.forwards,
            'replies': self.replies.to_dict() if isinstance(self.replies, TLObject) else self.replies,
            'edit_date': self.edit_date,
            'post_author': self.post_author,
            'grouped_id': self.grouped_id,
            'restriction_reason': [] if self.restriction_reason is None else [x.to_dict() if isinstance(x, TLObject) else x for x in self.restriction_reason]
        }

    def _bytes(self):
        assert ((self.views or self.views is not None) and (self.forwards or self.forwards is not None)) or ((self.views is None or self.views is False) and (self.forwards is None or self.forwards is False)), 'views, forwards parameters must all be False-y (like None) or all me True-y'
        return b''.join((
            b'\xc99\xaeX',
            struct.pack('<I', (0 if self.out is None or self.out is False else 2) | (0 if self.mentioned is None or self.mentioned is False else 16) | (0 if self.media_unread is None or self.media_unread is False else 32) | (0 if self.silent is None or self.silent is False else 8192) | (0 if self.post is None or self.post is False else 16384) | (0 if self.from_scheduled is None or self.from_scheduled is False else 262144) | (0 if self.legacy is None or self.legacy is False else 524288) | (0 if self.edit_hide is None or self.edit_hide is False else 2097152) | (0 if self.from_id is None or self.from_id is False else 256) | (0 if self.fwd_from is None or self.fwd_from is False else 4) | (0 if self.via_bot_id is None or self.via_bot_id is False else 2048) | (0 if self.reply_to is None or self.reply_to is False else 8) | (0 if self.media is None or self.media is False else 512) | (0 if self.reply_markup is None or self.reply_markup is False else 64) | (0 if self.entities is None or self.entities is False else 128) | (0 if self.views is None or self.views is False else 1024) | (0 if self.forwards is None or self.forwards is False else 1024) | (0 if self.replies is None or self.replies is False else 8388608) | (0 if self.edit_date is None or self.edit_date is False else 32768) | (0 if self.post_author is None or self.post_author is False else 65536) | (0 if self.grouped_id is None or self.grouped_id is False else 131072) | (0 if self.restriction_reason is None or self.restriction_reason is False else 4194304)),
            struct.pack('<i', self.id),
            b'' if self.from_id is None or self.from_id is False else (self.from_id._bytes()),
            self.peer_id._bytes(),
            b'' if self.fwd_from is None or self.fwd_from is False else (self.fwd_from._bytes()),
            b'' if self.via_bot_id is None or self.via_bot_id is False else (struct.pack('<i', self.via_bot_id)),
            b'' if self.reply_to is None or self.reply_to is False else (self.reply_to._bytes()),
            self.serialize_datetime(self.date),
            self.serialize_bytes(self.message),
            b'' if self.media is None or self.media is False else (self.media._bytes()),
            b'' if self.reply_markup is None or self.reply_markup is False else (self.reply_markup._bytes()),
            b'' if self.entities is None or self.entities is False else b''.join((b'\x15\xc4\xb5\x1c',struct.pack('<i', len(self.entities)),b''.join(x._bytes() for x in self.entities))),
            b'' if self.views is None or self.views is False else (struct.pack('<i', self.views)),
            b'' if self.forwards is None or self.forwards is False else (struct.pack('<i', self.forwards)),
            b'' if self.replies is None or self.replies is False else (self.replies._bytes()),
            b'' if self.edit_date is None or self.edit_date is False else (self.serialize_datetime(self.edit_date)),
            b'' if self.post_author is None or self.post_author is False else (self.serialize_bytes(self.post_author)),
            b'' if self.grouped_id is None or self.grouped_id is False else (struct.pack('<q', self.grouped_id)),
            b'' if self.restriction_reason is None or self.restriction_reason is False else b''.join((b'\x15\xc4\xb5\x1c',struct.pack('<i', len(self.restriction_reason)),b''.join(x._bytes() for x in self.restriction_reason))),
        ))

    @classmethod
    def from_reader(cls, reader):
        flags = reader.read_int()

        _out = bool(flags & 2)
        _mentioned = bool(flags & 16)
        _media_unread = bool(flags & 32)
        _silent = bool(flags & 8192)
        _post = bool(flags & 16384)
        _from_scheduled = bool(flags & 262144)
        _legacy = bool(flags & 524288)
        _edit_hide = bool(flags & 2097152)
        _id = reader.read_int()
        if flags & 256:
            _from_id = reader.tgread_object()
        else:
            _from_id = None
        _peer_id = reader.tgread_object()
        if flags & 4:
            _fwd_from = reader.tgread_object()
        else:
            _fwd_from = None
        if flags & 2048:
            _via_bot_id = reader.read_int()
        else:
            _via_bot_id = None
        if flags & 8:
            _reply_to = reader.tgread_object()
        else:
            _reply_to = None
        _date = reader.tgread_date()
        _message = reader.tgread_string()
        if flags & 512:
            _media = reader.tgread_object()
        else:
            _media = None
        if flags & 64:
            _reply_markup = reader.tgread_object()
        else:
            _reply_markup = None
        if flags & 128:
            reader.read_int()
            _entities = []
            for _ in range(reader.read_int()):
                _x = reader.tgread_object()
                _entities.append(_x)

        else:
            _entities = None
        if flags & 1024:
            _views = reader.read_int()
        else:
            _views = None
        if flags & 1024:
            _forwards = reader.read_int()
        else:
            _forwards = None
        if flags & 8388608:
            _replies = reader.tgread_object()
        else:
            _replies = None
        if flags & 32768:
            _edit_date = reader.tgread_date()
        else:
            _edit_date = None
        if flags & 65536:
            _post_author = reader.tgread_string()
        else:
            _post_author = None
        if flags & 131072:
            _grouped_id = reader.read_long()
        else:
            _grouped_id = None
        if flags & 4194304:
            reader.read_int()
            _restriction_reason = []
            for _ in range(reader.read_int()):
                _x = reader.tgread_object()
                _restriction_reason.append(_x)

        else:
            _restriction_reason = None
        return cls(id=_id, peer_id=_peer_id, date=_date, message=_message, out=_out, mentioned=_mentioned, media_unread=_media_unread, silent=_silent, post=_post, from_scheduled=_from_scheduled, legacy=_legacy, edit_hide=_edit_hide, from_id=_from_id, fwd_from=_fwd_from, via_bot_id=_via_bot_id, reply_to=_reply_to, media=_media, reply_markup=_reply_markup, entities=_entities, views=_views, forwards=_forwards, replies=_replies, edit_date=_edit_date, post_author=_post_author, grouped_id=_grouped_id, restriction_reason=_restriction_reason)

types.Message = Message

class MessageService(custom.message.Message):
    CONSTRUCTOR_ID = 0x286fa604
    SUBCLASS_OF_ID = 0x790009e3
    def to_dict(self):
        return {
            '_': 'MessageService',
            'id': self.id,
            'peer_id': self.peer_id.to_dict() if isinstance(self.peer_id, TLObject) else self.peer_id,
            'date': self.date,
            'action': self.action.to_dict() if isinstance(self.action, TLObject) else self.action,
            'out': self.out,
            'mentioned': self.mentioned,
            'media_unread': self.media_unread,
            'silent': self.silent,
            'post': self.post,
            'legacy': self.legacy,
            'from_id': self.from_id.to_dict() if isinstance(self.from_id, TLObject) else self.from_id,
            'reply_to': self.reply_to.to_dict() if isinstance(self.reply_to, TLObject) else self.reply_to
        }

    def _bytes(self):
        return b''.join((
            b'\x04\xa6o(',
            struct.pack('<I', (0 if self.out is None or self.out is False else 2) | (0 if self.mentioned is None or self.mentioned is False else 16) | (0 if self.media_unread is None or self.media_unread is False else 32) | (0 if self.silent is None or self.silent is False else 8192) | (0 if self.post is None or self.post is False else 16384) | (0 if self.legacy is None or self.legacy is False else 524288) | (0 if self.from_id is None or self.from_id is False else 256) | (0 if self.reply_to is None or self.reply_to is False else 8)),
            struct.pack('<i', self.id),
            b'' if self.from_id is None or self.from_id is False else (self.from_id._bytes()),
            self.peer_id._bytes(),
            b'' if self.reply_to is None or self.reply_to is False else (self.reply_to._bytes()),
            self.serialize_datetime(self.date),
            self.action._bytes(),
        ))

    @classmethod
    def from_reader(cls, reader):
        flags = reader.read_int()

        _out = bool(flags & 2)
        _mentioned = bool(flags & 16)
        _media_unread = bool(flags & 32)
        _silent = bool(flags & 8192)
        _post = bool(flags & 16384)
        _legacy = bool(flags & 524288)
        _id = reader.read_int()
        if flags & 256:
            _from_id = reader.tgread_object()
        else:
            _from_id = None
        _peer_id = reader.tgread_object()
        if flags & 8:
            _reply_to = reader.tgread_object()
        else:
            _reply_to = None
        _date = reader.tgread_date()
        _action = reader.tgread_object()
        return cls(id=_id, peer_id=_peer_id, date=_date, action=_action, out=_out, mentioned=_mentioned, media_unread=_media_unread, silent=_silent, post=_post, legacy=_legacy, from_id=_from_id, reply_to=_reply_to)

types.MessageService = MessageService

