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

namespace MyTelegram.Schema;

///<summary>
/// Document
/// See <a href="https://corefork.telegram.org/constructor/document" />
///</summary>
[TlObject(0x8fd4c4d8)]
public sealed class TDocument : ILayeredDocument
{
    public uint ConstructorId => 0x8fd4c4d8;
    ///<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>
    /// Document ID
    ///</summary>
    public long Id { get; set; }

    ///<summary>
    /// Check sum, dependent on document ID
    ///</summary>
    public long AccessHash { get; set; }

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

    ///<summary>
    /// Creation date
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// MIME type
    ///</summary>
    public string MimeType { get; set; }

    ///<summary>
    /// Size
    ///</summary>
    public long Size { get; set; }

    ///<summary>
    /// Thumbnails
    ///</summary>
    public TVector<MyTelegram.Schema.IPhotoSize>? Thumbs { get; set; }

    ///<summary>
    /// Video thumbnails
    ///</summary>
    public TVector<MyTelegram.Schema.IVideoSize>? VideoThumbs { get; set; }

    ///<summary>
    /// DC ID
    ///</summary>
    public int DcId { get; set; }

    ///<summary>
    /// Attributes
    ///</summary>
    public TVector<MyTelegram.Schema.IDocumentAttribute> Attributes { get; set; }

    public void ComputeFlag()
    {
        if (Thumbs?.Count > 0) { Flags = Flags.SetBit(0); }
        if (VideoThumbs?.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(MimeType);
        writer.Write(Size);
        if (Flags.IsBitSet(0)) { writer.Write(Thumbs); }
        if (Flags.IsBitSet(1)) { writer.Write(VideoThumbs); }
        writer.Write(DcId);
        writer.Write(Attributes);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Id = buffer.ReadInt64();
        AccessHash = buffer.ReadInt64();
        FileReference = buffer.ReadBytes();
        Date = buffer.ReadInt32();
        MimeType = buffer.ReadString();
        Size = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { Thumbs = buffer.Read<TVector<MyTelegram.Schema.IPhotoSize>>(); }
        if (Flags.IsBitSet(1)) { VideoThumbs = buffer.Read<TVector<MyTelegram.Schema.IVideoSize>>(); }
        DcId = buffer.ReadInt32();
        Attributes = buffer.Read<TVector<MyTelegram.Schema.IDocumentAttribute>>();
    }
}