// <auto-generated/>
// ReSharper disable All

namespace MyTelegram.Schema;

///<summary>
/// Represents an audio file
/// See <a href="https://corefork.telegram.org/constructor/documentAttributeAudio" />
///</summary>
[TlObject(0x9852f9c6)]
public sealed class TDocumentAttributeAudio : IDocumentAttribute
{
    public uint ConstructorId => 0x9852f9c6;
    ///<summary>
    /// Flags, see <a href="https://corefork.telegram.org/mtproto/TL-combinators#conditional-fields">TL conditional fields</a>
    ///</summary>
    public int Flags { get; set; }

    ///<summary>
    /// Whether this is a voice message
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Voice { get; set; }

    ///<summary>
    /// Duration in seconds
    ///</summary>
    public int Duration { get; set; }

    ///<summary>
    /// Name of song
    ///</summary>
    public string? Title { get; set; }

    ///<summary>
    /// Performer
    ///</summary>
    public string? Performer { get; set; }

    ///<summary>
    /// Waveform: consists in a series of bitpacked 5-bit values. <br>Example implementation: <a href="https://github.com/DrKLO/Telegram/blob/96dce2c9aabc33b87db61d830aa087b6b03fe397/TMessagesProj/jni/audio.c#L546">android</a>.
    ///</summary>
    public ReadOnlyMemory<byte>? Waveform { get; set; }

    public void ComputeFlag()
    {
        if (Voice) { Flags = Flags.SetBit(10); }
        if (Title != null) { Flags = Flags.SetBit(0); }
        if (Performer != null) { Flags = Flags.SetBit(1); }
        if (Waveform != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Duration);
        if (Flags.IsBitSet(0)) { writer.Write(Title); }
        if (Flags.IsBitSet(1)) { writer.Write(Performer); }
        if (Flags.IsBitSet(2)) { writer.Write(Waveform); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(10)) { Voice = true; }
        Duration = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Title = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { Performer = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { Waveform = buffer.ReadBytes(); }
    }
}