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

namespace MyTelegram.Schema;

///<summary>
/// Attached photo.
/// See <a href="https://corefork.telegram.org/constructor/messageMediaPhoto" />
///</summary>
[TlObject(0x695150d7)]
public sealed class TMessageMediaPhoto : IMessageMedia
{
    public uint ConstructorId => 0x695150d7;
    ///<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>
    /// Photo
    /// See <a href="https://corefork.telegram.org/type/Photo" />
    ///</summary>
    public MyTelegram.Schema.IPhoto? Photo { get; set; }

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

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

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

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { Spoiler = true; }
        if (Flags.IsBitSet(0)) { Photo = buffer.Read<MyTelegram.Schema.IPhoto>(); }
        if (Flags.IsBitSet(2)) { TtlSeconds = buffer.ReadInt32(); }
    }
}