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

namespace MyTelegram.Schema;

///<summary>
/// Forwarded document
/// See <a href="https://corefork.telegram.org/constructor/inputMediaDocument" />
///</summary>
[TlObject(0xa8763ab5)]
public sealed class TInputMediaDocument : IInputMedia
{
    public uint ConstructorId => 0xa8763ab5;
    ///<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 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 document to be forwarded.
    /// See <a href="https://corefork.telegram.org/type/InputDocument" />
    ///</summary>
    public MyTelegram.Schema.IInputDocument Id { get; set; }
    public MyTelegram.Schema.IInputPhoto? VideoCover { get; set; }
    public int? VideoTimestamp { get; set; }

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

    ///<summary>
    /// Text query or emoji that was used by the user to find this sticker or GIF: used to improve search result relevance.
    ///</summary>
    public string? Query { get; set; }

    public void ComputeFlag()
    {
        if (Spoiler) { Flags = Flags.SetBit(2); }
        if (VideoCover != null) { Flags = Flags.SetBit(3); }
        if (/*VideoTimestamp != 0 && */VideoTimestamp.HasValue) { Flags = Flags.SetBit(4); }
        if (/*TtlSeconds != 0 && */TtlSeconds.HasValue) { Flags = Flags.SetBit(0); }
        if (Query != null) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        if (Flags.IsBitSet(3)) { writer.Write(VideoCover); }
        if (Flags.IsBitSet(4)) { writer.Write(VideoTimestamp.Value); }
        if (Flags.IsBitSet(0)) { writer.Write(TtlSeconds.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(Query); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { Spoiler = true; }
        Id = buffer.Read<MyTelegram.Schema.IInputDocument>();
        if (Flags.IsBitSet(3)) { VideoCover = buffer.Read<MyTelegram.Schema.IInputPhoto>(); }
        if (Flags.IsBitSet(4)) { VideoTimestamp = buffer.ReadInt32(); }
        if (Flags.IsBitSet(0)) { TtlSeconds = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { Query = buffer.ReadString(); }
    }
}