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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Set a custom <a href="https://corefork.telegram.org/api/wallpapers">wallpaper »</a> in a specific private chat with another user.
/// <para>Possible errors</para>
/// Code Type Description
/// 400 PEER_ID_INVALID The provided peer id is invalid.
/// 400 WALLPAPER_INVALID The specified wallpaper is invalid.
/// 400 WALLPAPER_NOT_FOUND The specified wallpaper could not be found.
/// See <a href="https://corefork.telegram.org/method/messages.setChatWallPaper" />
///</summary>
[TlObject(0x8ffacae1)]
public sealed class RequestSetChatWallPaper : IRequest<MyTelegram.Schema.IUpdates>
{
    public uint ConstructorId => 0x8ffacae1;
    ///<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>
    /// Only for <a href="https://corefork.telegram.org/api/premium">Premium</a> users, sets the specified wallpaper for both users of the chat, without requiring confirmation from the other user.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ForBoth { get; set; }

    ///<summary>
    /// If we don't like the new wallpaper the other user of the chat has chosen for us using the <code>for_both</code> flag, we can re-set our previous wallpaper just on our side using this flag.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Revert { get; set; }

    ///<summary>
    /// The private chat where the wallpaper will be set
    /// See <a href="https://corefork.telegram.org/type/InputPeer" />
    ///</summary>
    public MyTelegram.Schema.IInputPeer Peer { get; set; }

    ///<summary>
    /// The <a href="https://corefork.telegram.org/api/wallpapers">wallpaper »</a>, obtained as described in the <a href="https://corefork.telegram.org/api/wallpapers#uploading-wallpapers">wallpaper documentation »</a>; must <strong>not</strong> be provided when installing a wallpaper obtained from a <a href="https://corefork.telegram.org/constructor/messageActionSetChatWallPaper">messageActionSetChatWallPaper</a> service message (<code>id</code> must be provided, instead).
    /// See <a href="https://corefork.telegram.org/type/InputWallPaper" />
    ///</summary>
    public MyTelegram.Schema.IInputWallPaper? Wallpaper { get; set; }

    ///<summary>
    /// Wallpaper settings, obtained as described in the <a href="https://corefork.telegram.org/api/wallpapers#uploading-wallpapers">wallpaper documentation »</a> or from <a href="https://corefork.telegram.org/constructor/messageActionSetChatWallPaper">messageActionSetChatWallPaper</a>.<code>wallpaper</code>.<code>settings</code>.
    /// See <a href="https://corefork.telegram.org/type/WallPaperSettings" />
    ///</summary>
    public MyTelegram.Schema.IWallPaperSettings? Settings { get; set; }

    ///<summary>
    /// If the wallpaper was obtained from a <a href="https://corefork.telegram.org/constructor/messageActionSetChatWallPaper">messageActionSetChatWallPaper</a> service message, must contain the ID of that message.
    ///</summary>
    public int? Id { get; set; }

    public void ComputeFlag()
    {
        if (ForBoth) { Flags = Flags.SetBit(3); }
        if (Revert) { Flags = Flags.SetBit(4); }
        if (Wallpaper != null) { Flags = Flags.SetBit(0); }
        if (Settings != null) { Flags = Flags.SetBit(2); }
        if (/*Id != 0 && */Id.HasValue) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Peer);
        if (Flags.IsBitSet(0)) { writer.Write(Wallpaper); }
        if (Flags.IsBitSet(2)) { writer.Write(Settings); }
        if (Flags.IsBitSet(1)) { writer.Write(Id.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { ForBoth = true; }
        if (Flags.IsBitSet(4)) { Revert = true; }
        Peer = buffer.Read<MyTelegram.Schema.IInputPeer>();
        if (Flags.IsBitSet(0)) { Wallpaper = buffer.Read<MyTelegram.Schema.IInputWallPaper>(); }
        if (Flags.IsBitSet(2)) { Settings = buffer.Read<MyTelegram.Schema.IWallPaperSettings>(); }
        if (Flags.IsBitSet(1)) { Id = buffer.ReadInt32(); }
    }
}
