from ....tl.mtproto_request import MTProtoRequest
from ....utils import get_input_peer


class SearchRequest(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. TL definition below.
    messages.search#f288a275 flags:# peer:InputPeer q:string from_id:flags.0?InputUser filter:MessagesFilter min_date:int max_date:int offset:int max_id:int limit:int = messages.Messages"""

    # Telegram's constructor (U)ID for this class
    constructor_id = 0xf288a275
    # Also the ID of its resulting type for fast checks
    subclass_of_id = 0xd4b40b5e

    def __init__(self, peer, q, filter, min_date, max_date, offset, max_id, limit, from_id=None):
        """
        :param peer: Telegram type: "InputPeer".
        :param q: Telegram type: "string".
        :param from_id: Telegram type: "InputUser".
        :param filter: Telegram type: "MessagesFilter".
        :param min_date: Telegram type: "date".
        :param max_date: Telegram type: "date".
        :param offset: Telegram type: "int".
        :param max_id: Telegram type: "int".
        :param limit: Telegram type: "int".

        :returns messages.Messages: Instance of either Messages, MessagesSlice, ChannelMessages.
        """
        super().__init__()
        self.result = None
        self.confirmed = True  # Confirmed by default

        self.peer = get_input_peer(peer)
        self.q = q
        self.from_id = from_id
        self.filter = filter
        self.min_date = min_date
        self.max_date = max_date
        self.offset = offset
        self.max_id = max_id
        self.limit = limit

    def to_dict(self):
        return {
            'peer': None if self.peer is None else self.peer.to_dict(),
            'q': self.q,
            'from_id': None if self.from_id is None else self.from_id.to_dict(),
            'filter': None if self.filter is None else self.filter.to_dict(),
            'min_date': self.min_date,
            'max_date': self.max_date,
            'offset': self.offset,
            'max_id': self.max_id,
            'limit': self.limit,
        }

    def on_send(self, writer):
        writer.write_int(SearchRequest.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
        writer.write_int(flags)

        self.peer.on_send(writer)
        writer.tgwrite_string(self.q)
        if self.from_id:
            self.from_id.on_send(writer)

        self.filter.on_send(writer)
        writer.tgwrite_date(self.min_date)
        writer.tgwrite_date(self.max_date)
        writer.write_int(self.offset)
        writer.write_int(self.max_id)
        writer.write_int(self.limit)

    @staticmethod
    def empty():
        """Returns an "empty" instance (attributes=None)"""
        return SearchRequest(None, None, None, None, None, None, None, None, None)

    def on_response(self, reader):
        self.result = reader.tgread_object()

    def __repr__(self):
        return 'messages.search#f288a275 flags:# peer:InputPeer q:string from_id:flags.0?InputUser filter:MessagesFilter min_date:int max_date:int offset:int max_id:int limit:int = messages.Messages'

    def __str__(self):
        return '(messages.search (ID: 0xf288a275) = (peer={}, q={}, from_id={}, filter={}, min_date={}, max_date={}, offset={}, max_id={}, limit={}))'.format(str(self.peer), str(self.q), str(self.from_id), str(self.filter), str(self.min_date), str(self.max_date), str(self.offset), str(self.max_id), str(self.limit))
