from telethon.tl.mtproto_request import MTProtoRequest


class ChatInvite(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    chatInvite#db74f558 flags:None channel:flags.0?true broadcast:flags.1?true public:flags.2?true megagroup:flags.3?true title:string photo:ChatPhoto participants_count:int participants:flags.4?Vector<User> = ChatInvite"""

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

    def __init__(self, title, photo, participants_count, channel=None, broadcast=None, public=None, megagroup=None, participants=None):
        """
        :param channel: Telegram type: «true».
        :param broadcast: Telegram type: «true».
        :param public: Telegram type: «true».
        :param megagroup: Telegram type: «true».
        :param title: Telegram type: «string».
        :param photo: Telegram type: «ChatPhoto».
        :param participants_count: Telegram type: «int».
        :param participants: Telegram type: «User». Must be a list.
        """
        super().__init__()

        self.channel = channel
        self.broadcast = broadcast
        self.public = public
        self.megagroup = megagroup
        self.title = title
        self.photo = photo
        self.participants_count = participants_count
        self.participants = participants

    def on_send(self, writer):
        writer.write_int(ChatInvite.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 0) if self.channel else 0
        flags |= (1 << 1) if self.broadcast else 0
        flags |= (1 << 2) if self.public else 0
        flags |= (1 << 3) if self.megagroup else 0
        flags |= (1 << 4) if self.participants else 0
        writer.write_int(flags)

        writer.tgwrite_string(self.title)
        self.photo.on_send(writer)
        writer.write_int(self.participants_count)
        if self.participants:
            writer.write_int(0x1cb5c415, signed=False)  # Vector's constructor ID
            writer.write_int(len(self.participants))
            for participants_item in self.participants:
                if participants_item:
                    participants_item.on_send(writer)

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

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

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

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

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

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

        self.title = reader.tgread_string()
        self.photo = reader.tgread_object()
        self.participants_count = reader.read_int()
        if (flags & (1 << 4)) != 0:
            reader.read_int()  # Vector's constructor ID
            self.participants = []  # Initialize an empty list
            participants_len = reader.read_int()
            for _ in range(participants_len):
                participants_item = reader.tgread_object()
                self.participants.append(participants_item)

    def __repr__(self):
        return 'chatInvite#db74f558 flags:None channel:flags.0?true broadcast:flags.1?true public:flags.2?true megagroup:flags.3?true title:string photo:ChatPhoto participants_count:int participants:flags.4?Vector<User> = ChatInvite'

    def __str__(self):
        return '(chatInvite (ID: 0xdb74f558) = (channel={}, broadcast={}, public={}, megagroup={}, title={}, photo={}, participants_count={}, participants={}))'.format(str(self.channel), str(self.broadcast), str(self.public), str(self.megagroup), str(self.title), str(self.photo), str(self.participants_count), None if not self.participants else [str(_) for _ in self.participants])
