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

namespace MyTelegram.Schema.Auth;

///<summary>
/// The code was sent via the <a href="https://corefork.telegram.org/api/auth#email-verification">previously configured login email »</a>
/// See <a href="https://corefork.telegram.org/constructor/auth.sentCodeTypeEmailCode" />
///</summary>
[TlObject(0xf450f59b)]
public sealed class TSentCodeTypeEmailCode : ISentCodeType
{
    public uint ConstructorId => 0xf450f59b;
    ///<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 authorization through Apple ID is allowed
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool AppleSigninAllowed { get; set; }

    ///<summary>
    /// Whether authorization through Google ID is allowed
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool GoogleSigninAllowed { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/pattern">Pattern</a> of the email
    ///</summary>
    public string EmailPattern { get; set; }

    ///<summary>
    /// Length of the sent verification code
    ///</summary>
    public int Length { get; set; }

    ///<summary>
    /// Clients should wait for the specified amount of seconds before allowing the user to invoke <a href="https://corefork.telegram.org/method/auth.resetLoginEmail">auth.resetLoginEmail</a> (will be 0 for <a href="https://corefork.telegram.org/api/premium">Premium</a> users).
    ///</summary>
    public int? ResetAvailablePeriod { get; set; }

    ///<summary>
    /// An email reset was already requested, and will occur at the specified date.
    ///</summary>
    public int? ResetPendingDate { get; set; }

    public void ComputeFlag()
    {
        if (AppleSigninAllowed) { Flags = Flags.SetBit(0); }
        if (GoogleSigninAllowed) { Flags = Flags.SetBit(1); }
        if (/*ResetAvailablePeriod != 0 && */ResetAvailablePeriod.HasValue) { Flags = Flags.SetBit(3); }
        if (/*ResetPendingDate != 0 && */ResetPendingDate.HasValue) { Flags = Flags.SetBit(4); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(EmailPattern);
        writer.Write(Length);
        if (Flags.IsBitSet(3)) { writer.Write(ResetAvailablePeriod.Value); }
        if (Flags.IsBitSet(4)) { writer.Write(ResetPendingDate.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { AppleSigninAllowed = true; }
        if (Flags.IsBitSet(1)) { GoogleSigninAllowed = true; }
        EmailPattern = buffer.ReadString();
        Length = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { ResetAvailablePeriod = buffer.ReadInt32(); }
        if (Flags.IsBitSet(4)) { ResetPendingDate = buffer.ReadInt32(); }
    }
}