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

namespace MyTelegram.Schema.Auth.LayerN;

///<summary>
/// Contains info about a sent verification code.
/// See <a href="https://corefork.telegram.org/constructor/auth.sentCode" />
///</summary>
[TlObject(0x5e002502)]
public sealed class TSentCode : ISentCode
{
    public uint ConstructorId => 0x5e002502;
    ///<summary>
    /// Flags, see <a href="https://corefork.telegram.org/mtproto/TL-combinators#conditional-fields">TL conditional fields</a>
    ///</summary>
    public int Flags { get; set; }
    public bool PhoneRegistered { get; set; }

    ///<summary>
    /// Phone code type
    /// See <a href="https://corefork.telegram.org/type/auth.SentCodeType" />
    ///</summary>
    public MyTelegram.Schema.Auth.ISentCodeType Type { get; set; }

    ///<summary>
    /// Phone code hash, to be stored and later re-used with <a href="https://corefork.telegram.org/method/auth.signIn">auth.signIn</a>
    ///</summary>
    public string PhoneCodeHash { get; set; }

    ///<summary>
    /// Phone code type that will be sent next, if the phone code is not received within <code>timeout</code> seconds: to send it use <a href="https://corefork.telegram.org/method/auth.resendCode">auth.resendCode</a>
    /// See <a href="https://corefork.telegram.org/type/auth.CodeType" />
    ///</summary>
    public MyTelegram.Schema.Auth.ICodeType? NextType { get; set; }

    ///<summary>
    /// Timeout for reception of the phone code
    ///</summary>
    public int? Timeout { get; set; }

    public void ComputeFlag()
    {
        if (PhoneRegistered) { Flags = Flags.SetBit(0); }
        if (NextType != null) { Flags = Flags.SetBit(1); }
        if (/*Timeout != 0 && */Timeout.HasValue) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Type);
        writer.Write(PhoneCodeHash);
        if (Flags.IsBitSet(1)) { writer.Write(NextType); }
        if (Flags.IsBitSet(2)) { writer.Write(Timeout.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { PhoneRegistered = true; }
        Type = buffer.Read<MyTelegram.Schema.Auth.ISentCodeType>();
        PhoneCodeHash = buffer.ReadString();
        if (Flags.IsBitSet(1)) { NextType = buffer.Read<MyTelegram.Schema.Auth.ICodeType>(); }
        if (Flags.IsBitSet(2)) { Timeout = buffer.ReadInt32(); }
    }
}