from telethon.tl.mtproto_request import MTProtoRequest


class DocumentAttributeAudio(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    documentAttributeAudio#9852f9c6 flags:None voice:flags.10?true duration:int title:flags.0?string performer:flags.1?string waveform:flags.2?bytes = DocumentAttribute"""

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

    def __init__(self, duration, voice=None, title=None, performer=None, waveform=None):
        """
        :param voice: Telegram type: «true».
        :param duration: Telegram type: «int».
        :param title: Telegram type: «string».
        :param performer: Telegram type: «string».
        :param waveform: Telegram type: «bytes».
        """
        super().__init__()

        self.voice = voice
        self.duration = duration
        self.title = title
        self.performer = performer
        self.waveform = waveform

    def on_send(self, writer):
        writer.write_int(DocumentAttributeAudio.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 10) if self.voice else 0
        flags |= (1 << 0) if self.title else 0
        flags |= (1 << 1) if self.performer else 0
        flags |= (1 << 2) if self.waveform else 0
        writer.write_int(flags)

        writer.write_int(self.duration)
        if self.title:
            writer.tgwrite_string(self.title)

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

        if self.waveform:
            writer.tgwrite_bytes(self.waveform)

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

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

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

        self.duration = reader.read_int()
        if (flags & (1 << 0)) != 0:
            self.title = reader.tgread_string()

        if (flags & (1 << 1)) != 0:
            self.performer = reader.tgread_string()

        if (flags & (1 << 2)) != 0:
            self.waveform = reader.tgread_bytes()

    def __repr__(self):
        return 'documentAttributeAudio#9852f9c6 flags:None voice:flags.10?true duration:int title:flags.0?string performer:flags.1?string waveform:flags.2?bytes = DocumentAttribute'

    def __str__(self):
        return '(documentAttributeAudio (ID: 0x9852f9c6) = (voice={}, duration={}, title={}, performer={}, waveform={}))'.format(str(self.voice), str(self.duration), str(self.title), str(self.performer), str(self.waveform))
