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


class SendMessageRequest(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. TL definition below.
    messages.sendMessage#fa88427a flags:# no_webpage:flags.1?true silent:flags.5?true background:flags.6?true clear_draft:flags.7?true peer:InputPeer reply_to_msg_id:flags.0?int message:string random_id:long reply_markup:flags.2?ReplyMarkup entities:flags.3?Vector<MessageEntity> = Updates"""

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

    def __init__(self, peer, message, no_webpage=None, silent=None, background=None, clear_draft=None, reply_to_msg_id=None, random_id=None, reply_markup=None, entities=None):
        """
        :param no_webpage: Telegram type: "true".
        :param silent: Telegram type: "true".
        :param background: Telegram type: "true".
        :param clear_draft: Telegram type: "true".
        :param peer: Telegram type: "InputPeer".
        :param reply_to_msg_id: Telegram type: "int".
        :param message: Telegram type: "string".
        :param random_id: Telegram type: "long".
        :param reply_markup: Telegram type: "ReplyMarkup".
        :param entities: Telegram type: "MessageEntity". Must be a list.

        :returns Updates: Instance of either UpdatesTooLong, UpdateShortMessage, UpdateShortChatMessage, UpdateShort, UpdatesCombined, UpdatesTg, UpdateShortSentMessage.
        """
        super().__init__()
        self.result = None
        self.confirmed = True  # Confirmed by default

        self.no_webpage = no_webpage
        self.silent = silent
        self.background = background
        self.clear_draft = clear_draft
        self.peer = get_input_peer(peer)
        self.reply_to_msg_id = reply_to_msg_id
        self.message = message
        self.random_id = random_id if random_id is not None else int.from_bytes(os.urandom(8), signed=True, byteorder='little')
        self.reply_markup = reply_markup
        self.entities = entities

    def to_dict(self):
        return {
            'no_webpage': self.no_webpage,
            'silent': self.silent,
            'background': self.background,
            'clear_draft': self.clear_draft,
            'peer': None if self.peer is None else self.peer.to_dict(),
            'reply_to_msg_id': self.reply_to_msg_id,
            'message': self.message,
            'random_id': self.random_id,
            'reply_markup': None if self.reply_markup is None else self.reply_markup.to_dict(),
            'entities': [] if self.entities is None else [None if x is None else x.to_dict() for x in self.entities],
        }

    def on_send(self, writer):
        writer.write_int(SendMessageRequest.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 1) if self.no_webpage else 0
        flags |= (1 << 5) if self.silent else 0
        flags |= (1 << 6) if self.background else 0
        flags |= (1 << 7) if self.clear_draft else 0
        flags |= (1 << 0) if self.reply_to_msg_id else 0
        flags |= (1 << 2) if self.reply_markup else 0
        flags |= (1 << 3) if self.entities else 0
        writer.write_int(flags)

        self.peer.on_send(writer)
        if self.reply_to_msg_id:
            writer.write_int(self.reply_to_msg_id)

        writer.tgwrite_string(self.message)
        writer.write_long(self.random_id)
        if self.reply_markup:
            self.reply_markup.on_send(writer)

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

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

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

    def __repr__(self):
        return 'messages.sendMessage#fa88427a flags:# no_webpage:flags.1?true silent:flags.5?true background:flags.6?true clear_draft:flags.7?true peer:InputPeer reply_to_msg_id:flags.0?int message:string random_id:long reply_markup:flags.2?ReplyMarkup entities:flags.3?Vector<MessageEntity> = Updates'

    def __str__(self):
        return '(messages.sendMessage (ID: 0xfa88427a) = (no_webpage={}, silent={}, background={}, clear_draft={}, peer={}, reply_to_msg_id={}, message={}, random_id={}, reply_markup={}, entities={}))'.format(str(self.no_webpage), str(self.silent), str(self.background), str(self.clear_draft), str(self.peer), str(self.reply_to_msg_id), str(self.message), str(self.random_id), str(self.reply_markup), None if not self.entities else [str(_) for _ in self.entities])
