from ....tl.tlobject import TLObject


class PaymentForm(TLObject):
    """Class generated by TLObjects' generator. All changes will be ERASED. TL definition below.
    payments.paymentForm#3f56aea3 flags:# can_save_credentials:flags.2?true password_missing:flags.3?true bot_id:int invoice:Invoice provider_id:int url:string native_provider:flags.4?string native_params:flags.4?DataJSON saved_info:flags.0?PaymentRequestedInfo saved_credentials:flags.1?PaymentSavedCredentials users:Vector<User> = payments.PaymentForm"""

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

    def __init__(self, bot_id, invoice, provider_id, url, users, can_save_credentials=None, password_missing=None, native_provider=None, native_params=None, saved_info=None, saved_credentials=None):
        """
        :param can_save_credentials: Telegram type: "true".
        :param password_missing: Telegram type: "true".
        :param bot_id: Telegram type: "int".
        :param invoice: Telegram type: "Invoice".
        :param provider_id: Telegram type: "int".
        :param url: Telegram type: "string".
        :param native_provider: Telegram type: "string".
        :param native_params: Telegram type: "DataJSON".
        :param saved_info: Telegram type: "PaymentRequestedInfo".
        :param saved_credentials: Telegram type: "PaymentSavedCredentials".
        :param users: Telegram type: "User". Must be a list.

        Constructor for payments.PaymentForm: Instance of PaymentForm.
        """
        super().__init__()

        self.can_save_credentials = can_save_credentials
        self.password_missing = password_missing
        self.bot_id = bot_id
        self.invoice = invoice
        self.provider_id = provider_id
        self.url = url
        self.native_provider = native_provider
        self.native_params = native_params
        self.saved_info = saved_info
        self.saved_credentials = saved_credentials
        self.users = users

    def to_dict(self):
        return {
            'can_save_credentials': self.can_save_credentials,
            'password_missing': self.password_missing,
            'bot_id': self.bot_id,
            'invoice': None if self.invoice is None else self.invoice.to_dict(),
            'provider_id': self.provider_id,
            'url': self.url,
            'native_provider': self.native_provider,
            'native_params': None if self.native_params is None else self.native_params.to_dict(),
            'saved_info': None if self.saved_info is None else self.saved_info.to_dict(),
            'saved_credentials': None if self.saved_credentials is None else self.saved_credentials.to_dict(),
            'users': [] if self.users is None else [None if x is None else x.to_dict() for x in self.users],
        }

    def on_send(self, writer):
        writer.write_int(PaymentForm.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 2) if self.can_save_credentials else 0
        flags |= (1 << 3) if self.password_missing else 0
        flags |= (1 << 4) if self.native_provider else 0
        flags |= (1 << 4) if self.native_params else 0
        flags |= (1 << 0) if self.saved_info else 0
        flags |= (1 << 1) if self.saved_credentials else 0
        writer.write_int(flags)

        writer.write_int(self.bot_id)
        self.invoice.on_send(writer)
        writer.write_int(self.provider_id)
        writer.tgwrite_string(self.url)
        if self.native_provider:
            writer.tgwrite_string(self.native_provider)

        if self.native_params:
            self.native_params.on_send(writer)

        if self.saved_info:
            self.saved_info.on_send(writer)

        if self.saved_credentials:
            self.saved_credentials.on_send(writer)

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

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

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

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

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

        self.bot_id = reader.read_int()
        self.invoice = reader.tgread_object()
        self.provider_id = reader.read_int()
        self.url = reader.tgread_string()
        if (flags & (1 << 4)) != 0:
            self.native_provider = reader.tgread_string()

        if (flags & (1 << 4)) != 0:
            self.native_params = reader.tgread_object()

        if (flags & (1 << 0)) != 0:
            self.saved_info = reader.tgread_object()

        if (flags & (1 << 1)) != 0:
            self.saved_credentials = reader.tgread_object()

        reader.read_int()  # Vector's constructor ID
        self.users = []  # Initialize an empty list
        _len = reader.read_int()
        for _ in range(_len):
            _x = reader.tgread_object()
            self.users.append(_x)

    def __repr__(self):
        return 'payments.paymentForm#3f56aea3 flags:# can_save_credentials:flags.2?true password_missing:flags.3?true bot_id:int invoice:Invoice provider_id:int url:string native_provider:flags.4?string native_params:flags.4?DataJSON saved_info:flags.0?PaymentRequestedInfo saved_credentials:flags.1?PaymentSavedCredentials users:Vector<User> = payments.PaymentForm'

    def __str__(self):
        return TLObject.pretty_format(self)

    def stringify(self):
        return TLObject.pretty_format(self, indent=0)
