from ...tl.tlobject import TLObject


class ChatInvite(TLObject):
    """Class generated by TLObjects' generator. All changes will be ERASED. TL definition below.
    chatInvite#db74f558 flags:# 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 (U)ID for this class
    constructor_id = 0xdb74f558
    # Also the ID of its resulting type for fast checks
    subclass_of_id = 0x4561736

    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.

        Constructor for ChatInvite: Instance of either ChatInviteAlready, ChatInvite.
        """
        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 to_dict(self):
        return {
            'channel': self.channel,
            'broadcast': self.broadcast,
            'public': self.public,
            'megagroup': self.megagroup,
            'title': self.title,
            'photo': None if self.photo is None else self.photo.to_dict(),
            'participants_count': self.participants_count,
            'participants': [] if self.participants is None else [None if x is None else x.to_dict() for x in self.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 _x in self.participants:
                if _x:
                    _x.on_send(writer)

    @staticmethod
    def empty():
        """Returns an "empty" instance (attributes=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
            _len = reader.read_int()
            for _ in range(_len):
                _x = reader.tgread_object()
                self.participants.append(_x)

    def __repr__(self):
        return 'chatInvite#db74f558 flags:# 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 TLObject.pretty_format(self)

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