from telethon.tl.mtproto_request import MTProtoRequest


class BotResults(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    messages.botResults#256709a6 flags:None gallery:flags.0?true query_id:long next_offset:flags.1?string switch_pm:flags.2?InlineBotSwitchPM results:Vector<BotInlineResult> = messages.BotResults"""

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

    def __init__(self, query_id, results, gallery=None, next_offset=None, switch_pm=None):
        """
        :param gallery: Telegram type: «true».
        :param query_id: Telegram type: «long».
        :param next_offset: Telegram type: «string».
        :param switch_pm: Telegram type: «InlineBotSwitchPM».
        :param results: Telegram type: «BotInlineResult». Must be a list.
        """
        super().__init__()

        self.gallery = gallery
        self.query_id = query_id
        self.next_offset = next_offset
        self.switch_pm = switch_pm
        self.results = results

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

        writer.write_long(self.query_id)
        if self.next_offset:
            writer.tgwrite_string(self.next_offset)

        if self.switch_pm:
            self.switch_pm.on_send(writer)

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

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

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

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

        self.query_id = reader.read_long()
        if (flags & (1 << 1)) != 0:
            self.next_offset = reader.tgread_string()

        if (flags & (1 << 2)) != 0:
            self.switch_pm = reader.tgread_object()

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

    def __repr__(self):
        return 'messages.botResults#256709a6 flags:None gallery:flags.0?true query_id:long next_offset:flags.1?string switch_pm:flags.2?InlineBotSwitchPM results:Vector<BotInlineResult> = messages.BotResults'

    def __str__(self):
        return '(messages.botResults (ID: 0x256709a6) = (gallery={}, query_id={}, next_offset={}, switch_pm={}, results={}))'.format(str(self.gallery), str(self.query_id), str(self.next_offset), str(self.switch_pm), None if not self.results else [str(_) for _ in self.results])
