<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */

use \WP2FA\Authenticator\Login as Login;
use \WP2FA\Authenticator\BackupCodes as BackupCodes;

// login page link
$login_page         = conikal_get_assign_page('conikal_page_login_field');

// authentication params
$user_id            = isset($_GET['user_id']) ? $_GET['user_id'] : '';
$origin_provider    = isset($_GET['origin_provider']) ? $_GET['origin_provider'] : '';
$provider           = isset($_GET['provider']) ? $_GET['provider'] : '';
$referer_id         = isset($_GET['referer_id']) ? $_GET['referer_id'] : '';

// get login user data
$user               = get_user_by('ID', $user_id);

if ( ! $provider ) {
    $provider = login::get_available_providers_for_user( $user );
}

$codes_remaining = BackupCodes::codes_remaining_for_user( $user );

if ( !empty($user) ) {
    $login_nonce = login::create_login_nonce( $user->ID );
    if ( ! $login_nonce ) {
        echo esc_html__( 'Failed to create a login nonce.', 'campoal' );
    }
} else {
    echo esc_html__( 'User not exist.', 'campoal' );
    $login_nonce = false;
}

$rememberme = intval( login::rememberme() );

if (empty($referer_id)) {
    $account_page = isset($campoal_options['conikal_page_user_account_field']) ? $campoal_options['conikal_page_user_account_field'] : 0;
    if ($account_page != 0) {
        $redirect_to = get_permalink($account_page);
    } else {
        $redirect_to = home_url('/');
    }
} else {
    $redirect_to = get_permalink($referer_id);
}

?>
<div class="wp2fa-validate-form">
    <?php if ( !empty($user) && !empty($login_nonce) ) : ?>
        <form class="ui large form" name="validate_2fa_form" id="loginform" action="<?php echo esc_url( login::login_url( array( 'action' => 'validate_2fa' ), 'login_post' ) ); ?>" method="post" autocomplete="off">
            <input type="hidden" name="provider"      id="provider"      value="<?php echo esc_attr( $provider ); ?>" />
            <input type="hidden" name="wp-auth-id"    id="wp-auth-id"    value="<?php echo esc_attr( $user->ID ); ?>" />
            <input type="hidden" name="wp-auth-nonce" id="wp-auth-nonce" value="<?php echo esc_attr( $login_nonce['key'] ); ?>" />
            <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
            <input type="hidden" name="rememberme"    id="rememberme"    value="<?php echo esc_attr( $rememberme ); ?>" />

            <?php
            // Check to see what provider is set and give the relevant authentication page.
            if ( 'totp' === $provider ) {
                login::totp_authentication_page( $user );
            } elseif ( 'email' === $provider ) {
                login::email_authentication_page( $user );
            } elseif ( 'backup_codes' === $provider ) {
                login::backup_codes_authentication_page( $user );
            }
            ?>

            <input type="submit" class="ui primary large button"  name="submit-2fa" id="submit-2fa" value="<?php esc_attr_e('Sign In', 'campoal'); ?>">
        </form>
    <?php endif; ?>

    <?php
    if ( isset( $codes_remaining ) && $codes_remaining > 0 ) :
        $backup_code_url = $login_page ? add_query_arg(array('action' => 'two_factor', 'user_id' => $user->ID, 'origin_provider' => $provider, 'provider' => 'backup_codes', 'referer_id' => $referer_id), $login_page->link) : ''; 
        $totp_url = $login_page ? add_query_arg(array('action' => 'two_factor', 'user_id' => $user->ID, 'provider' => $origin_provider, 'referer_id' => $referer_id), $login_page->link) : '';
        ?>
        <div class="ui basic vertical segment backup-methods-wrap">
                <?php if ($provider != 'backup_codes') { ?>
                    <a href="<?php echo esc_url( $backup_code_url ); ?>">
                        <?php esc_html_e( 'Or, use a backup code.', 'campoal' ); ?>
                    </a>
                <?php } else { ?>
                    <a href="<?php echo esc_url( $totp_url ); ?>">
                        <?php esc_html_e( 'Back to authentication.', 'campoal' ); ?>
                    </a>
                <?php } ?>
        </div>
    <?php endif; ?>
</div>