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

namespace MyTelegram.Schema.Account;

///<summary>
/// Change settings related to a session.
/// <para>Possible errors</para>
/// Code Type Description
/// 400 HASH_INVALID The provided hash is invalid.
/// See <a href="https://corefork.telegram.org/method/account.changeAuthorizationSettings" />
///</summary>
[TlObject(0x40f48462)]
public sealed class RequestChangeAuthorizationSettings : IRequest<IBool>
{
    public uint ConstructorId => 0x40f48462;
    ///<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, <a href="https://corefork.telegram.org/api/auth#confirming-login">confirms a newly logged in session »</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Confirmed { get; set; }

    ///<summary>
    /// Session ID from the <a href="https://corefork.telegram.org/constructor/authorization">authorization</a> constructor, fetchable using <a href="https://corefork.telegram.org/method/account.getAuthorizations">account.getAuthorizations</a>
    ///</summary>
    public long Hash { get; set; }

    ///<summary>
    /// Whether to enable or disable receiving encrypted chats: if the flag is not set, the previous setting is not changed
    /// See <a href="https://corefork.telegram.org/type/Bool" />
    ///</summary>
    public bool? EncryptedRequestsDisabled { get; set; }

    ///<summary>
    /// Whether to enable or disable receiving calls: if the flag is not set, the previous setting is not changed
    /// See <a href="https://corefork.telegram.org/type/Bool" />
    ///</summary>
    public bool? CallRequestsDisabled { get; set; }

    public void ComputeFlag()
    {
        if (Confirmed) { Flags = Flags.SetBit(3); }
        if (EncryptedRequestsDisabled !=null) { Flags = Flags.SetBit(0); }
        if (CallRequestsDisabled !=null) { Flags = Flags.SetBit(1); }
    }

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

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { Confirmed = true; }
        Hash = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { EncryptedRequestsDisabled = buffer.Read(); }
        if (Flags.IsBitSet(1)) { CallRequestsDisabled = buffer.Read(); }
    }
}
