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

namespace MyTelegram.Schema;

///<summary>
/// Document that will be downloaded by the telegram servers
/// See <a href="https://corefork.telegram.org/constructor/inputMediaDocumentExternal" />
///</summary>
[TlObject(0x779600f9)]
public sealed class TInputMediaDocumentExternal : IInputMedia
{
    public uint ConstructorId => 0x779600f9;
    ///<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>
    /// URL of the document
    ///</summary>
    public string Url { get; set; }

    ///<summary>
    /// Self-destruct time to live of document
    ///</summary>
    public int? TtlSeconds { get; set; }
    public MyTelegram.Schema.IInputPhoto? VideoCover { get; set; }
    public int? VideoTimestamp { get; set; }

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

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

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