from ...tl.mtproto_request import MTProtoRequest


class PhoneCallDiscarded(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    phoneCallDiscarded#50ca4de1 flags:# need_rating:flags.2?true need_debug:flags.3?true id:long reason:flags.0?PhoneCallDiscardReason duration:flags.1?int = PhoneCall"""

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

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

        Constructor for PhoneCall: Instance of either PhoneCallEmpty, PhoneCallWaiting, PhoneCallRequested, PhoneCallAccepted, PhoneCall, PhoneCallDiscarded.
        """
        super().__init__()

        self.need_rating = need_rating
        self.need_debug = need_debug
        self.id = id
        self.reason = reason
        self.duration = duration

    def on_send(self, writer):
        writer.write_int(PhoneCallDiscarded.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 2) if self.need_rating else 0
        flags |= (1 << 3) if self.need_debug else 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.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 PhoneCallDiscarded(None, None, None, None, None)

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

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

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

        self.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 'phoneCallDiscarded#50ca4de1 flags:# need_rating:flags.2?true need_debug:flags.3?true id:long reason:flags.0?PhoneCallDiscardReason duration:flags.1?int = PhoneCall'

    def __str__(self):
        return '(phoneCallDiscarded (ID: 0x50ca4de1) = (need_rating={}, need_debug={}, id={}, reason={}, duration={}))'.format(str(self.need_rating), str(self.need_debug), str(self.id), str(self.reason), str(self.duration))
