<?php
/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Input_Text
 *
 * Handles the rendering and saving of a text input field in the Pixelio Cart Abandon Recovery More plugin.
 *
 * This class extends Pixelio_Cart_Abandon_Recovery_More_All_Input and provides:
 * - Input validation and sanitization.
 * - Nonce verification for security.
 * - Option saving and optional API key encryption.
 * - Dependency-based disabling of the input.
 * - Rendering of the input field with error messages and header/footer includes.
 *
 * @package PixelioCartAbandonRecoveryMore
 */

namespace PixelioCartAbandonRecoveryMore\Admin\Views\Reusable;

use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;
use function Symfony\Component\VarDumper\Dumper\esc;

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

/**
 * Pixelio_Cart_Abandon_Recovery_More_Input_Text class
 *
 * This class handles a reusable text input field in the Pixelio Cart Abandon Recovery More plugin.
 * It extends Pixelio_Cart_Abandon_Recovery_More_All_Input to inherit common input functionality.
 *
 * Features:
 * - Input rendering with header and footer.
 * - Validation and sanitization of submitted data.
 * - Nonce verification for security.
 * - Option saving, with optional API key encryption.
 * - Dependency-based disabling of the input.
 * - Error message display when validation fails.
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Views\Reusable
 * @since 0.1
 */
class Pixelio_Cart_Abandon_Recovery_More_Input_Text extends Pixelio_Cart_Abandon_Recovery_More_All_Input {

	private $pixelio_util;
	/**
	 * Constructor.
	 *
	 * Initializes the text input field, validates input, handles dependencies, and sets saved value.
	 *
	 * @param string $label            Label of the input field.
	 * @param string $description      Description shown below the input.
	 * @param string $placeholder      Placeholder for the input.
	 * @param string $name             Name of the input option.
	 * @param string $dependency_name  Name of dependency input (if any).
	 * @param string $type             Input type key (matched in type_array).
	 * @param bool   $pro              If field is for Pro users only.
	 * @param bool   $api_key          Whether the input contains an API key to encrypt.
	 */
	public function __construct( $label, $description, $placeholder, $name, $dependency_name, $type, $pro = false, $api_key = false ) {

		$this->pixelio_util = new Pixelio_Cart_Abandon_Recovery_More_Util(new Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper());
		$this->type = $this->type_array[ $type ];
		if ( isset( $_POST[ $name ] ) && isset( $_POST[ $name . '_wpnonce' ] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ $name . '_wpnonce' ] ) ), $name ) ) {
			if ( $this->type === $this->type_array[0] && ! preg_match( '/^\d+$/', sanitize_text_field( wp_unslash( $_POST[ $name ] ) ) ) && sanitize_text_field( wp_unslash( $_POST[ $name ] ) ) !== '' ) {
				$this->error = 'wrong_value';
			} else {
				$this->pixelio_cart_abandon_recovery_more_saved_value = sanitize_text_field( wp_unslash( $_POST[ $name ] ) );
				if ( $api_key ) {
					$this->pixelio_cart_abandon_recovery_more_saved_value = $this->pixelio_util->pixelio_cart_abandon_recovery_more_encrypt_api_key( $this->pixelio_cart_abandon_recovery_more_saved_value );
				}
				$this->pixelio_util->pixelio_cart_abandon_recovery_more_add_or_update_option( $name, $this->pixelio_cart_abandon_recovery_more_saved_value );
			}
		} elseif ( isset( $_POST[ $name ] ) ) {
			$this->error = 'nonce';
		}
		$this->name            = $name;
		$this->dependency_name = $dependency_name;
		$this->label           = $label;
		$this->placeholder     = $placeholder;
		$this->description     = $description;
		$this->pixelio_cart_abandon_recovery_more_saved_value = $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_get_input_content($this->name,"");

		if ( '' === $dependency_name && ! $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked( $dependency_name ) ) {
			$this->disabled = 'disabled';
		}

		$this->pro   = $pro;
		$this->value = $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_get_input_content( $name, $api_key );
	}

	/**
	 * Renders the input field with its header, footer, and error message.
	 *
	 * @return void
	 */
	public function pixelio_cart_abandon_recovery_more_get_views(): void {
		?>

		<?php include plugin_dir_path( __FILE__ ) . Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_class_slug . '-input-header.php'; ?>
		<input class="pixelio-cart-abandon-recovery-more-input-text
			<?php
			echo esc_attr( $this->dependency_name ."-input");
			echo ( '' !== $this->error ) ? esc_attr('pixelio-cart-abandon-recovery-more-input-error') : esc_attr('');
			?>
			"
				type="<?php echo esc_attr( $this->type ); ?>"
				placeholder="<?php echo esc_attr( $this->placeholder ); ?>"
				name="<?php echo esc_attr( $this->name ); ?>"
				value="<?php echo esc_attr( $this->value ); ?>" <?php echo esc_attr( $this->disabled ); ?>/>
		<p class="<?php echo ( '' !== $this->error ) ? esc_attr( 'pixelio-cart-abandon-recovery-more-error' ) : esc_attr( 'pixelio-cart-abandon-recovery-more-hide-it' ); ?>"><?php esc_html_e( "An error occurred, be sure, it's correctly filled", 'rivio-ai-cart-recovery-marketing-email-automation' ); ?></p>
		<?php include plugin_dir_path( __FILE__ ) . Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_class_slug . '-input-footer.php'; ?>
		<?php
	}
}
