<?php
/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */

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

/**PHPCS -> code compliance: we put useful comments where needed.. */
class HappyFenceNGPrivacy extends HappyFenceNGService {
	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function init() {
		new HappyFenceNGPolicyController();
		new HappyFenceNGCookieController();
		new HappyFenceNGTermsController();
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'wp_footer', array( $this, 'render_cookie_notice' ) );
		HappyFenceNGCStore::registerFunction( HappyFenceNGRgs::RFS_PVRE, HappyFenceNGRgs::SR );
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $method_name ....
	 */
	public static function getStatus( string $method_name ) {
		switch ( $method_name ) {
			case 'render_cookie_notice':
				return HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_privacy_management, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT );
			case 'enqueue_assets':
				return HappyFenceNGPlaceholder::STATUS_ON;
			default:
				return HappyFenceNGPlaceholder::UNKNOWN;
		}
	}


	/**PHPCS -> code compliance: we put useful comments where needed.. */
	private function cookieAccepted(): bool {
		return isset( $_COOKIE['happyfenceng_privacy_consent'] ) && HappyFenceNGPROC::equals( sanitize_text_field( wp_unslash( $_COOKIE['happyfenceng_privacy_consent'] ) ), '1' );
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function enqueue_assets() {

		wp_enqueue_style(
			'happyfenceng-privacy-style',
			HAPPYFENCENG_PLUGIN_URL . 'assets/happyfenceng-privacy.css',
			array(),
			'2.0.1'
		);

		wp_enqueue_script(
			'happyfenceng-privacy-script',
			HAPPYFENCENG_PLUGIN_URL . 'assets/happyfenceng-privacy.js',
			array( 'jquery' ),
			'2.0.1',
			true
		);
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function render_policy_trigger() {
		?>
		
		<?php if ( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_policy_banner ) : ?>
				<a href="#" id="happyfenceng-privacy-policy-trigger" style="color: <?php echo esc_html( HappyFenceNGWhoAmI::get_instance()->PRIVACY->text_color ); ?>" ><?php esc_html_e( 'Privacy Policy', 'happyfenceng-total-enterprise-security' ); ?></a>
		<?php else : ?>
				<a href="<?php echo esc_url( HappyFenceNGWhoAmI::get_instance()->PRIVACY->privacy_url ?? '' ); ?>" target="_blank"><?php esc_html_e( 'Privacy Policy', 'happyfenceng-total-enterprise-security' ); ?></a>                                            
		<?php endif; ?>

		<?php
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function render_policy_modal() {
		?>
		<div id="happyfenceng-privacy-policy-modal" class="happyfenceng-privacy-policy-modal" style="display:none;">
				<div class="happyfenceng-privacy-policy-modal-content">
					<span id="happyprivacy-privacy-policy-modal-close">&times;</span>
					<div id="happyprivacy-privacy-policy-modal-content-body">
						<p><?php esc_html_e( 'Loading policy...', 'happyfenceng-total-enterprise-security' ); ?></p>
					</div>                                            
				</div>
			</div>
		<?php
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function render_cookie_trigger() {
		?>
		<?php if ( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_cookie_banner ) : ?>
						<a href="#" id="happyfenceng-privacy-cookie-trigger" style="color: <?php echo esc_html( HappyFenceNGWhoAmI::get_instance()->PRIVACY->text_color ); ?>" ><?php esc_html_e( 'Cookie Policy', 'happyfenceng-total-enterprise-security' ); ?></a>
					<?php else : ?>
						<a href="<?php echo esc_url( HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PRIVACY->cookie_url ?? '' ) ); ?>" target="_blank"><?php esc_html_e( 'Cookie Policy', 'happyfenceng-total-enterprise-security' ); ?></a>
					<?php endif; ?>
		<?php
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function render_cookie_modal() {
		?>
			<div id="happyfenceng-privacy-cookie-modal" class="happyfenceng-privacy-cookie-modal" style="display:none;">
				<div class="happyfenceng-privacy-cookie-modal-content">
					<span id="happyprivacy-privacy-cookie-modal-close">&times;</span>
					<div id="happyprivacy-privacy-cookie-modal-content-body">
						<p><?php esc_html_e( 'Loading cookie...', 'happyfenceng-total-enterprise-security' ); ?></p>
					</div>  
				</div>  
			</div>
		<?php
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function render_terms_trigger() {
		?>
		<?php if ( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_terms_banner ) : ?>
						<a href="#" id="happyfenceng-privacy-terms-trigger" style="color: <?php echo esc_html( HappyFenceNGWhoAmI::get_instance()->PRIVACY->text_color ); ?>" ><?php esc_html_e( 'Terms & Conditions', 'happyfenceng-total-enterprise-security' ); ?></a>
					<?php else : ?>
						<a href="<?php echo esc_url( HappyFenceNGWhoAmI::get_instance()->PRIVACY->terms_url ?? '' ); ?>" target="_blank"><?php esc_html_e( 'Terms & Conditions', 'happyfenceng-total-enterprise-security' ); ?></a>                    
					<?php endif; ?>
		<?php
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function render_terms_modal() {
		?>
		<div id="happyfenceng-privacy-terms-modal" class="happyfenceng-privacy-terms-modal" style="display:none;">
				<div class="happyfenceng-privacy-terms-modal-content">
					<span id="happyprivacy-privacy-terms-modal-close">&times;</span>
					<div id="happyprivacy-privacy-terms-modal-content-body">
						<p><?php esc_html_e( 'Loading terms...', 'happyfenceng-total-enterprise-security' ); ?></p>
					</div>                                         
				</div>
			</div>
		<?php
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function render_cookie_settings() {
		?>
		<div id="happyfenceng-privacy-modal" class="happyfenceng-privacy-modal" style="display:none;">
				<div class="happyfenceng-privacy-modal-content">
					<h2><?php esc_html_e( 'Cookie Preferences', 'happyfenceng-total-enterprise-security' ); ?></h2>
					<form id="happyfenceng-privacy-form">
						<label><input type="checkbox" name="necessary" disabled checked> <?php esc_html_e( 'Necessary cookies (always active)', 'happyfenceng-total-enterprise-security' ); ?></label><br>
						<label><input type="checkbox" name="analytics"> <?php esc_html_e( 'Analytics cookies', 'happyfenceng-total-enterprise-security' ); ?></label><br>
						<label><input type="checkbox" name="marketing"> <?php esc_html_e( 'Marketing cookies', 'happyfenceng-total-enterprise-security' ); ?></label><br><br>
						<button type="button" class="happyfenceng-privacy-btn save"><?php esc_html_e( 'Save Preferences', 'happyfenceng-total-enterprise-security' ); ?></button>
						<button type="button" class="happyfenceng-privacy-btn cancel"><?php esc_html_e( 'Cancel', 'happyfenceng-total-enterprise-security' ); ?></button>
					</form>
				</div>
			</div> 
		<?php
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function render_cookie_notice() {
		if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
			return;
		}
		$this->enqueue_assets();
		if ( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_privacy_management ) {
			$text      = HappyFenceNGPlaceholder::EMPTY_STRING;
			$color     = esc_attr( HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PRIVACY->color, HappyFenceNGPrivacyProps::_COLOR ) );
			$text_colo = esc_attr( HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PRIVACY->text_color, HappyFenceNGPrivacyProps::_TEXT_COLOR ) );
			?>
			<div id="happyfenceng-privacy-banner" style="background-color: <?php echo esc_attr( $color ); ?>; color: <?php echo esc_attr( $text_colo ); ?>;">
				<span>
				<?php
				if ( HappyFenceNGPROC::equals( HappyFenceNGWhoAmI::get_instance()->PRIVACY->top_banner_text, null ) || HappyFenceNGPROC::equals( HappyFenceNGWhoAmI::get_instance()->PRIVACY->top_banner_text, HappyFenceNGPlaceholder::EMPTY_STRING ) ) {
					echo esc_html__( 'We use cookies to improve your experience. By continuing, you accept our privacy and cookie policies.', 'happyfenceng-total-enterprise-security' );
				} else {
					echo esc_html( HappyFenceNGWhoAmI::get_instance()->PRIVACY->top_banner_text );
				}
				?>
				</span>
				<div class="happyfenceng-privacy-buttons">
					<button class="happyfenceng-privacy-btn necessary"><?php esc_html_e( 'Only necessary cookies', 'happyfenceng-total-enterprise-security' ); ?></button>
					<button class="happyfenceng-privacy-btn accept"><?php esc_html_e( 'Accept all cookies', 'happyfenceng-total-enterprise-security' ); ?></button>
					<button class="happyfenceng-privacy-btn settings"><?php esc_html_e( 'Customize', 'happyfenceng-total-enterprise-security' ); ?></button>    
					<?php

					$this->render_policy_trigger();
					$this->render_cookie_trigger();
					$this->render_terms_trigger();
					?>
									</div>
			</div>

			<?php
						$this->render_policy_modal();
						$this->render_cookie_modal();
						$this->render_terms_modal();
						$this->render_cookie_settings();

			if ( $this->cookieAccepted() && HappyFenceNGWhoAmI::get_instance()->PRIVACY->show_manage_link ) :
				?>
					<div id="happyfenceng-privacy-manage-link" style="text-align: center; margin-top: 10px;">
						<button class="happyfenceng-privacy-btn settings"><?php esc_html_e( 'Manage cookie preferences', 'happyfenceng-total-enterprise-security' ); ?></button>                        
					</div>                                
				<?php endif; ?>
				<?php
		}
	}
}
