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

namespace MyTelegram.Schema;

///<summary>
/// Represents a <a href="https://corefork.telegram.org/api/wallpapers">wallpaper</a> based on an image.
/// See <a href="https://corefork.telegram.org/constructor/wallPaper" />
///</summary>
[TlObject(0xa437c3ed)]
public sealed class TWallPaper : IWallPaper
{
    public uint ConstructorId => 0xa437c3ed;
    ///<summary>
    /// Identifier
    ///</summary>
    public long Id { get; set; }

    ///<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 we created this wallpaper
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Creator { get; set; }

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

    ///<summary>
    /// Whether this is a <a href="https://corefork.telegram.org/api/wallpapers#pattern-wallpapers">pattern wallpaper »</a>
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Pattern { get; set; }

    ///<summary>
    /// Whether this wallpaper should be used in dark mode.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Dark { get; set; }

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

    ///<summary>
    /// Unique wallpaper ID, used when generating <a href="https://corefork.telegram.org/api/links#wallpaper-links">wallpaper links</a> or <a href="https://corefork.telegram.org/api/wallpapers">importing wallpaper links</a>.
    ///</summary>
    public string Slug { get; set; }

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

    ///<summary>
    /// Info on how to generate the wallpaper, according to <a href="https://corefork.telegram.org/api/wallpapers">these instructions »</a>.
    /// See <a href="https://corefork.telegram.org/type/WallPaperSettings" />
    ///</summary>
    public MyTelegram.Schema.IWallPaperSettings? Settings { get; set; }

    public void ComputeFlag()
    {
        if (Creator) { Flags = Flags.SetBit(0); }
        if (Default) { Flags = Flags.SetBit(1); }
        if (Pattern) { Flags = Flags.SetBit(3); }
        if (Dark) { Flags = Flags.SetBit(4); }
        if (Settings != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Id);
        writer.Write(Flags);
        writer.Write(AccessHash);
        writer.Write(Slug);
        writer.Write(Document);
        if (Flags.IsBitSet(2)) { writer.Write(Settings); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Id = buffer.ReadInt64();
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Creator = true; }
        if (Flags.IsBitSet(1)) { Default = true; }
        if (Flags.IsBitSet(3)) { Pattern = true; }
        if (Flags.IsBitSet(4)) { Dark = true; }
        AccessHash = buffer.ReadInt64();
        Slug = buffer.ReadString();
        Document = buffer.Read<MyTelegram.Schema.IDocument>();
        if (Flags.IsBitSet(2)) { Settings = buffer.Read<MyTelegram.Schema.IWallPaperSettings>(); }
    }
}