<?php


namespace OTP\Handler\Forms;

use OTP\Helper\FormSessionVars;
use OTP\Helper\MoConstants;
use OTP\Helper\MoMessages;
use OTP\Helper\MoOTPDocs;
use OTP\Helper\MoUtility;
use OTP\Helper\SessionUtils;
use OTP\Objects\FormHandler;
use OTP\Objects\IFormHandler;
use OTP\Objects\VerificationType;
use OTP\Traits\Instance;
use ReflectionException;
class RealesWPTheme extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = TRUE;
        $this->_formSessionVar = FormSessionVars::REALESWP_REGISTER;
        $this->_typePhoneTag = "\155\157\137\x72\145\141\154\x65\x73\x5f\160\x68\157\156\x65\x5f\145\x6e\x61\142\154\x65";
        $this->_typeEmailTag = "\155\157\137\162\145\141\154\x65\x73\137\145\x6d\141\x69\x6c\137\145\156\141\x62\154\x65";
        $this->_phoneFormId = "\43\160\150\x6f\156\x65\123\x69\147\156\x75\160";
        $this->_formKey = "\x52\105\x41\x4c\x45\x53\x5f\122\x45\107\x49\123\124\x45\x52";
        $this->_formName = mo_("\122\x65\141\154\145\x73\40\127\120\x20\x54\150\x65\x6d\x65\x20\x52\x65\147\151\163\x74\x72\x61\x74\151\x6f\156\40\x46\157\162\155");
        $this->_isFormEnabled = get_mo_option("\162\145\x61\x6c\145\163\137\145\156\x61\x62\154\145");
        $this->_formDocuments = MoOTPDocs::REALES_THEME;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\x72\145\141\154\x65\163\137\x65\156\141\142\x6c\x65\137\x74\x79\x70\145");
        add_action("\167\x70\x5f\145\156\x71\x75\x65\165\x65\x5f\163\143\x72\151\x70\x74\x73", array($this, "\145\x6e\x71\x75\145\x75\x65\137\x73\143\162\151\x70\164\x5f\x6f\156\137\160\x61\x67\145"));
        $this->routeData();
    }
    function routeData()
    {
        if (array_key_exists("\157\160\x74\151\x6f\x6e", $_GET)) {
            goto As1;
        }
        return;
        As1:
        switch (trim($_GET["\x6f\160\x74\x69\157\x6e"])) {
            case "\155\x69\156\151\x6f\x72\141\156\x67\145\x2d\162\145\141\x6c\x65\x73\x77\x70\55\x76\145\162\x69\146\x79":
                $this->_send_otp_realeswp_verify($_POST);
                goto pn;
            case "\155\151\x6e\151\157\x72\141\156\x67\x65\55\166\141\154\151\x64\141\x74\145\x2d\162\145\141\x6c\x65\x73\x77\160\x2d\x6f\164\x70":
                $this->_reales_validate_otp($_POST);
                goto pn;
        }
        Hh:
        pn:
    }
    function enqueue_script_on_page()
    {
        wp_register_script("\162\145\141\154\x65\x73\x77\x70\123\143\x72\151\160\164", MOV_URL . "\151\156\x63\154\x75\x64\x65\x73\57\x6a\x73\57\x72\145\x61\x6c\145\163\x77\x70\x2e\155\x69\x6e\x2e\152\163\x3f\x76\145\162\163\x69\x6f\156\75" . MOV_VERSION, array("\152\161\165\145\x72\x79"));
        wp_localize_script("\x72\x65\141\154\x65\163\x77\160\123\x63\162\151\x70\164", "\x6d\x6f\x76\141\x72\x73", array("\x69\x6d\x67\x55\x52\114" => MOV_URL . "\x69\156\x63\x6c\165\x64\145\x73\x2f\x69\x6d\141\147\145\163\57\x6c\157\141\x64\145\162\56\x67\x69\146", "\x66\x69\x65\x6c\144\156\141\155\x65" => $this->_otpType == $this->_typePhoneTag ? "\x70\x68\157\x6e\145\x20\156\165\x6d\x62\x65\x72" : "\x65\x6d\141\151\154", "\x66\x69\x65\x6c\x64" => $this->_otpType == $this->_typePhoneTag ? "\160\150\x6f\x6e\x65\123\151\147\x6e\165\160" : "\145\155\141\x69\x6c\x53\x69\x67\x6e\165\x70", "\163\x69\164\145\125\122\114" => site_url(), "\151\156\x73\145\x72\164\101\146\x74\x65\162" => $this->_otpType == $this->_typePhoneTag ? "\x23\x70\150\x6f\156\x65\123\151\x67\156\x75\160" : "\x23\145\155\x61\x69\x6c\x53\151\x67\x6e\165\160", "\160\x6c\x61\143\x65\110\157\x6c\144\x65\x72" => mo_("\x4f\124\x50\x20\x43\157\144\145"), "\x62\x75\164\x74\157\x6e\x54\145\x78\164" => mo_("\x56\141\154\x69\x64\x61\164\x65\x20\x61\156\144\40\123\x69\x67\x6e\x20\x55\x70"), "\x61\152\141\x78\165\162\x6c" => wp_ajax_url()));
        wp_enqueue_script("\162\x65\141\x6c\145\x73\167\x70\123\x63\x72\x69\160\x74");
    }
    function _send_otp_realeswp_verify($BT)
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        if (strcasecmp($this->_otpType, $this->_typePhoneTag) == 0) {
            goto t7;
        }
        $this->_send_otp_to_email($BT);
        goto nO;
        t7:
        $this->_send_otp_to_phone($BT);
        nO:
    }
    function _send_otp_to_phone($BT)
    {
        if (array_key_exists("\165\x73\x65\x72\137\160\x68\x6f\156\x65", $BT) && !MoUtility::isBlank($BT["\165\x73\145\162\137\x70\x68\157\x6e\145"])) {
            goto ZQ;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_PHONE), MoConstants::ERROR_JSON_TYPE));
        goto vm;
        ZQ:
        SessionUtils::addPhoneVerified($this->_formSessionVar, trim($BT["\165\x73\145\162\137\x70\x68\157\156\x65"]));
        $this->sendChallenge("\164\x65\163\x74", '', null, trim($BT["\165\163\145\162\x5f\160\150\x6f\x6e\145"]), VerificationType::PHONE);
        vm:
    }
    function _send_otp_to_email($BT)
    {
        if (array_key_exists("\x75\163\145\162\x5f\x65\155\x61\151\x6c", $BT) && !MoUtility::isBlank($BT["\x75\163\145\x72\x5f\145\x6d\141\x69\x6c"])) {
            goto gl;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::ENTER_EMAIL), MoConstants::ERROR_JSON_TYPE));
        goto e8;
        gl:
        SessionUtils::addEmailVerified($this->_formSessionVar, $BT["\x75\163\x65\x72\137\145\x6d\141\x69\x6c"]);
        $this->sendChallenge("\164\x65\x73\164", $BT["\165\163\x65\162\x5f\145\x6d\x61\x69\154"], null, $BT["\165\x73\145\x72\137\145\155\141\x69\154"], VerificationType::EMAIL);
        e8:
    }
    function _reales_validate_otp($BT)
    {
        $as = !isset($BT["\157\x74\160"]) ? sanitize_text_field($BT["\x6f\164\160"]) : '';
        $this->checkIfOTPVerificationHasStarted();
        $this->validateSubmittedFields($BT);
        $this->validateChallenge(NULL, $as);
    }
    function validateSubmittedFields($BT)
    {
        $HK = $this->getVerificationType();
        if ($HK === VerificationType::EMAIL && !SessionUtils::isEmailVerifiedMatch($this->_formSessionVar, $BT["\165\163\145\x72\137\x65\x6d\141\x69\154"])) {
            goto o_;
        }
        if ($HK === VerificationType::PHONE && !SessionUtils::isPhoneVerifiedMatch($this->_formSessionVar, $BT["\x75\163\x65\162\137\x70\x68\x6f\156\145"])) {
            goto Cd;
        }
        goto xr;
        o_:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::EMAIL_MISMATCH), MoConstants::ERROR_JSON_TYPE));
        die;
        goto xr;
        Cd:
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::PHONE_MISMATCH), MoConstants::ERROR_JSON_TYPE));
        die;
        xr:
    }
    function checkIfOTPVerificationHasStarted()
    {
        if (SessionUtils::isOTPInitialized($this->_formSessionVar)) {
            goto A8;
        }
        wp_send_json(MoUtility::createJson(MoMessages::showMessage(MoMessages::PLEASE_VALIDATE), MoConstants::ERROR_JSON_TYPE));
        die;
        A8:
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        wp_send_json(MoUtility::createJson(MoUtility::_get_invalid_otp_method(), MoConstants::ERROR_JSON_TYPE));
        die;
    }
    function handle_post_verification($TG, $te, $j4, $xy, $sP, $yJ, $Cw)
    {
        $this->unsetOTPSessionVariables();
        wp_send_json(MoUtility::createJson(MoMessages::REG_SUCCESS, MoConstants::SUCCESS_JSON_TYPE));
        die;
    }
    public function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->isFormEnabled() && $this->_otpType == $this->_typePhoneTag)) {
            goto nD;
        }
        array_push($fv, $this->_phoneFormId);
        nD:
        return $fv;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto wn;
        }
        return;
        wn:
        $this->_isFormEnabled = $this->sanitizeFormPOST("\162\145\x61\154\145\163\x5f\x65\156\x61\142\x6c\145");
        $this->_otpType = $this->sanitizeFormPOST("\x72\145\141\x6c\145\x73\137\x65\x6e\x61\142\154\x65\x5f\x74\x79\160\x65");
        update_mo_option("\162\x65\x61\x6c\x65\163\x5f\145\x6e\141\x62\154\145", $this->_isFormEnabled);
        update_mo_option("\x72\x65\141\x6c\x65\163\x5f\145\x6e\x61\x62\154\x65\137\164\x79\160\x65", $this->_otpType);
    }
}
