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

namespace MyTelegram.Schema;

///<summary>
/// Document (video, audio, voice, sticker, any media type except photo)
/// See <a href="https://corefork.telegram.org/constructor/messageMediaDocument" />
///</summary>
[TlObject(0x52d8ccd9)]
public sealed class TMessageMediaDocument : IMessageMedia
{
    public uint ConstructorId => 0x52d8ccd9;
    ///<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 normal sticker, if not set this is a premium sticker and a premium sticker animation must be played.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Nopremium { get; set; }

    ///<summary>
    /// Whether this media should be hidden behind a spoiler warning
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Spoiler { get; set; }

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

    ///<summary>
    /// Whether this is a round video.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Round { 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>
    /// Attached document
    /// See <a href="https://corefork.telegram.org/type/Document" />
    ///</summary>
    public MyTelegram.Schema.IDocument? Document { get; set; }

    ///<summary>
    /// Videos only, contains alternative qualities of the video.
    ///</summary>
    public TVector<MyTelegram.Schema.IDocument>? AltDocuments { get; set; }
    public MyTelegram.Schema.IPhoto? VideoCover { get; set; }
    public int? VideoTimestamp { get; set; }

    ///<summary>
    /// Time to live of self-destructing document
    ///</summary>
    public int? TtlSeconds { get; set; }

    public void ComputeFlag()
    {
        if (Nopremium) { Flags = Flags.SetBit(3); }
        if (Spoiler) { Flags = Flags.SetBit(4); }
        if (Video) { Flags = Flags.SetBit(6); }
        if (Round) { Flags = Flags.SetBit(7); }
        if (Voice) { Flags = Flags.SetBit(8); }
        if (Document != null) { Flags = Flags.SetBit(0); }
        if (AltDocuments?.Count > 0) { Flags = Flags.SetBit(5); }
        if (VideoCover != null) { Flags = Flags.SetBit(9); }
        if (/*VideoTimestamp != 0 && */VideoTimestamp.HasValue) { Flags = Flags.SetBit(10); }
        if (/*TtlSeconds != 0 && */TtlSeconds.HasValue) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(Document); }
        if (Flags.IsBitSet(5)) { writer.Write(AltDocuments); }
        if (Flags.IsBitSet(9)) { writer.Write(VideoCover); }
        if (Flags.IsBitSet(10)) { writer.Write(VideoTimestamp.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(TtlSeconds.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { Nopremium = true; }
        if (Flags.IsBitSet(4)) { Spoiler = true; }
        if (Flags.IsBitSet(6)) { Video = true; }
        if (Flags.IsBitSet(7)) { Round = true; }
        if (Flags.IsBitSet(8)) { Voice = true; }
        if (Flags.IsBitSet(0)) { Document = buffer.Read<MyTelegram.Schema.IDocument>(); }
        if (Flags.IsBitSet(5)) { AltDocuments = buffer.Read<TVector<MyTelegram.Schema.IDocument>>(); }
        if (Flags.IsBitSet(9)) { VideoCover = buffer.Read<MyTelegram.Schema.IPhoto>(); }
        if (Flags.IsBitSet(10)) { VideoTimestamp = buffer.ReadInt32(); }
        if (Flags.IsBitSet(2)) { TtlSeconds = buffer.ReadInt32(); }
    }
}