<?php
/**
 * Public controller.
 *
 * @package CartNinjaRecovery
 */

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

class CARTNINJA_Public {

	public function render_checkout_consent() {
		if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || ( function_exists( 'is_order_received_page' ) && is_order_received_page() ) ) {
			return;
		}
		?>
		<p class="form-row form-row-wide cartninja-checkout-consent">
			<label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
				<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="cartninja_checkout_consent" id="cartninja_checkout_consent" value="1" />
				<span><?php esc_html_e( 'I agree to receive a cart recovery reminder if I do not complete this order.', 'cartninja-recovery-for-woocommerce' ); ?></span>
			</label>
			<span class="description"><?php esc_html_e( 'Your checkout contact details are saved for cart recovery only after you select this option.', 'cartninja-recovery-for-woocommerce' ); ?></span>
		</p>
		<?php
	}

	public function enqueue_assets() {
		if ( ! $this->should_enqueue_assets() ) {
			return;
		}

		$css_path    = CARTNINJA_PLUGIN_DIR . 'public/css/public.css';
		$script_path = CARTNINJA_PLUGIN_DIR . 'public/js/public.js';
		$css_version = file_exists( $css_path ) ? CARTNINJA_VERSION . '-' . filemtime( $css_path ) : CARTNINJA_VERSION;
		$script_version = file_exists( $script_path ) ? CARTNINJA_VERSION . '-' . filemtime( $script_path ) : CARTNINJA_VERSION;

		wp_enqueue_style( 'cartninja-public', CARTNINJA_PLUGIN_URL . 'public/css/public.css', array(), $css_version );
		wp_enqueue_script( 'cartninja-public', CARTNINJA_PLUGIN_URL . 'public/js/public.js', array( 'jquery' ), $script_version, true );
		wp_localize_script(
			'cartninja-public',
			'cartninjaPublic',
			array(
				'ajaxUrl' => admin_url( 'admin-ajax.php' ),
				'nonce'   => wp_create_nonce( 'cartninja_public_nonce' ),
			)
		);
	}

	private function should_enqueue_assets(): bool {
		$is_checkout = function_exists( 'is_checkout' ) && is_checkout() && ( ! function_exists( 'is_order_received_page' ) || ! is_order_received_page() );

		if ( $is_checkout ) {
			return true;
		}

		if ( ! function_exists( 'cartninja_is_premium_unlocked' ) || ! cartninja_is_premium_unlocked() ) {
			return false;
		}

		$settings = get_option( 'cartninja_settings', array() );
		$popup    = isset( $settings['popup'] ) && is_array( $settings['popup'] ) ? $settings['popup'] : array();

		if ( 'yes' !== ( isset( $popup['enable_popup'] ) ? $popup['enable_popup'] : 'no' ) && 'yes' !== ( isset( $popup['enable_add_to_cart_popup'] ) ? $popup['enable_add_to_cart_popup'] : 'no' ) ) {
			return false;
		}

		return ( function_exists( 'is_product' ) && is_product() )
			|| ( function_exists( 'is_shop' ) && is_shop() )
			|| ( function_exists( 'is_product_category' ) && is_product_category() )
			|| ( function_exists( 'is_product_tag' ) && is_product_tag() )
			|| ( function_exists( 'is_cart' ) && is_cart() );
	}
}
