<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://immowp.fr
 * @since      1.0.0
 *
 * @package    Immowp_Gestion_Immobiliere
 * @subpackage Immowp_Gestion_Immobiliere/public
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Immowp_Gestion_Immobiliere
 * @subpackage Immowp_Gestion_Immobiliere/public
 * @author     ImmoWP <contact@immowp.fr>
 */
class Immowp_Gestion_Immobiliere_Public {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string $plugin_name       The name of the plugin.
	 * @param      string $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version     = $version;
	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {
		wp_register_style( 'swiper-carousel', plugin_dir_url( __FILE__ ) . 'vendor/swiper/swiper-bundle.min.css', array(), '8.4.7', 'all' );
		wp_register_style( 'photoswipe', plugin_dir_url( __FILE__ ) . 'css/photoswipe.css', array(), $this->version, 'all' );
		wp_register_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/immowp-gestion-public.min.css', array(), '1.4.0', 'all' );

		if ( is_singular( 'immowp_gestion_immo' ) || is_post_type_archive( 'immowp_gestion_immo' ) || is_tax( array(
				'immowp_offre',
				'immowp_type',
				'immowp_localisation',
				'immowp_caracteristiques',
				'immowp_agences',
				'immowp_agent',
				'immowp_departement',
				'immowp_region',
			) ) ) {
			wp_enqueue_style( 'swiper-carousel' );
			wp_enqueue_style( 'photoswipe' );
			wp_enqueue_style( $this->plugin_name );
			
			// Dynamic CSS
			$primary_color   = get_option( 'immowp_primary_color', '#050a30' );
			$secondary_color = get_option( 'immowp_secondary_color', '#7ec8e3' );
			$custom_css      = ":root { --immowp-color1: {$primary_color}; --immowp-color2: {$secondary_color}; }";
			wp_add_inline_style( $this->plugin_name, $custom_css );
		}
	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {
		wp_register_script( 'swiper-carousel', plugin_dir_url( __FILE__ ) . 'vendor/swiper/swiper-bundle.min.js', array(), '8.4.7', true );
		wp_register_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/immowp-gestion-public.js', array(
			'swiper-carousel',
		), '9.12', true );

		wp_localize_script(
			$this->plugin_name,
			'immowp_ajax_object',
			array(
				'ajax_url'       => admin_url( 'admin-ajax.php' ),
			)
		);

		if ( is_singular( 'immowp_gestion_immo' ) || is_post_type_archive( 'immowp_gestion_immo' ) || is_tax( array(
				'immowp_offre',
				'immowp_type',
				'immowp_localisation',
				'immowp_caracteristiques',
				'immowp_agences',
				'immowp_agent',
				'immowp_departement',
				'immowp_region',
			) ) ) {
			wp_enqueue_script( $this->plugin_name );
		}

		// Enqueue Single Script
		if ( is_singular( 'immowp_gestion_immo' ) ) {
			wp_enqueue_script( $this->plugin_name . '-single', plugin_dir_url( __FILE__ ) . 'js/immowp-single.js', array( 'swiper-carousel' ), $this->version, true );
			
			wp_localize_script( $this->plugin_name . '-single', 'immowp_single_params', array(
				'photoswipe_lightbox_url' => plugin_dir_url( __FILE__ ) . 'js/photoswipe/photoswipe-lightbox.esm.js',
				'photoswipe_module_url'   => plugin_dir_url( __FILE__ ) . 'js/photoswipe/photoswipe.esm.js',
			) );
		}
	}

	/**
	 * Add type="module" to scripts.
	 *
	 * @param string $tag    The <script> tag for the enqueued script.
	 * @param string $handle The script's registered handle.
	 * @param string $src    The script's source URL.
	 * @return string
	 */
	public function add_type_attribute( $tag, $handle, $src ) {
		if ( $this->plugin_name . '-single' === $handle ) {
			$tag = str_replace( '<script ', '<script type="module" ', $tag );
		}
		return $tag;
	}

}
