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

namespace MyTelegram.Schema.Auth;

///<summary>
/// Contains user authorization info.
/// See <a href="https://corefork.telegram.org/constructor/auth.authorization" />
///</summary>
[TlObject(0x2ea2c0d4)]
public sealed class TAuthorization : IAuthorization
{
    public uint ConstructorId => 0x2ea2c0d4;
    ///<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>
    /// Suggests the user to set up a 2-step verification password to be able to log in again
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool SetupPasswordRequired { get; set; }

    ///<summary>
    /// Iff setup_password_required is set and the user declines to set a 2-step verification password, they will be able to log into their account via SMS again only after this many days pass.
    ///</summary>
    public int? OtherwiseReloginDays { get; set; }

    ///<summary>
    /// Temporary <a href="https://corefork.telegram.org/passport">passport</a> sessions
    ///</summary>
    public int? TmpSessions { get; set; }

    ///<summary>
    /// A <a href="https://corefork.telegram.org/api/auth#future-auth-tokens">future auth token</a>
    ///</summary>
    public ReadOnlyMemory<byte>? FutureAuthToken { get; set; }

    ///<summary>
    /// Info on authorized user
    /// See <a href="https://corefork.telegram.org/type/User" />
    ///</summary>
    public MyTelegram.Schema.IUser User { get; set; }

    public void ComputeFlag()
    {
        if (SetupPasswordRequired) { Flags = Flags.SetBit(1); }
        if (/*OtherwiseReloginDays != 0 && */OtherwiseReloginDays.HasValue) { Flags = Flags.SetBit(1); }
        if (/*TmpSessions != 0 && */TmpSessions.HasValue) { Flags = Flags.SetBit(0); }
        if (FutureAuthToken != null) { Flags = Flags.SetBit(2); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(1)) { writer.Write(OtherwiseReloginDays.Value); }
        if (Flags.IsBitSet(0)) { writer.Write(TmpSessions.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(FutureAuthToken); }
        writer.Write(User);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { SetupPasswordRequired = true; }
        if (Flags.IsBitSet(1)) { OtherwiseReloginDays = buffer.ReadInt32(); }
        if (Flags.IsBitSet(0)) { TmpSessions = buffer.ReadInt32(); }
        if (Flags.IsBitSet(2)) { FutureAuthToken = buffer.ReadBytes(); }
        User = buffer.Read<MyTelegram.Schema.IUser>();
    }
}