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

namespace MyTelegram.Schema;

///<summary>
/// Theme settings
/// See <a href="https://corefork.telegram.org/constructor/themeSettings" />
///</summary>
[TlObject(0xfa58b6d4)]
public sealed class TThemeSettings : IThemeSettings
{
    public uint ConstructorId => 0xfa58b6d4;
    ///<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>
    /// If set, the freeform gradient fill needs to be animated on every sent message.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool MessageColorsAnimated { get; set; }

    ///<summary>
    /// Base theme
    /// See <a href="https://corefork.telegram.org/type/BaseTheme" />
    ///</summary>
    public MyTelegram.Schema.IBaseTheme BaseTheme { get; set; }

    ///<summary>
    /// Accent color, ARGB format
    ///</summary>
    public int AccentColor { get; set; }

    ///<summary>
    /// Accent color of outgoing messages in ARGB format
    ///</summary>
    public int? OutboxAccentColor { get; set; }

    ///<summary>
    /// The fill to be used as a background for outgoing messages, in RGB24 format. <br>If just one or two equal colors are provided, describes a solid fill of a background. <br>If two different colors are provided, describes the top and bottom colors of a 0-degree gradient.<br>If three or four colors are provided, describes a freeform gradient fill of a background.
    ///</summary>
    public TVector<int>? MessageColors { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/wallpapers">Wallpaper</a>
    /// See <a href="https://corefork.telegram.org/type/WallPaper" />
    ///</summary>
    public MyTelegram.Schema.IWallPaper? Wallpaper { get; set; }

    public void ComputeFlag()
    {
        if (MessageColorsAnimated) { Flags = Flags.SetBit(2); }
        if (/*OutboxAccentColor != 0 && */OutboxAccentColor.HasValue) { Flags = Flags.SetBit(3); }
        if (MessageColors?.Count > 0) { Flags = Flags.SetBit(0); }
        if (Wallpaper != null) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(BaseTheme);
        writer.Write(AccentColor);
        if (Flags.IsBitSet(3)) { writer.Write(OutboxAccentColor.Value); }
        if (Flags.IsBitSet(0)) { writer.Write(MessageColors); }
        if (Flags.IsBitSet(1)) { writer.Write(Wallpaper); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { MessageColorsAnimated = true; }
        BaseTheme = buffer.Read<MyTelegram.Schema.IBaseTheme>();
        AccentColor = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { OutboxAccentColor = buffer.ReadInt32(); }
        if (Flags.IsBitSet(0)) { MessageColors = buffer.Read<TVector<int>>(); }
        if (Flags.IsBitSet(1)) { Wallpaper = buffer.Read<MyTelegram.Schema.IWallPaper>(); }
    }
}