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

namespace MyTelegram.Schema;

///<summary>
/// New photo that will be uploaded by the server using the specified URL
/// See <a href="https://corefork.telegram.org/constructor/inputMediaPhotoExternal" />
///</summary>
[TlObject(0xe5bbfe1a)]
public sealed class TInputMediaPhotoExternal : IInputMedia
{
    public uint ConstructorId => 0xe5bbfe1a;
    ///<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 photo
    ///</summary>
    public string Url { get; set; }

    ///<summary>
    /// Self-destruct time to live of photo
    ///</summary>
    public int? TtlSeconds { get; set; }

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

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Url);
        if (Flags.IsBitSet(0)) { writer.Write(TtlSeconds.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(); }
    }
}