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

namespace MyTelegram.Schema;

///<summary>
/// Paid media preview for not yet purchased paid media, <a href="https://corefork.telegram.org/api/paid-media">see here »</a> for more info.
/// See <a href="https://corefork.telegram.org/constructor/messageExtendedMediaPreview" />
///</summary>
[TlObject(0xad628cc8)]
public sealed class TMessageExtendedMediaPreview : IMessageExtendedMedia
{
    public uint ConstructorId => 0xad628cc8;
    ///<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>
    /// Width
    ///</summary>
    public int? W { get; set; }

    ///<summary>
    /// Height
    ///</summary>
    public int? H { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/files#stripped-thumbnails">Extremely low resolution thumbnail</a>.
    /// See <a href="https://corefork.telegram.org/type/PhotoSize" />
    ///</summary>
    public MyTelegram.Schema.IPhotoSize? Thumb { get; set; }

    ///<summary>
    /// Video duration for videos.
    ///</summary>
    public int? VideoDuration { get; set; }

    public void ComputeFlag()
    {
        if (/*W != 0 && */W.HasValue) { Flags = Flags.SetBit(0); }
        if (/*H != 0 && */H.HasValue) { Flags = Flags.SetBit(0); }
        if (Thumb != null) { Flags = Flags.SetBit(1); }
        if (/*VideoDuration != 0 && */VideoDuration.HasValue) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(W.Value); }
        if (Flags.IsBitSet(0)) { writer.Write(H.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(Thumb); }
        if (Flags.IsBitSet(2)) { writer.Write(VideoDuration.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { W = buffer.ReadInt32(); }
        if (Flags.IsBitSet(0)) { H = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { Thumb = buffer.Read<MyTelegram.Schema.IPhotoSize>(); }
        if (Flags.IsBitSet(2)) { VideoDuration = buffer.ReadInt32(); }
    }
}