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

namespace MyTelegram.Schema;

///<summary>
/// Theme
/// See <a href="https://corefork.telegram.org/constructor/theme" />
///</summary>
[TlObject(0xa00e67d6)]
public sealed class TTheme : ITheme
{
    public uint ConstructorId => 0xa00e67d6;
    ///<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 current user is the creator of this theme
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Creator { get; set; }

    ///<summary>
    /// Whether this is the default theme
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Default { get; set; }

    ///<summary>
    /// Whether this theme is meant to be used as a <a href="https://telegram.org/blog/chat-themes-interactive-emoji-read-receipts">chat theme</a>
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ForChat { get; set; }

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

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

    ///<summary>
    /// Unique theme ID
    ///</summary>
    public string Slug { get; set; }

    ///<summary>
    /// Theme name
    ///</summary>
    public string Title { get; set; }

    ///<summary>
    /// Theme
    /// See <a href="https://corefork.telegram.org/type/Document" />
    ///</summary>
    public MyTelegram.Schema.IDocument? Document { get; set; }

    ///<summary>
    /// Theme settings
    ///</summary>
    public TVector<MyTelegram.Schema.IThemeSettings>? Settings { get; set; }

    ///<summary>
    /// Theme emoji
    ///</summary>
    public string? Emoticon { get; set; }

    ///<summary>
    /// Installation count
    ///</summary>
    public int? InstallsCount { get; set; }

    public void ComputeFlag()
    {
        if (Creator) { Flags = Flags.SetBit(0); }
        if (Default) { Flags = Flags.SetBit(1); }
        if (ForChat) { Flags = Flags.SetBit(5); }
        if (Document != null) { Flags = Flags.SetBit(2); }
        if (Settings?.Count > 0) { Flags = Flags.SetBit(3); }
        if (Emoticon != null) { Flags = Flags.SetBit(6); }
        if (/*InstallsCount != 0 && */InstallsCount.HasValue) { Flags = Flags.SetBit(4); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(AccessHash);
        writer.Write(Slug);
        writer.Write(Title);
        if (Flags.IsBitSet(2)) { writer.Write(Document); }
        if (Flags.IsBitSet(3)) { writer.Write(Settings); }
        if (Flags.IsBitSet(6)) { writer.Write(Emoticon); }
        if (Flags.IsBitSet(4)) { writer.Write(InstallsCount.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Creator = true; }
        if (Flags.IsBitSet(1)) { Default = true; }
        if (Flags.IsBitSet(5)) { ForChat = true; }
        Id = buffer.ReadInt64();
        AccessHash = buffer.ReadInt64();
        Slug = buffer.ReadString();
        Title = buffer.ReadString();
        if (Flags.IsBitSet(2)) { Document = buffer.Read<MyTelegram.Schema.IDocument>(); }
        if (Flags.IsBitSet(3)) { Settings = buffer.Read<TVector<MyTelegram.Schema.IThemeSettings>>(); }
        if (Flags.IsBitSet(6)) { Emoticon = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { InstallsCount = buffer.ReadInt32(); }
    }
}