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

namespace MyTelegram.Schema;

///<summary>
/// Settings used by telegram servers for sending the confirm code.Example implementations: <a href="https://github.com/DrKLO/Telegram/blob/master/TMessagesProj/src/main/java/org/telegram/ui/LoginActivity.java">telegram for android</a>, <a href="https://github.com/tdlib/td/tree/master/td/telegram/SendCodeHelper.cpp">tdlib</a>.
/// See <a href="https://corefork.telegram.org/constructor/codeSettings" />
///</summary>
[TlObject(0xad253d78)]
public sealed class TCodeSettings : ICodeSettings
{
    public uint ConstructorId => 0xad253d78;
    ///<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 to allow phone verification via <a href="https://corefork.telegram.org/api/auth">phone calls</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool AllowFlashcall { get; set; }

    ///<summary>
    /// Pass true if the phone number is used on the current device. Ignored if allow_flashcall is not set.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool CurrentNumber { get; set; }

    ///<summary>
    /// If a token that will be included in eventually sent SMSs is required: required in newer versions of android, to use the <a href="https://developers.google.com/identity/sms-retriever/overview">android SMS receiver APIs</a>
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool AllowAppHash { get; set; }

    ///<summary>
    /// Whether this device supports receiving the code using the <a href="https://corefork.telegram.org/constructor/auth.codeTypeMissedCall">auth.codeTypeMissedCall</a> method
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool AllowMissedCall { get; set; }

    ///<summary>
    /// Whether Firebase auth is supported
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool AllowFirebase { get; set; }

    ///<summary>
    /// Set this flag if there is a SIM card in the current device, but it is not possible to check whether the specified phone number matches the SIM's phone number.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool UnknownNumber { get; set; }

    ///<summary>
    /// Previously stored future auth tokens, see <a href="https://corefork.telegram.org/api/auth#future-auth-tokens">the documentation for more info »</a>
    ///</summary>
    public TVector<byte[]>? LogoutTokens { get; set; }

    ///<summary>
    /// Used only by official iOS apps for Firebase auth: device token for apple push.
    ///</summary>
    public string? Token { get; set; }

    ///<summary>
    /// Used only by official iOS apps for firebase auth: whether a sandbox-certificate will be used during transmission of the push notification.
    /// See <a href="https://corefork.telegram.org/type/Bool" />
    ///</summary>
    public bool? AppSandbox { get; set; }

    public void ComputeFlag()
    {
        if (AllowFlashcall) { Flags = Flags.SetBit(0); }
        if (CurrentNumber) { Flags = Flags.SetBit(1); }
        if (AllowAppHash) { Flags = Flags.SetBit(4); }
        if (AllowMissedCall) { Flags = Flags.SetBit(5); }
        if (AllowFirebase) { Flags = Flags.SetBit(7); }
        if (UnknownNumber) { Flags = Flags.SetBit(9); }
        if (LogoutTokens?.Count > 0) { Flags = Flags.SetBit(6); }
        if (Token != null) { Flags = Flags.SetBit(8); }
        if (AppSandbox !=null) { Flags = Flags.SetBit(8); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(6)) { writer.Write(LogoutTokens); }
        if (Flags.IsBitSet(8)) { writer.Write(Token); }
        if (Flags.IsBitSet(8)) { writer.Write(AppSandbox.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { AllowFlashcall = true; }
        if (Flags.IsBitSet(1)) { CurrentNumber = true; }
        if (Flags.IsBitSet(4)) { AllowAppHash = true; }
        if (Flags.IsBitSet(5)) { AllowMissedCall = true; }
        if (Flags.IsBitSet(7)) { AllowFirebase = true; }
        if (Flags.IsBitSet(9)) { UnknownNumber = true; }
        if (Flags.IsBitSet(6)) { LogoutTokens = buffer.Read<TVector<byte[]>>(); }
        if (Flags.IsBitSet(8)) { Token = buffer.ReadString(); }
        if (Flags.IsBitSet(8)) { AppSandbox = buffer.Read(); }
    }
}