<?php


namespace OTP\Handler\Forms;

use OTP\Helper\FormSessionVars;
use OTP\Helper\MoOTPDocs;
use OTP\Helper\MoUtility;
use OTP\Helper\SessionUtils;
use OTP\Helper\MoMessages;
use OTP\Objects\FormHandler;
use OTP\Objects\IFormHandler;
use OTP\Objects\VerificationType;
use OTP\Traits\Instance;
use ReflectionException;
use WP_Error;
class ProfileBuilderRegistrationForm extends FormHandler implements IFormHandler
{
    use Instance;
    protected function __construct()
    {
        $this->_isLoginOrSocialForm = FALSE;
        $this->_isAjaxForm = FALSE;
        $this->_formSessionVar = FormSessionVars::PB_DEFAULT_REG;
        $this->_typePhoneTag = "\x6d\x6f\137\x70\142\x5f\160\150\157\156\145\137\x65\x6e\x61\142\154\x65";
        $this->_typeEmailTag = "\x6d\157\x5f\x70\142\137\x65\x6d\141\151\154\x5f\x65\156\141\x62\x6c\145";
        $this->_typeBothTag = "\x6d\x6f\x5f\x70\x62\x5f\x62\157\x74\150\137\145\x6e\x61\x62\154\x65";
        $this->_formKey = "\x50\x42\137\x44\x45\x46\x41\x55\114\x54\x5f\106\117\x52\x4d";
        $this->_formName = mo_("\120\162\157\x66\x69\x6c\x65\x20\x42\x75\151\154\144\145\162\40\x52\x65\147\x69\x73\x74\x72\x61\x74\151\x6f\x6e\40\106\x6f\x72\x6d");
        $this->_isFormEnabled = get_mo_option("\160\142\137\144\x65\x66\141\x75\x6c\164\x5f\x65\156\x61\x62\154\x65");
        $this->_formDocuments = MoOTPDocs::PB_FORM_LINK;
        parent::__construct();
    }
    function handleForm()
    {
        $this->_otpType = get_mo_option("\160\142\137\x65\x6e\141\x62\x6c\145\137\x74\171\160\145");
        $this->_restrictDuplicates = get_mo_option('pb_restrict_duplicates');
        $this->_phoneKey = get_mo_option("\160\x62\x5f\x70\x68\x6f\156\x65\x5f\x6d\x65\164\x61\x5f\x6b\x65\171");
        $this->_phoneFormId = "\x69\156\160\165\164\x5b\156\141\x6d\x65\75" . $this->_phoneKey . "\135";
        add_filter("\167\160\160\x62\x5f\157\x75\x74\x70\165\x74\137\x66\151\x65\154\x64\x5f\145\x72\x72\157\x72\163\x5f\146\x69\x6c\164\x65\x72", array($this, "\146\157\162\155\x62\x75\151\x6c\144\145\x72\137\163\151\164\145\x5f\162\x65\x67\x69\x73\x74\x72\141\x74\151\157\x6e\137\145\x72\162\x6f\162\x73"), 99, 4);
    }
    function isPhoneVerificationEnabled()
    {
        $HK = $this->getVerificationType();
        return $HK === VerificationType::PHONE || $HK === VerificationType::BOTH;
    }
        function formbuilder_site_registration_errors($fieldErrors, $fieldArgs, $globalRequest, $typeArgs)
    {       
        if(!empty($fieldErrors)) return $fieldErrors;
        if($globalRequest['action']=='register')
        {
            $phone_metakey=$this->_phoneKey;
            $phone_number=$globalRequest[$phone_metakey];
            foreach ($fieldArgs as $key => $value) 
            {
                if($value["meta-name"]===$phone_metakey)
                {
                    $id=$value['id'];
                }
            }
            if($this->_restrictDuplicates && $this->isPhoneNumberAlreadyInUse($phone_number,$phone_metakey))
            {
            $fieldErrors[$id] = '<span class="wppb-form-error">'.MoMessages::showMessage(MoMessages::PHONE_EXISTS).' </span>';  
            return $fieldErrors;
            }

            if(SessionUtils::isStatusMatch($this->_formSessionVar,self::VALIDATED,$this->getVerificationType()))
            {
                $this->unsetOTPSessionVariables();
                return $fieldErrors;
            }
            return $this->startOTPVerificationProcess($fieldErrors,$globalRequest);
        }
        return $fieldErrors;
    }

        function isPhoneNumberAlreadyInUse($phone,$key)
    {
        global $wpdb;
        MoUtility::processPhoneNumber($phone);
        $q = "SELECT `user_id` FROM `{$wpdb->prefix}usermeta` WHERE `meta_key` = '$key' AND `meta_value` =  '$phone'";
        $results = $wpdb->get_row($q);
        return !MoUtility::isBlank($results);
    }
    // function formbuilder_site_registration_errors($cW, $xV, $kK, $B6)
    // {
    //     if (empty($cW)) {
    //         goto EN;
    //     }
    //     return $cW;
    //     EN:
    //     if (!($kK["\141\143\164\151\x6f\x6e"] == "\162\145\x67\x69\x73\x74\x65\x72")) {
    //         goto GN;
    //     }
    //     if (!SessionUtils::isStatusMatch($this->_formSessionVar, self::VALIDATED, $this->getVerificationType())) {
    //         goto dE;
    //     }
    //     $this->unsetOTPSessionVariables();
    //     return $cW;
    //     dE:
    //     return $this->startOTPVerificationProcess($cW, $kK);
    //     GN:
    //     return $cW;
    // }
    function startOTPVerificationProcess($cW, $BT)
    {
        MoUtility::initialize_transaction($this->_formSessionVar);
        $bN = $this->extractArgs($BT, $this->_phoneKey);
        $this->sendChallenge($bN["\165\x73\x65\162\156\141\x6d\145"], $bN["\145\x6d\x61\x69\154"], new WP_Error(), $bN["\x70\x68\x6f\x6e\x65"], $this->getVerificationType(), $bN["\x70\x61\163\x73\167\x31"], array());
    }
    private function extractArgs($bN, $qK)
    {
        return array("\x75\163\x65\x72\156\x61\155\x65" => $bN["\165\163\145\162\x6e\x61\x6d\145"], "\145\x6d\x61\x69\154" => $bN["\145\155\x61\151\x6c"], "\x70\141\163\163\167\61" => $bN["\x70\141\x73\163\167\x31"], "\160\x68\x6f\x6e\x65" => MoUtility::sanitizeCheck($qK, $bN));
    }
    function handle_failed_verification($te, $j4, $sP, $Cw)
    {
        miniorange_site_otp_validation_form($te, $j4, $sP, MoUtility::_get_invalid_otp_method(), $this->getVerificationType(), FALSE);
    }
    function handle_post_verification($TG, $te, $j4, $xy, $sP, $yJ, $Cw)
    {
        SessionUtils::addStatus($this->_formSessionVar, self::VALIDATED, $Cw);
    }
    public function unsetOTPSessionVariables()
    {
        SessionUtils::unsetSession(array($this->_txSessionId, $this->_formSessionVar));
    }
    public function getPhoneNumberSelector($fv)
    {
        if (!($this->isFormEnabled() && $this->isPhoneVerificationEnabled())) {
            goto G0;
        }
        array_push($fv, $this->_phoneFormId);
        G0:
        return $fv;
    }
    function handleFormOptions()
    {
        if (MoUtility::areFormOptionsBeingSaved($this->getFormOption())) {
            goto pw;
        }
        return;
        pw:
        $this->_isFormEnabled = $this->sanitizeFormPOST("\160\x62\x5f\144\145\146\141\165\x6c\164\137\x65\156\x61\x62\x6c\x65");
        $this->_otpType = $this->sanitizeFormPOST("\160\142\x5f\145\156\x61\x62\154\x65\137\x74\x79\160\x65");
        $this->_phoneKey = $this->sanitizeFormPOST("\160\x62\137\x70\x68\x6f\156\x65\x5f\x66\151\145\154\144\x5f\153\x65\171");
        $this->_restrictDuplicates = $this->sanitizeFormPOST('pb_restrict_duplicates');
        update_mo_option('pb_restrict_duplicates', $this->_restrictDuplicates);
        update_mo_option("\x70\x62\x5f\144\x65\x66\x61\x75\x6c\164\137\x65\156\x61\142\154\145", $this->_isFormEnabled);
        update_mo_option("\x70\x62\x5f\145\x6e\x61\x62\154\x65\x5f\x74\171\x70\x65", $this->_otpType);
        update_mo_option("\x70\x62\137\160\150\x6f\x6e\145\x5f\155\x65\x74\141\x5f\153\145\171", $this->_phoneKey);
    }
}
