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

namespace MyTelegram.Schema;

///<summary>
/// Photo
/// See <a href="https://corefork.telegram.org/constructor/photo" />
///</summary>
[TlObject(0xfb197a65)]
public sealed class TPhoto : IPhoto
{
    public uint ConstructorId => 0xfb197a65;
    ///<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 the photo has mask stickers attached to it
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool HasStickers { get; set; }

    ///<summary>
    /// ID
    ///</summary>
    public long Id { get; set; }

    ///<summary>
    /// Access hash
    ///</summary>
    public long AccessHash { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/file_reference">file reference</a>
    ///</summary>
    public byte[] FileReference { get; set; }

    ///<summary>
    /// Date of upload
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// Available sizes for download
    ///</summary>
    public TVector<MyTelegram.Schema.IPhotoSize> Sizes { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/files#animated-profile-pictures">For animated profiles</a>, the MPEG4 videos
    ///</summary>
    public TVector<MyTelegram.Schema.IVideoSize>? VideoSizes { get; set; }

    ///<summary>
    /// DC ID to use for download
    ///</summary>
    public int DcId { get; set; }

    public void ComputeFlag()
    {
        if (HasStickers) { Flags = Flags.SetBit(0); }
        if (VideoSizes?.Count > 0) { Flags = Flags.SetBit(1); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(AccessHash);
        writer.Write(FileReference);
        writer.Write(Date);
        writer.Write(Sizes);
        if (Flags.IsBitSet(1)) { writer.Write(VideoSizes); }
        writer.Write(DcId);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { HasStickers = true; }
        Id = buffer.ReadInt64();
        AccessHash = buffer.ReadInt64();
        FileReference = buffer.ReadBytes();
        Date = buffer.ReadInt32();
        Sizes = buffer.Read<TVector<MyTelegram.Schema.IPhotoSize>>();
        if (Flags.IsBitSet(1)) { VideoSizes = buffer.Read<TVector<MyTelegram.Schema.IVideoSize>>(); }
        DcId = buffer.ReadInt32();
    }
}