<?php
/**
 * Frontend bootstrap — enqueue assets.
 *
 * @package PrivexPasswordProtect
 */

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

/**
 * Class PPMSTR_Public
 */
class PPMSTR_Public {

	/**
	 * Protection engine.
	 *
	 * @var PPMSTR_Protection_Engine
	 */
	private $engine;

	/**
	 * Form renderer.
	 *
	 * @var PPMSTR_Form_Renderer
	 */
	private $renderer;

	/**
	 * Constructor.
	 *
	 * @param PPMSTR_Protection_Engine $engine   Protection engine.
	 * @param PPMSTR_Form_Renderer     $renderer Form renderer.
	 */
	public function __construct( PPMSTR_Protection_Engine $engine, PPMSTR_Form_Renderer $renderer ) {
		$this->engine   = $engine;
		$this->renderer = $renderer;
	}

	/**
	 * Register hooks.
	 */
	public function init() {
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	/**
	 * Enqueue frontend stylesheets and scripts only when needed.
	 */
	public function enqueue_assets() {
		if ( ! $this->engine->is_current_page_protected() ) {
			return;
		}

		wp_enqueue_style(
			'ppmstr-public',
			PPMSTR_PLUGIN_URL . 'assets/css/ppmstr-public.css',
			array(),
			PPMSTR_VERSION
		);

		$settings = get_option( 'ppmstr_settings', array() );
		$template = isset( $settings['active_template'] ) ? sanitize_file_name( $settings['active_template'] ) : 'default';

		// Per-post override.
		if ( is_singular() ) {
			$post_tpl = get_post_meta( get_queried_object_id(), '_ppmstr_template', true );
			if ( $post_tpl ) {
				$template = sanitize_file_name( $post_tpl );
			}
		}

		$tpl_css_path = PPMSTR_PLUGIN_DIR . 'templates/' . $template . '/style.css';
		if ( file_exists( $tpl_css_path ) ) {
			wp_enqueue_style(
				'ppmstr-template-' . $template,
				PPMSTR_PLUGIN_URL . 'templates/' . $template . '/style.css',
				array( 'ppmstr-public' ),
				PPMSTR_VERSION
			);
		}

		wp_enqueue_script(
			'ppmstr-public',
			PPMSTR_PLUGIN_URL . 'assets/js/ppmstr-public.js',
			array(),
			PPMSTR_VERSION,
			true
		);

		wp_localize_script(
			'ppmstr-public',
			'ppmstrPublic',
			array(
				'ajaxUrl' => esc_url( admin_url( 'admin-ajax.php' ) ),
			)
		);

		// Phase 5: Inject customization CSS.
		$this->output_custom_css( $settings );
	}

	/**
	 * Output inline CSS from template customization settings.
	 *
	 * @param array $settings Plugin settings.
	 */
	private function output_custom_css( $settings ) {
		$cust = isset( $settings['template_customization'] ) ? $settings['template_customization'] : array();
		if ( empty( $cust ) ) {
			return;
		}

		$css = '';

		// Background.
		if ( ! empty( $cust['bg_color'] ) ) {
			$bg = esc_attr( $cust['bg_color'] );
			$css .= ".ppmstr-fullpage-body { background: {$bg} !important; }\n";
		}

		// Card.
		if ( ! empty( $cust['card_bg'] ) ) {
			$card_bg = esc_attr( $cust['card_bg'] );
			$css .= ".ppmstr-form-card, .ppmstr-fullpage-card { background: {$card_bg} !important; }\n";
		}

		if ( isset( $cust['card_radius'] ) ) {
			$cr = absint( $cust['card_radius'] );
			$css .= ".ppmstr-form-card, .ppmstr-fullpage-card { border-radius: {$cr}px !important; }\n";
		}

		if ( empty( $cust['card_shadow'] ) ) {
			$css .= ".ppmstr-form-card, .ppmstr-fullpage-card { box-shadow: none !important; }\n";
		}

		// Accent.
		if ( ! empty( $cust['accent_color'] ) ) {
			$accent = esc_attr( $cust['accent_color'] );
			$css .= ".ppmstr-form-submit { background: {$accent} !important; }\n";
			$css .= ".ppmstr-form-icon { color: {$accent} !important; }\n";
			$css .= ".ppmstr-form-input:focus { border-color: {$accent} !important; }\n";
		}

		// Text.
		if ( ! empty( $cust['text_color'] ) ) {
			$tc = esc_attr( $cust['text_color'] );
			$css .= ".ppmstr-form-headline { color: {$tc} !important; }\n";
			$css .= ".ppmstr-form-desc { color: {$tc} !important; opacity:0.7; }\n";
		}

		// Input radius.
		if ( isset( $cust['input_radius'] ) ) {
			$ir = absint( $cust['input_radius'] );
			$css .= ".ppmstr-form-input { border-radius: {$ir}px !important; }\n";
		}

		// Button radius.
		if ( isset( $cust['btn_radius'] ) ) {
			$br = absint( $cust['btn_radius'] );
			$css .= ".ppmstr-form-submit { border-radius: {$br}px !important; }\n";
		}

		// Hide icon.
		if ( empty( $cust['show_icon'] ) ) {
			$css .= ".ppmstr-form-icon { display: none !important; }\n";
		}

		// Hide description.
		if ( empty( $cust['show_desc'] ) ) {
			$css .= ".ppmstr-form-desc { display: none !important; }\n";
		}

		if ( ! empty( $css ) ) {
			wp_add_inline_style( 'ppmstr-public', $css );
		}
	}
}
