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

namespace MyTelegram.Schema;

///<summary>
/// Represents a <a href="https://corefork.telegram.org/api/colors">color palette »</a>.
/// See <a href="https://corefork.telegram.org/constructor/peerColor" />
///</summary>
[TlObject(0xb54b5acf)]
public sealed class TPeerColor : IPeerColor
{
    public uint ConstructorId => 0xb54b5acf;
    ///<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>
    /// <a href="https://corefork.telegram.org/api/colors">Color palette ID, see here »</a> for more info; if not set, the default palette should be used.
    ///</summary>
    public int? Color { get; set; }

    ///<summary>
    /// Optional <a href="https://corefork.telegram.org/api/custom-emoji">custom emoji ID</a> used to generate the pattern.
    ///</summary>
    public long? BackgroundEmojiId { get; set; }

    public void ComputeFlag()
    {
        if (/*Color != 0 && */Color.HasValue) { Flags = Flags.SetBit(0); }
        if (/*BackgroundEmojiId != 0 &&*/ BackgroundEmojiId.HasValue) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(Color.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(BackgroundEmojiId.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Color = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { BackgroundEmojiId = buffer.ReadInt64(); }
    }
}