from ...tl.mtproto_request import MTProtoRequest


class MessageActionPhoneCall(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    messageActionPhoneCall#80e11a7f flags:# call_id:long reason:flags.0?PhoneCallDiscardReason duration:flags.1?int = MessageAction"""

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

    def __init__(self, call_id, reason=None, duration=None):
        """
        :param call_id: Telegram type: «long».
        :param reason: Telegram type: «PhoneCallDiscardReason».
        :param duration: Telegram type: «int».

        Constructor for MessageAction: Instance of either MessageActionEmpty, MessageActionChatCreate, MessageActionChatEditTitle, MessageActionChatEditPhoto, MessageActionChatDeletePhoto, MessageActionChatAddUser, MessageActionChatDeleteUser, MessageActionChatJoinedByLink, MessageActionChannelCreate, MessageActionChatMigrateTo, MessageActionChannelMigrateFrom, MessageActionPinMessage, MessageActionHistoryClear, MessageActionGameScore, MessageActionPaymentSentMe, MessageActionPaymentSent, MessageActionPhoneCall.
        """
        super().__init__()

        self.call_id = call_id
        self.reason = reason
        self.duration = duration

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

        writer.write_long(self.call_id)
        if self.reason:
            self.reason.on_send(writer)

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

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

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

        self.call_id = reader.read_long()
        if (flags & (1 << 0)) != 0:
            self.reason = reader.tgread_object()

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

    def __repr__(self):
        return 'messageActionPhoneCall#80e11a7f flags:# call_id:long reason:flags.0?PhoneCallDiscardReason duration:flags.1?int = MessageAction'

    def __str__(self):
        return '(messageActionPhoneCall (ID: 0x80e11a7f) = (call_id={}, reason={}, duration={}))'.format(str(self.call_id), str(self.reason), str(self.duration))
