<?php

namespace SabaiApps\Directories\Component\Captcha\Api;

class CloudflareTurnstileApi extends AbstractApi {

	protected static $_jsLoaded;

	protected function _captchaApiInfo() {
		return array(
			'label'            => __( 'Cloudflare Turnstile', 'directories-pro' ),
			'default_settings' => array(
				'sitekey' => null,
				'secret'  => null,
				'size'    => 'normal',
				'theme'   => 'auto',
			),
			'weight'           => 3,
		);
	}

	public function captchaApiSettingsForm( array $settings, array $parents ) {
		return array(
			'sitekey' => array(
				'#title'         => __( 'Site key', 'directories-pro' ),
				'#type'          => 'textfield',
				'#default_value' => $settings['sitekey'],
				'#horizontal'    => true,
			),
			'secret'  => array(
				'#title'         => __( 'Secret key', 'directories-pro' ),
				'#type'          => 'textfield',
				'#default_value' => $settings['secret'],
				'#horizontal'    => true,
			),
			'size'    => array(
				'#type'          => 'select',
				'#title'         => __( 'Display size', 'directories-pro' ),
				'#options'       => array(
					'normal'   => __( 'Normal', 'directories-pro' ),
					'flexible' => __( 'Flexible', 'directories-pro' ),
					'compact'  => __( 'Compact', 'directories-pro' ),
				),
				'#default_value' => $settings['size'],
				'#horizontal'    => true,
			),
			'theme'   => array(
				'#type'          => 'select',
				'#title'         => __( 'Theme', 'directories-pro' ),
				'#options'       => array(
					'light' => __( 'Light', 'directories-pro' ),
					'dark'  => __( 'Dark', 'directories-pro' ),
					'auto'  => __( 'Auto', 'directories-pro' ),
				),
				'#default_value' => $settings['theme'],
				'#horizontal'    => true,
			),
		);
	}

	public function captchaApiForm( array $settings, $name, $trigger = null ) {
		if ( ! self::$_jsLoaded ) {
			$js = sprintf(
				'var drtsCloudflareTurnstileCallback = function() {
    if (typeof(turnstile) === "undefined"
        || typeof(turnstile.render) === "undefined"
    ) return;
    jQuery(".drts-captcha-cloudflare-turnstile-form-field").each(function(i) {
        var $this = jQuery(this), options = {
            sitekey: "%s",
            language: "%s",
            size: $this.data("size"),
            theme: $this.data("theme"),
            callback: function (token) {
                console.log("Challenge Success ${token}");
            },
        };
        $this.data("turnstile-widget-id", turnstile.render("#" + $this.attr("id"), options))
            .find("textarea").attr("name", $this.attr("id"));
    });
};',
				$this->_application->H( $settings['sitekey'] ),
				$this->_application->H( strtr( $this->_application->getPlatform()->getLocale(), '_', '-' ) )
			);

			$this->_application->getPlatform()->addHead(
				'<script type="text/javascript">' . $js . '</script>
<script type="text/javascript" src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=drtsCloudflareTurnstileCallback" defer></script>',
				'captcha_cloudflare_turnstile'
			);
			self::$_jsLoaded = true;
		}

		$id = 'drts-captcha-cloudflare-turnstile-form-field-' . $name;
		return array(
			'#type'   => 'item',
			'#markup' => '<div class="drts-captcha-cloudflare-turnstile-form-field" id="' . $id . '" data-trigger="' . $this->_application->H( $trigger ) . '"></div>',
		);
	}

	public function _preRenderCallback( $form ) {
		if ( $form->hasError() ) {
			$form->settings['#js_ready'][] = 'drtsCloudflareTurnstileCallback();';
		}
	}
}
