from telethon.tl.mtproto_request import MTProtoRequest


class ReplyKeyboardMarkup(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    replyKeyboardMarkup#3502758c flags:None resize:flags.0?true single_use:flags.1?true selective:flags.2?true rows:Vector<KeyboardButtonRow> = ReplyMarkup"""

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

    def __init__(self, rows, resize=None, single_use=None, selective=None):
        """
        :param resize: Telegram type: «true».
        :param single_use: Telegram type: «true».
        :param selective: Telegram type: «true».
        :param rows: Telegram type: «KeyboardButtonRow». Must be a list.
        """
        super().__init__()

        self.resize = resize
        self.single_use = single_use
        self.selective = selective
        self.rows = rows

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

        writer.write_int(0x1cb5c415, signed=False)  # Vector's constructor ID
        writer.write_int(len(self.rows))
        for rows_item in self.rows:
            rows_item.on_send(writer)

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

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

        if (flags & (1 << 0)) != 0:
            self.resize = True  # Arbitrary not-None value, no need to read since it is a flag

        if (flags & (1 << 1)) != 0:
            self.single_use = True  # Arbitrary not-None value, no need to read since it is a flag

        if (flags & (1 << 2)) != 0:
            self.selective = True  # Arbitrary not-None value, no need to read since it is a flag

        reader.read_int()  # Vector's constructor ID
        self.rows = []  # Initialize an empty list
        rows_len = reader.read_int()
        for _ in range(rows_len):
            rows_item = reader.tgread_object()
            self.rows.append(rows_item)

    def __repr__(self):
        return 'replyKeyboardMarkup#3502758c flags:None resize:flags.0?true single_use:flags.1?true selective:flags.2?true rows:Vector<KeyboardButtonRow> = ReplyMarkup'

    def __str__(self):
        return '(replyKeyboardMarkup (ID: 0x3502758c) = (resize={}, single_use={}, selective={}, rows={}))'.format(str(self.resize), str(self.single_use), str(self.selective), None if not self.rows else [str(_) for _ in self.rows])
