<?php
/**
 * Elementor Pro — Form widget “Simple CAPTCHA” field (server-rendered, no AJAX inject).
 *
 * @package SimpleCaptcha
 */

namespace SimpleCaptcha;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Registers a native Form field type: users add it in the Form widget like any other field.
 */
class Elementor_Pro_Form_Field extends \ElementorPro\Modules\Forms\Fields\Field_Base {

	private Module $module;

	public function __construct( Module $module ) {
		$this->module = $module;
		parent::__construct();
		add_action( 'elementor/preview/init', [ $this, 'on_preview_init' ] );
	}

	public function on_preview_init(): void {
		add_action( 'wp_footer', [ $this, 'print_builder_preview_script' ] );
	}

	/**
	 * @return string
	 */
	public function get_type(): string {
		return 'wpps_simple_captcha';
	}

	/**
	 * @return string
	 */
	public function get_name(): string {
		return esc_html__( 'Simple CAPTCHA', 'wppowersuite-lite' );
	}

	/**
	 * @return string[]
	 */
	public function get_script_depends(): array {
		return [];
	}

	/**
	 * @return string[]
	 */
	public function get_style_depends(): array {
		return [];
	}

	/**
	 * @param \Elementor\Widget_Base $widget
	 * @return void
	 */
	public function update_controls( $widget ): void {
		// No extra per-field options — global Simple CAPTCHA settings apply.
	}

	/**
	 * @param array<string, mixed>   $item
	 * @param int|string               $item_index
	 * @param \ElementorPro\Modules\Forms\Classes\Form|$form $form
	 * @return void
	 */
	public function render( $item, $item_index, $form ): void {
		unset( $form );
		$this->module->enqueueCaptchaStyles();
		if ( $this->is_builder_panel() ) {
			echo '<div class="wpps-simple-captcha-wrap wpps-simple-captcha" style="padding:10px 12px;border:1px dashed #c3c4c7;border-radius:4px;font-size:13px;">';
			echo esc_html__( 'Simple CAPTCHA — shown on the live site (this is a placeholder).', 'wppowersuite-lite' );
			echo '</div>';
			return;
		}

		$cid  = ( is_array( $item ) && ! empty( $item['custom_id'] ) ) ? (string) $item['custom_id'] : 'i' . (int) $item_index;
		$suf  = '_el_' . preg_replace( '/[^a-zA-Z0-9_\-]/', '', $cid ) . '_' . (int) $item_index;
		$this->module->renderForElementorField( 'elementor_f_' . sanitize_key( $cid ), $suf );
	}

	/**
	 * @param mixed $field
	 * @param \ElementorPro\Modules\Forms\Classes\Form_Record|mixed  $record
	 * @param \ElementorPro\Modules\Forms\Classes\Ajax_Handler|mixed $ajax_handler
	 * @return void
	 */
	public function validation( $field, $record, $ajax_handler ): void {
		unset( $record );
		if ( $this->module->validateSimpleCaptchaRequest() ) {
			return;
		}
		$field_id = is_array( $field ) && isset( $field['id'] ) ? (string) $field['id'] : 'wpps_simple_captcha';
		$msg      = $this->module->getSimpleCaptchaRequestErrorMessage();
		if ( is_object( $ajax_handler ) && method_exists( $ajax_handler, 'add_error' ) ) {
			$ajax_handler->add_error( $field_id, $msg );
		}
	}

	/**
	 * @return void
	 */
	public function print_builder_preview_script(): void {
		static $printed = false;
		if ( $printed ) {
			return;
		}
		$printed = true;
		$type = $this->get_type();
		?>
		<?php ob_start(); ?>
		jQuery( function( $ ) {
			elementor.hooks.addFilter( 'elementor_pro/forms/content_template/field/<?php echo esc_js( $type ); ?>', function( input, item, i ) {
				return (
					'<div class="wpps-simple-captcha-wrap" style="padding:10px 12px;border:1px dashed #c3c4c7;border-radius:4px;font-size:13px;">' +
					<?php echo wp_json_encode( __( 'Simple CAPTCHA (preview — live site shows the real challenge).', 'wppowersuite-lite' ) ); ?> +
					'</div>'
				);
			}, 10, 3 );
		} );
<?php \WP_PowerSuite\Core\InlineAssets::script( 'wpps-ia-modules-simple-captcha-includes-elementor-pro-form-field-php-script-1', (string) ob_get_clean() ); ?>
		<?php
	}

	/**
	 * True when building the form in the Elementor editor (not a normal frontend view).
	 */
	private function is_builder_panel(): bool {
		if ( ! class_exists( '\Elementor\Plugin' ) || ! isset( \Elementor\Plugin::$instance->editor ) ) {
			return false;
		}
		return (bool) \Elementor\Plugin::$instance->editor->is_edit_mode();
	}
}
