<?php
/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Checkbox
 *
 * Represents a reusable checkbox input with optional dynamic labels, preview and edit functionality.
 * Handles saving related color options, checking dependencies, and displaying the checkbox UI.
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Views\Reusable
 */

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;

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

/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Checkbox
 *
 * Represents a reusable checkbox input with optional dynamic labels, preview and edit functionality.
 * Handles saving related color options, checking dependencies, and displaying the checkbox UI.
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Views\Reusable
 */
class Pixelio_Cart_Abandon_Recovery_More_Checkbox extends Pixelio_Cart_Abandon_Recovery_More_All_Input {

	/**
	 * List of color-related options linked to this checkbox.
	 *
	 * @var string[]
	 */
	private $color_class = array(
		'pixelio-cart-abandon-recovery-more-newsletters-submit-background-color',
		'pixelio-cart-abandon-recovery-more-newsletters-submit-text-color',
		'pixelio-cart-abandon-recovery-more-newsletters-submit-hover-color',
		'pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup-text-color',
		'pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-background-color',
		'pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-popup-text-color',
		'pixelio-cart-abandon-recovery-more-pop-up-yes-thanks-exit-hover-color',
		'pixelio-cart-abandon-recovery-more-pop-up-timer-content-text-color',
		'pixelio-cart-abandon-recovery-more-pop-up-header-title-text-color',
		'pixelio-cart-abandon-recovery-more-pop-up-header-title-action-text-color',
		'pixelio-cart-abandon-recovery-more-pop-up-header-background-color',
	);

	/**
	 * Label for the "other" state (optional dynamic label).
	 *
	 * @var string|mixed
	 */
	private mixed $other_state_label;

	/**
	 * Label when checkbox is unchecked.
	 *
	 * @var string|mixed
	 */
	private mixed $the_unchecked_state_label;

	/**
	 * Label when checkbox is checked.
	 *
	 * @var string|mixed
	 */
	private mixed $the_checked_state_label;

	/**
	 * Input onshow attribute for dynamic interactions.
	 *
	 * @var mixed
	 */
	private mixed $on_show;
	private  $pixelio_util;

	/**
	 * Pixelio_Cart_Abandon_Recovery_More_Checkbox constructor.
	 *
	 * Initializes the checkbox input, handles saving associated colors, manages preview/edit states and dependencies.
	 *
	 * @param string $label Label for the checkbox.
	 * @param string $description Description text.
	 * @param string $name Name used for saving the checkbox value.
	 * @param bool   $pro Whether it is a pro feature (default: false).
	 * @param bool   $preview Whether to show preview (default: false).
	 * @param bool   $edit Whether to show edit options (default: false).
	 * @param string $preview_class Preview class filename (default: '').
	 * @param string $edit_class Edit class filename (default: '').
	 * @param string $dependency Name of dependency input (default: ''). Used to check if the parent dependency has been checked or not in the first load. If yes then show it. If not, hide it.
	 * @param bool   $dynamique Whether labels are dynamic (default: false).
	 * @param string $the_checked_state_label Label for checked state (default: '').
	 * @param string $the_unchecked_state_label Label for unchecked state (default: '').
	 * @param string $onshow Onshow attribute (default: '').
	 */
	public function __construct( $label, $description, $name, $pro = false, $preview = false, $edit = false, $preview_class = '', $edit_class = '', $dependency = '', $dynamique = false, $the_checked_state_label = '', $the_unchecked_state_label = '', $onshow = '' ) {

		$this->pixelio_util = new Pixelio_Cart_Abandon_Recovery_More_Util(new Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper());
		[$this->name, $this->label, $this->pixelio_cart_abandon_recovery_more_saved_value, $this->error] = $this->pixelio_util->pixelio_cart_abandon_recovery_more_check_nonce( $name, $label );

		$this->on_show                   = $onshow;
		$this->the_checked_state_label   = $the_checked_state_label;
		$this->the_unchecked_state_label = $the_unchecked_state_label;
		$this->description               = $description;
		$this->pixelio_cart_abandon_recovery_more_save_color();
		$this->pro             = false;
		$this->preview         = $preview;
		$this->dynamique       = $dynamique;
		$this->edit            = $edit;
		$this->dependency_name = $dependency;
		$this->preview_class   = sanitize_text_field( $preview_class );
		$this->edit_class      = sanitize_text_field( $edit_class );
		$this->pixelio_cart_abandon_recovery_more_saved_value = $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked($this->name);
	}

	/**
	 * Save all associated color options if posted and nonce is verified.
	 *
	 * @return void
	 */
	public function pixelio_cart_abandon_recovery_more_save_color(): void {
		foreach ( $this->color_class as $color ) {
			if ( isset( $_POST ) && isset( $_POST[ $color ] ) && isset( $_POST[ $this->name . '_wpnonce' ] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ $this->name . '_wpnonce' ] ) ), $this->name ) ) {
				$pixelio_cart_abandon_recovery_more_abandon_checkbox_value = sanitize_text_field( wp_unslash( $_POST[ $color ] ) );
				$this->pixelio_util->pixelio_cart_abandon_recovery_more_add_or_update_option( $color, $pixelio_cart_abandon_recovery_more_abandon_checkbox_value );
			} elseif ( isset( $_POST ) && isset( $_POST[ $color ] ) ) {
				$this->error = 'nonce';

			}
		}
	}

	/**
	 * Render the checkbox input with labels, preview/edit containers, and JS for interactions.
	 *
	 * @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';
		?>
		<div class="pixelio-cart-abandon-recovery-more-opt-in-checkbox-container ">
			<label class="pixelio-cart-abandon-recovery-more-checkbox-wrapper">
				<input type="hidden"
						name="<?php echo esc_attr( $this->name ); ?>"
						value="<?php $this->pixelio_util->pixelio_cart_abandon_recovery_more_is_checked_val( $this->name );?>"/>
				<input type="checkbox" name=""
					<?php $this->pixelio_util->pixelio_cart_abandon_recovery_more_is_checked( $this->name ); ?>
						class="pixelio-cart-abandon-recovery-more-opt-in-checkbox pixelio-cart-abandon-recovery-more-opt-in-checkbox-with-name pixelio-cart-abandon-recovery-more-opt-in-checkbox pixelio-cart-abandon-recovery-more-opt-in-checkbox-<?php echo esc_attr( $this->name ); ?>"
						data-onshow="<?php echo esc_attr( $this->on_show ); ?>"/>
				<div
					class="pixelio-cart-abandon-recovery-more-slider <?php echo ( ! $this->pro ) ? esc_attr( 'pixelio-cart-abandon-recovery-more-slider-not-pro' ) : esc_attr( '' ); ?> pixelio-cart-abandon-recovery-more-checkbox-round <?php echo ( '' !== $this->error ) ? esc_attr( 'pixelio-cart-abandon-recovery-more-checkbox-error' ) : esc_attr( '' ); ?>"
					data-clicked_on="<?php echo esc_attr( $this->name ); ?>"></div>

			</label>
			<p class=" pixelio-cart-abandon-recovery-more-checkbox-no pixelio-cart-abandon-recovery-more-checkbox-selection-text
				<?php echo ( ( ! $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked( $this->name ) && ! $this->pro ) || ( !$this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked( $this->name ) && $this->pro ) ) ? esc_attr( '' ) : esc_attr( 'pixelio-cart-abandon-recovery-more-hide-it' ); ?>"
				id="<?php echo esc_attr( $this->name ); ?>-no"
			>
				<?php
				if ( $this->dynamique ) {
					echo esc_html($this->the_unchecked_state_label );
				} else {
					esc_html_e( 'No, it is not activated now', 'rivio-ai-cart-recovery-marketing-email-automation' );
				}
				?>
			</p>
			<p class="pixelio-cart-abandon-recovery-more-checkbox-yes <?php echo ( $this->pro ) ? esc_attr( 'pixelio-cart-abandon-recovery-more-hide-it' ) : esc_attr( '' ); ?> pixelio-cart-abandon-recovery-more-checkbox-selection-text <?php echo $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked( $this->name ) ? esc_attr( '' ) : esc_attr( 'pixelio-cart-abandon-recovery-more-hide-it' ); ?>"
				id="<?php echo esc_attr( $this->name ); ?>-yes"
			>
				<?php
				if ( $this->dynamique ) {
					echo esc_html( $this->the_checked_state_label );
				} else {
					esc_html_e( 'Yes, it is activated now', 'rivio-ai-cart-recovery-marketing-email-automation' );
				}
				?>
				<button
					class=" pixelio-cart-abandon-recovery-more-input-button pixelio-cart-abandon-recovery-more-input-button-setting-action-preview pixelio-cart-abandon-recovery-more-input-button-setting-action <?php echo $this->preview ? esc_attr( '' ) : esc_attr( 'pixelio-cart-abandon-recovery-more-hide-it' ); ?>"
					data-clicked_on="<?php echo esc_attr( $this->name ); ?>"><?php esc_html_e( ' Preview it', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?></button><?php $this->preview && $this->edit ? esc_html_e( 'or', 'rivio-ai-cart-recovery-marketing-email-automation' ) : ''; ?>
				<button
					class="pixelio-cart-abandon-recovery-more-input-button pixelio-cart-abandon-recovery-more-input-button-setting-action-edit pixelio-cart-abandon-recovery-more-input-button-setting-action <?php echo $this->edit ? esc_attr( '' ) : esc_attr( 'pixelio-cart-abandon-recovery-more-hide-it' ); ?>"
					data-clicked_on="<?php echo esc_attr( $this->name ); ?>"><?php esc_html_e( ' edit it', 'rivio-ai-cart-recovery-marketing-email-automation' ); ?></button>

			<div
				class="pixelio-cart-abandon-recovery-more-preview-container  pixelio-cart-abandon-recovery-more-hide-it <?php echo esc_attr( $this->name . '-preview' ); ?> <?php echo esc_attr( $this->name . '-' . get_option( "$this->name-position", 'right' ) ); ?>">
				<div class="pixelio-cart-abandon-recovery-more-disabled-background"></div>
				<?php
				if ( $this->preview && file_exists( plugin_dir_path( __FILE__ ) . '../preview/' . $this->preview_class . '.php' ) ) {
					include plugin_dir_path( __FILE__ ) . '../preview/' . $this->preview_class . '.php';
				}
				?>
			</div>
			<div
				class="pixelio-cart-abandon-recovery-more-edit-container  pixelio-cart-abandon-recovery-more-hide-it <?php echo esc_attr( $this->name . '-edit' ); ?>">
				<div class=" pixelio-cart-abandon-recovery-more-disabled-background"></div>
				<?php
				if ( $this->edit && file_exists( plugin_dir_path( __FILE__ ) . '../preview/' . $this->edit_class . '.php' ) ) {
					include plugin_dir_path( __FILE__ ) . '../preview/' . $this->edit_class . '.php';
				}
				?>
			</div>
			</p>

		</div>
		<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
	}
}

