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

namespace MyTelegram.Schema;

///<summary>
/// Defines a video
/// See <a href="https://corefork.telegram.org/constructor/documentAttributeVideo" />
///</summary>
[TlObject(0x43c57c48)]
public sealed class TDocumentAttributeVideo : IDocumentAttribute
{
    public uint ConstructorId => 0x43c57c48;
    ///<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 round video
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool RoundMessage { get; set; }

    ///<summary>
    /// Whether the video supports streaming
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool SupportsStreaming { get; set; }

    ///<summary>
    /// Whether the specified document is a video file with no audio tracks (a GIF animation (even as MPEG4), for example)
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Nosound { get; set; }

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

    ///<summary>
    /// Video width
    ///</summary>
    public int W { get; set; }

    ///<summary>
    /// Video height
    ///</summary>
    public int H { get; set; }

    ///<summary>
    /// Number of bytes to preload when preloading videos (particularly <a href="https://corefork.telegram.org/api/stories">video stories</a>).
    ///</summary>
    public int? PreloadPrefixSize { get; set; }

    ///<summary>
    /// Floating point UNIX timestamp in seconds, indicating the frame of the video that should be used as static preview and thumbnail.
    ///</summary>
    public double? VideoStartTs { get; set; }

    ///<summary>
    /// Codec used for the video, i.e. "h264", "h265", or "av1"
    ///</summary>
    public string? VideoCodec { get; set; }

    public void ComputeFlag()
    {
        if (RoundMessage) { Flags = Flags.SetBit(0); }
        if (SupportsStreaming) { Flags = Flags.SetBit(1); }
        if (Nosound) { Flags = Flags.SetBit(3); }
        if (/*PreloadPrefixSize != 0 && */PreloadPrefixSize.HasValue) { Flags = Flags.SetBit(2); }
        if (VideoStartTs>0) { Flags = Flags.SetBit(4); }
        if (VideoCodec != null) { Flags = Flags.SetBit(5); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Duration);
        writer.Write(W);
        writer.Write(H);
        if (Flags.IsBitSet(2)) { writer.Write(PreloadPrefixSize.Value); }
        if (Flags.IsBitSet(4)) { writer.Write(VideoStartTs.Value); }
        if (Flags.IsBitSet(5)) { writer.Write(VideoCodec); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { RoundMessage = true; }
        if (Flags.IsBitSet(1)) { SupportsStreaming = true; }
        if (Flags.IsBitSet(3)) { Nosound = true; }
        Duration = buffer.ReadDouble();
        W = buffer.ReadInt32();
        H = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { PreloadPrefixSize = buffer.ReadInt32(); }
        if (Flags.IsBitSet(4)) { VideoStartTs = buffer.ReadDouble(); }
        if (Flags.IsBitSet(5)) { VideoCodec = buffer.ReadString(); }
    }
}