<?php
/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Color_Picker_Button
 *
 * This class represents a color picker input field with a button style in the Pixelio Cart Abandon Recovery More plugin.
 * It extends Pixelio_Cart_Abandon_Recovery_More_All_Input to handle input saving, validation, dependency management, and display.
 *
 * It is typically used in plugin settings pages to allow users to select colors with optional dependencies on other inputs.
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Views\Reusable
 */

namespace PixelioCartAbandonRecoveryMore\Admin\Views\Reusable;

use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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

/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Color_Picker_Button
 *
 * This class represents a color picker input field with a button style in the Pixelio Cart Abandon Recovery More plugin.
 * It extends Pixelio_Cart_Abandon_Recovery_More_All_Input to handle input saving, validation, dependency management, and display.
 *
 * It is typically used in plugin settings pages to allow users to select colors with optional dependencies on other inputs.
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Views\Reusable
 */
class Pixelio_Cart_Abandon_Recovery_More_Color_Picker_Button extends Pixelio_Cart_Abandon_Recovery_More_All_Input {

	private $pixelio_util;
	/**
	 * Pixelio_Cart_Abandon_Recovery_More_Color_Picker_Button constructor.
	 *
	 * Initializes the color picker input:
	 * - Sets input type
	 * - Validates and saves POSTed value if available
	 * - Checks dependencies to enable/disable input
	 *
	 * @param string $label           Label displayed for the input field.
	 * @param string $description     Description text displayed under the input.
	 * @param string $placeholder     Placeholder text for the input.
	 * @param string $name            Name of the input (used for POST data and option storage).
	 * @param string $dependency_name Name of the input this field depends on (can be empty).
	 * @param string $type            Input type key (mapped from $this->type_array).
	 * @param bool   $pro             Whether this field is a Pro feature (optional).
	 * @param bool   $api_key         Optional flag to retrieve value from API key storage.
	 */
	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();
		$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 ] ) );
				$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;
		if ( '' !== $dependency_name && ! $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked( $dependency_name ) ) {
			$this->disabled = 'disabled';
		}

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

	/**
	 * Render the color picker input HTML.
	 *
	 * Includes the input header and footer templates, displays the input field with proper classes,
	 * applies dependency and error handling, and renders error message if applicable.
	 *
	 * @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 );
				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->pixelio_cart_abandon_recovery_more_saved_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
	}
}

