from ....tl.mtproto_request import MTProtoRequest


class SendPaymentFormRequest(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    payments.sendPaymentForm#2b8879b3 flags:# msg_id:int requested_info_id:flags.0?string shipping_option_id:flags.1?string credentials:InputPaymentCredentials = payments.PaymentResult"""

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

    def __init__(self, msg_id, credentials, requested_info_id=None, shipping_option_id=None):
        """
        :param msg_id: Telegram type: «int».
        :param requested_info_id: Telegram type: «string».
        :param shipping_option_id: Telegram type: «string».
        :param credentials: Telegram type: «InputPaymentCredentials».

        :returns payments.PaymentResult: Instance of either PaymentResult, PaymentVerficationNeeded.
        """
        super().__init__()
        self.result = None
        self.confirmed = True  # Confirmed by default

        self.msg_id = msg_id
        self.requested_info_id = requested_info_id
        self.shipping_option_id = shipping_option_id
        self.credentials = credentials

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

        writer.write_int(self.msg_id)
        if self.requested_info_id:
            writer.tgwrite_string(self.requested_info_id)

        if self.shipping_option_id:
            writer.tgwrite_string(self.shipping_option_id)

        self.credentials.on_send(writer)

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

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

    def __repr__(self):
        return 'payments.sendPaymentForm#2b8879b3 flags:# msg_id:int requested_info_id:flags.0?string shipping_option_id:flags.1?string credentials:InputPaymentCredentials = payments.PaymentResult'

    def __str__(self):
        return '(payments.sendPaymentForm (ID: 0x2b8879b3) = (msg_id={}, requested_info_id={}, shipping_option_id={}, credentials={}))'.format(str(self.msg_id), str(self.requested_info_id), str(self.shipping_option_id), str(self.credentials))
