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

namespace MyTelegram.Schema.Account;

///<summary>
/// Modify autosave settings
/// <para>Possible errors</para>
/// Code Type Description
/// 400 PEER_ID_INVALID The provided peer id is invalid.
/// See <a href="https://corefork.telegram.org/method/account.saveAutoSaveSettings" />
///</summary>
[TlObject(0xd69b8361)]
public sealed class RequestSaveAutoSaveSettings : IRequest<IBool>
{
    public uint ConstructorId => 0xd69b8361;
    ///<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 new settings should affect all private chats
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Users { get; set; }

    ///<summary>
    /// Whether the new settings should affect all groups
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Chats { get; set; }

    ///<summary>
    /// Whether the new settings should affect all <a href="https://corefork.telegram.org/api/channel">channels</a>
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Broadcasts { get; set; }

    ///<summary>
    /// Whether the new settings should affect a specific peer
    /// See <a href="https://corefork.telegram.org/type/InputPeer" />
    ///</summary>
    public MyTelegram.Schema.IInputPeer? Peer { get; set; }

    ///<summary>
    /// The new autosave settings
    /// See <a href="https://corefork.telegram.org/type/AutoSaveSettings" />
    ///</summary>
    public MyTelegram.Schema.IAutoSaveSettings Settings { get; set; }

    public void ComputeFlag()
    {
        if (Users) { Flags = Flags.SetBit(0); }
        if (Chats) { Flags = Flags.SetBit(1); }
        if (Broadcasts) { Flags = Flags.SetBit(2); }
        if (Peer != null) { Flags = Flags.SetBit(3); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(3)) { writer.Write(Peer); }
        writer.Write(Settings);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Users = true; }
        if (Flags.IsBitSet(1)) { Chats = true; }
        if (Flags.IsBitSet(2)) { Broadcasts = true; }
        if (Flags.IsBitSet(3)) { Peer = buffer.Read<MyTelegram.Schema.IInputPeer>(); }
        Settings = buffer.Read<MyTelegram.Schema.IAutoSaveSettings>();
    }
}
