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

namespace MyTelegram.Schema;

///<summary>
/// New document
/// See <a href="https://corefork.telegram.org/constructor/inputMediaUploadedDocument" />
///</summary>
[TlObject(0x37c9330)]
public sealed class TInputMediaUploadedDocument : IInputMedia
{
    public uint ConstructorId => 0x37c9330;
    ///<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 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 NosoundVideo { get; set; }

    ///<summary>
    /// Force the media file to be uploaded as document
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ForceFile { 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>
    /// The <a href="https://corefork.telegram.org/api/files">uploaded file</a>
    /// See <a href="https://corefork.telegram.org/type/InputFile" />
    ///</summary>
    public MyTelegram.Schema.IInputFile File { get; set; }

    ///<summary>
    /// Thumbnail of the document, uploaded as for the file
    /// See <a href="https://corefork.telegram.org/type/InputFile" />
    ///</summary>
    public MyTelegram.Schema.IInputFile? Thumb { get; set; }

    ///<summary>
    /// MIME type of document
    ///</summary>
    public string MimeType { get; set; }

    ///<summary>
    /// Attributes that specify the type of the document (video, audio, voice, sticker, etc.)
    ///</summary>
    public TVector<MyTelegram.Schema.IDocumentAttribute> Attributes { get; set; }

    ///<summary>
    /// Attached stickers
    ///</summary>
    public TVector<MyTelegram.Schema.IInputDocument>? Stickers { get; set; }
    public MyTelegram.Schema.IInputPhoto? VideoCover { get; set; }
    public int? VideoTimestamp { get; set; }

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

    public void ComputeFlag()
    {
        if (NosoundVideo) { Flags = Flags.SetBit(3); }
        if (ForceFile) { Flags = Flags.SetBit(4); }
        if (Spoiler) { Flags = Flags.SetBit(5); }
        if (Thumb != null) { Flags = Flags.SetBit(2); }
        if (Stickers?.Count > 0) { Flags = Flags.SetBit(0); }
        if (VideoCover != null) { Flags = Flags.SetBit(6); }
        if (/*VideoTimestamp != 0 && */VideoTimestamp.HasValue) { Flags = Flags.SetBit(7); }
        if (/*TtlSeconds != 0 && */TtlSeconds.HasValue) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(File);
        if (Flags.IsBitSet(2)) { writer.Write(Thumb); }
        writer.Write(MimeType);
        writer.Write(Attributes);
        if (Flags.IsBitSet(0)) { writer.Write(Stickers); }
        if (Flags.IsBitSet(6)) { writer.Write(VideoCover); }
        if (Flags.IsBitSet(7)) { writer.Write(VideoTimestamp.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(TtlSeconds.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { NosoundVideo = true; }
        if (Flags.IsBitSet(4)) { ForceFile = true; }
        if (Flags.IsBitSet(5)) { Spoiler = true; }
        File = buffer.Read<MyTelegram.Schema.IInputFile>();
        if (Flags.IsBitSet(2)) { Thumb = buffer.Read<MyTelegram.Schema.IInputFile>(); }
        MimeType = buffer.ReadString();
        Attributes = buffer.Read<TVector<MyTelegram.Schema.IDocumentAttribute>>();
        if (Flags.IsBitSet(0)) { Stickers = buffer.Read<TVector<MyTelegram.Schema.IInputDocument>>(); }
        if (Flags.IsBitSet(6)) { VideoCover = buffer.Read<MyTelegram.Schema.IInputPhoto>(); }
        if (Flags.IsBitSet(7)) { VideoTimestamp = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { TtlSeconds = buffer.ReadInt32(); }
    }
}