from telethon.tl.mtproto_request import MTProtoRequest


class MessageFwdHeader(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    messageFwdHeader#c786ddcb flags:None from_id:flags.0?int date:date channel_id:flags.1?int channel_post:flags.2?int = MessageFwdHeader"""

    # Telegram's constructor ID (and unique identifier) for this class
    constructor_id = 0xc786ddcb

    def __init__(self, date, from_id=None, channel_id=None, channel_post=None):
        """
        :param from_id: Telegram type: «int».
        :param date: Telegram type: «date».
        :param channel_id: Telegram type: «int».
        :param channel_post: Telegram type: «int».
        """
        super().__init__()

        self.from_id = from_id
        self.date = date
        self.channel_id = channel_id
        self.channel_post = channel_post

    def on_send(self, writer):
        writer.write_int(MessageFwdHeader.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 0) if self.from_id else 0
        flags |= (1 << 1) if self.channel_id else 0
        flags |= (1 << 2) if self.channel_post else 0
        writer.write_int(flags)

        if self.from_id:
            writer.write_int(self.from_id)

        writer.tgwrite_date(self.date)
        if self.channel_id:
            writer.write_int(self.channel_id)

        if self.channel_post:
            writer.write_int(self.channel_post)

    @staticmethod
    def empty():
        """Returns an "empty" instance (all attributes are None)"""
        return MessageFwdHeader(None, None, None, None)

    def on_response(self, reader):
        flags = reader.read_int()

        if (flags & (1 << 0)) != 0:
            self.from_id = reader.read_int()

        self.date = reader.tgread_date()
        if (flags & (1 << 1)) != 0:
            self.channel_id = reader.read_int()

        if (flags & (1 << 2)) != 0:
            self.channel_post = reader.read_int()

    def __repr__(self):
        return 'messageFwdHeader#c786ddcb flags:None from_id:flags.0?int date:date channel_id:flags.1?int channel_post:flags.2?int = MessageFwdHeader'

    def __str__(self):
        return '(messageFwdHeader (ID: 0xc786ddcb) = (from_id={}, date={}, channel_id={}, channel_post={}))'.format(str(self.from_id), str(self.date), str(self.channel_id), str(self.channel_post))
