<?php
/**
 * Class: Open_Streetmap_Widget
 * Name: OpenStreetMap
 * Slug: eac-addon-open-streetmap
 *
 * Icon by: https://templatic.com/newsblog/100-free-templatic-map-icons/
 *
 * Description: Affiche une Map et ses marqueurs avec le projet OpenStreetMap alternatif à GoogleMap
 * Projet collaboratif de cartographie en ligne qui vise à constituer une base de données géographiques libre du monde
 *
 * @since 1.8.8
 */

namespace EACCustomWidgets\Includes\Widgets;

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

use EACCustomWidgets\EAC_Plugin;
use EACCustomWidgets\Core\Eac_Load_Config;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Repeater;
use Elementor\Group_Control_Css_Filter;

class Open_Streetmap_Widget extends Widget_Base {

	/**
	 * $config_layers
	 *
	 * URL du fichier de configuration des tuiles (tiles)
	 */
	private $config_layers = EAC_PLUGIN_URL . 'includes/config/osm/osmTiles.json';

	/**
	 * $layer_default
	 *
	 * La tuile par défaut
	 */
	private $layer_default = 'osm_basic';


	/**
	 * $base_layers
	 *
	 * Liste des tuiles (tiles) extraitent du fichier de configuration 'json'
	 */
	private $base_layers = array();

	/**
	 * $base_layers_default
	 *
	 * Liste des tuiles (tiles) par défaut
	 */
	private $base_layers_default = array(
		'osm_basic'     => 'OSM Basic',
		'osm_fr'        => 'OSM France',
		'osm_de'        => 'OSM Deutschland',
		'osm_bw'        => 'OSM B&W',
		'stamenToner'   => 'Toner',
		'stamenColor'   => 'Watercolor',
		'stamenLite'    => 'Toner Lite',
		'stamenTerrain' => 'Terrain',
		'topoMap'       => 'Topo Map',
	);

	/**
	 * $config_icons
	 *
	 * URL du fichier de configuration des icones
	 */
	private $config_icons = EAC_PLUGIN_URL . 'includes/config/osm/osmIcons.json';

	/**
	 * $base_icons
	 *
	 * Liste des icones extraitent du fichier de configuraion 'json'
	 */
	private $base_icons = array();

	/**
	 * $base_icons_sizes
	 *
	 * Les dimensions des icones extraitent du fichier de configuraion 'json'
	 */
	private $base_icons_sizes = array();

	/**
	 * $sizes_icons_default
	 *
	 * Les dimensions par défaut des icones
	 */
	private $sizes_icons_default = '33,44';

	/**
	 * $base_icons_default
	 *
	 * Liste des icones par défaut pour les marqueurs (markers)
	 */
	private $base_icons_default = array(
		'default.png'           => 'Default',
		'automotive.png'        => 'Automotive',
		'bars.png'              => 'Bars',
		'books-media.png'       => 'Books & Media',
		'clothings.png'         => 'Clothings',
		'commercial-places.png' => 'Commercial places',
		'doctors.png'           => 'Doctors',
		'exhibitions.png'       => 'Exhibitions',
		'fashion.png'           => 'Fashion',
		'food.png'              => 'Food',
		'government.png'        => 'Government',
		'health-medical.png'    => 'Health Medical',
		'hotels.png'            => 'Hotels',
		'industries.png'        => 'Industries',
		'libraries.png'         => 'Libraries',
		'magazines.png'         => 'Magazines',
		'movies.png'            => 'Movies',
		'museums.png'           => 'Museums',
		'nightlife.png'         => 'Nightlife',
		'parks.png'             => 'Parks',
		'places.png'            => 'Places',
		'real-estate.png'       => 'Real Estate',
		'restaurants.png'       => 'Restaurants',
		'schools.png'           => 'Schools',
		'sports.png'            => 'Sports',
		'swimming-pools.png'    => 'Swimming-pools',
		'transport.png'         => 'Transport',
		'travel.png'            => 'Travel',
	);

	/**
	 * $title_default
	 *
	 * La propriété 'title' par défaut
	 */
	private $title_default = 'No Title';

	/**
	 * Constructeur de la class Open_Streetmap_Widget
	 *
	 * Enregistre les scripts et les styles
	 */
	public function __construct( array $data = array(), ?array $args = null ) {
		parent::__construct( $data, $args );

		// Valorise la liste des tuiles (tiles)
		$this->setTilesConfig();

		// Valorise la liste des icones
		$this->setIconsConfig();

		/** Ajout du moteur de recherche Nominatim */
		add_action( 'elementor/editor/after_enqueue_scripts', array( $this, 'enqueue_editor_scripts' ) );
	}

	/**
	 * Le nom de la clé du composant dans le fichier de configuration
	 *
	 * @var $slug
	 *
	 * @access private
	 */
	private $slug = 'open-streetmap';

	/**
	 * Retrieve widget name.
	 *
	 * @access public
	 *
	 * @return string widget name.
	 */
	public function get_name(): string {
		return Eac_Load_Config::get_widget_name( $this->slug );
	}

	/**
	 * Retrieve widget title.
	 *
	 * @access public
	 *
	 * @return string widget title.
	 */
	public function get_title(): string {
		return Eac_Load_Config::get_widget_title( $this->slug );
	}

	/**
	 * Retrieve widget icon.
	 *
	 * @access public
	 *
	 * @return string widget icon.
	 */
	public function get_icon(): string {
		return Eac_Load_Config::get_widget_icon( $this->slug );
	}

	/**
	 * Affecte le composant à la catégorie définie dans plugin.php
	 *
	 * @access public
	 *
	 * @return array widget category.
	 */
	public function get_categories(): array {
		return Eac_Load_Config::get_widget_categories( $this->slug );
	}

	/**
	 * Load dependent libraries
	 *
	 * @access public
	 *
	 * @return array libraries list.
	 */
	public function get_script_depends(): array {
		return array( 'eac-leaflet' );
	}

	/**
	 * Load dependent styles
	 * Les styles sont chargés dans le footer
	 *
	 * @access public
	 *
	 * @return array CSS list.
	 */
	public function get_style_depends(): array {
		return array( 'eac-leaflet' );
	}

	/**
	 * Get widget keywords.
	 *
	 * Retrieve the list of keywords the widget belongs to.
	 *
	 * @access public
	 *
	 * @return array Widget keywords.
	 */
	public function get_keywords(): array {
		return Eac_Load_Config::get_widget_keywords( $this->slug );
	}

	/**
	 * Get help widget get_custom_help_url.
	 *
	 * @access public
	 *
	 * @return string URL help center
	 */
	public function get_custom_help_url(): string {
		return Eac_Load_Config::get_widget_help_url( $this->slug );
	}

	/**
	 * Ajout du moteur de recherche nominatim pour Openstreetmap
	 *
	 * @access public
	 */
	public function enqueue_editor_scripts(): void {
		wp_enqueue_script( 'eac-nominatim', EAC_Plugin::instance()->get_script_url( 'assets/js/osm/eac-osm-search' ), array( 'jquery' ), EAC_PLUGIN_VERSION, true );
	}

	/**
	 * has_widget_inner_wrapper
	 *
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return false;
	}

	/**
	 * is_dynamic_content
	 *
	 * @return bool
	 */
	protected function is_dynamic_content(): bool {
		return true;
	}

	/**
	 * Register widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @access protected
	 */
	protected function register_controls(): void {

		$this->start_controls_section(
			'osm_settings_map',
			array(
				'label' => esc_html__( 'Map', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

			/**
			$this->add_control('osm_settings_client_geolocate',
				[
					'label' => esc_html__("Localiser le visiteur (Géolocaliser)", 'eac-components'),
					'type' => Controls_Manager::SWITCHER,
					'description' => esc_html__('La gélocalisation doit être activée', 'eac-components'),
					'label_on' => esc_html__('oui', 'eac-components'),
					'label_off' => esc_html__('non', 'eac-components'),
					'return_value' => 'yes',
					'default' => '',
					'conditions' => [
						'terms' => [
							['name' => 'osm_settings_client_ip', 'operator' => '!==', 'value' => 'yes'],
							['name' => 'osm_settings_search', 'operator' => '!==', 'value' => 'yes'],
						],
					],
				]
			);
			*/

			$this->add_control(
				'osm_settings_client_ip',
				array(
					'label'        => esc_html__( 'Locate the visitor (IP address)', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'description'  => esc_html__( 'Localization by IP address does not work on a local server.', 'eac-components' ),
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => '',
					'conditions'   => array(
						'terms' => array(
							array(
								'name'     => 'osm_settings_search',
								'operator' => '!==',
								'value'    => 'yes',
							),
						),
					),
				)
			);

			$this->add_control(
				'osm_settings_client_warning',
				array(
					'type'            => Controls_Manager::RAW_HTML,
					'content_classes' => 'elementor-panel-alert elementor-panel-alert-warning',
					'raw'             => esc_html__( 'Geolocation to the correct city may be less reliable for IP addresses distributed by mobile operators.', 'eac-components' ),
					'condition'       => array( 'osm_settings_client_ip' => 'yes' ),
				)
			);

			$this->add_control(
				'osm_settings_search',
				array(
					'label'        => esc_html__( 'Search an address', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
					'conditions'   => array(
						'terms' => array(
							array(
								'name'     => 'osm_settings_client_ip',
								'operator' => '!==',
								'value'    => 'yes',
							),
						),
					),
				)
			);

			$this->add_control(
				'osm_settings_search_addresse', // elementor-control-osm_settings_search_addresse
				array(
					'label'       => esc_html__( 'Central marker address', 'eac-components' ),
					'type'        => Controls_Manager::RAW_HTML,
					'raw'         => sprintf( '<form onsubmit="getNominatimRepeaterAddress(this);" action="javascript:void(0);"><input type="text" placeholder="Paris, France" id="eac-get-nominatim-address" class="eac-get-nominatim-address" style="margin-block-start:10px; margin-block-end:10px;"><input type="submit" value="%s" class="elementor-button elementor-button-success" style="padding:8px 0;" onclick="getNominatimRepeaterAddress(this)"></form>', esc_html__( 'Search', 'eac-components' ) ),
					'label_block' => true,
					'condition'   => array( 'osm_settings_search' => 'yes' ),
				)
			);

			$this->add_control(
				'osm_settings_center_lat', // elementor-control-osm_settings_center_lat
				array(
					'label'       => esc_html__( 'Latitude', 'eac-components' ),
					'type'        => Controls_Manager::TEXT,
					'dynamic'     => array( 'active' => true ),
					'label_block' => true,
					'condition'   => array( 'osm_settings_search' => 'yes' ),
				)
			);

			$this->add_control(
				'osm_settings_center_lng', // elementor-control-osm_settings_center_lng
				array(
					'label'       => esc_html__( 'Longitude', 'eac-components' ),
					'type'        => Controls_Manager::TEXT,
					'dynamic'     => array( 'active' => true ),
					'label_block' => true,
					'condition'   => array( 'osm_settings_search' => 'yes' ),
				)
			);

			$this->add_control(
				'osm_settings_center_help',
				array(
					'label'     => '',
					'type'      => Controls_Manager::RAW_HTML,
					'raw'       => sprintf( '<span style="font-size:10px;"><a href="https://www.coordonnees-gps.fr/" target="_blank" rel="nofollow noopener noreferrer" >%s</a></span>', esc_html__( 'Click here for location coordinates', 'eac-components' ) ),
					'condition' => array( 'osm_settings_search' => 'yes' ),
				)
			);

			$this->add_control(
				'osm_settings_center_title', // elementor-control-osm_settings_center_title
				array(
					'label'       => esc_html__( 'Tooltip title', 'eac-components' ),
					'type'        => Controls_Manager::TEXT,
					'default'     => 'Paris, France',
					'dynamic'     => array( 'active' => true ),
					'ai'          => array( 'active' => false ),
					'label_block' => true,
					'separator'   => 'before',
				)
			);

			$this->add_control(
				'osm_settings_center_content',
				array(
					'label'       => esc_html__( 'Tooltip content', 'eac-components' ),
					'type'        => Controls_Manager::TEXTAREA,
					'dynamic'     => array( 'active' => true ),
					'ai'          => array( 'active' => false ),
					'label_block' => true,
				)
			);

		$this->end_controls_section();

		$this->start_controls_section(
			'osm_markers',
			array(
				'label' => esc_html__( 'Markers', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

			$this->add_control(
				'osm_is_json_enable',
				array(
					'label'        => 'JSON URL',
					'type'         => Controls_Manager::HIDDEN,
					'default'      => \EACCustomWidgets\Core\Utils\Eac_Tools_Util::is_json_enable() ? 'true' : 'false',
					'save_default' => false,
				)
			);

			$repeater = new Repeater();

			$repeater->start_controls_tabs( 'osm_markers_tabs' );

				$repeater->start_controls_tab(
					'osm_markers_tab_position',
					array(
						'label' => '<i class="eicon-map-pin eac-osm-tabs" aria-hidden="true"></i>',
					)
				);

					$repeater->add_control(
						'osm_markers_search_help',
						array(
							'label' => '',
							'type'  => Controls_Manager::RAW_HTML,
							'raw'   => esc_html__( 'Enter address then button Search', 'eac-components' ),
						)
					);

					$repeater->add_control(
						'osm_markers_search_addresse', // elementor-control-osm_markers_search_addresse
						array(
							'label'       => esc_html__( 'Address', 'eac-components' ),
							'type'        => Controls_Manager::RAW_HTML,
							'raw'         => sprintf( '<form onsubmit="getNominatimRepeaterAddress(this);" action="javascript:void(0);"><input type="text" id="eac-get-nominatim-address" class="eac-get-nominatim-address" style="margin-block-start:10px; margin-block-end:10px;"><input type="submit" value="%s" class="elementor-button elementor-button-success" style="padding:8px 0;" onclick="getNominatimRepeaterAddress(this)"></form>', esc_html__( 'Search', 'eac-components' ) ),
							'label_block' => true,
						)
					);

					$repeater->add_control(
						'osm_markers_tooltip_lat', // elementor-control-osm_markers_tooltip_lat
						array(
							'label'       => esc_html__( 'Latitude', 'eac-components' ),
							'type'        => Controls_Manager::TEXT,
							'dynamic'     => array( 'active' => true ),
							'label_block' => true,
						)
					);

					$repeater->add_control(
						'osm_markers_tooltip_lng', // elementor-control-osm_markers_tooltip_lng
						array(
							'label'       => esc_html__( 'Longitude', 'eac-components' ),
							'type'        => Controls_Manager::TEXT,
							'dynamic'     => array( 'active' => true ),
							'label_block' => true,
						)
					);

					$repeater->add_control(
						'osm_markers_tooltip_help',
						array(
							'label' => '',
							'type'  => Controls_Manager::RAW_HTML,
							'raw'   => sprintf( '<span style="font-size:10px;"><a href="https://www.coordonnees-gps.fr/" target="_blank" rel="nofollow noopener noreferrer" >%s</a></span>', esc_html__( 'Click here for location coordinates', 'eac-components' ) ),
						)
					);

				$repeater->end_controls_tab();

				$repeater->start_controls_tab(
					'osm_markers_tab_content',
					array(
						'label' => '<i class="eicon-notes eac-osm-tabs" aria-hidden="true"></i>',
					)
				);

					$repeater->add_control(
						'osm_markers_tooltip_title', // elementor-control-osm_markers_tooltip_title
						array(
							'label'       => esc_html__( 'Tooltip title', 'eac-components' ),
							'type'        => Controls_Manager::TEXT,
							'dynamic'     => array( 'active' => true ),
							'ai'          => array( 'active' => false ),
							'label_block' => true,
						)
					);

					$repeater->add_control(
						'osm_markers_tooltip_content',
						array(
							'label'       => esc_html__( 'Tooltip content', 'eac-components' ),
							'type'        => Controls_Manager::TEXTAREA,
							'dynamic'     => array( 'active' => true ),
							'ai'          => array( 'active' => false ),
							'label_block' => true,
						)
					);

					$repeater->add_control(
						'osm_markers_tooltip_marker',
						array(
							'label'       => esc_html__( 'Select icon', 'eac-components' ),
							'type'        => Controls_Manager::SELECT,
							'options'     => $this->base_icons,
							'default'     => 'default.png',
							'label_block' => true,
						)
					);

				$repeater->end_controls_tab();

			$repeater->end_controls_tabs();

			$this->add_control(
				'osm_markers_import_list',
				array(
					'label'        => esc_html__( 'Import marker', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => '',
				)
			);

			$this->add_control(
				'osm_markers_import_type',
				array(
					'label'     => esc_html__( 'Link type', 'eac-components' ),
					'type'      => Controls_Manager::SELECT,
					'default'   => 'none',
					'options'   => array(
						'none' => esc_html__( 'None', 'eac-components' ),
						'url'  => esc_html( 'URL' ),
						'file' => esc_html__( 'Local', 'eac-components' ),
					),
					'condition' => array( 'osm_markers_import_list' => 'yes' ),
				)
			);

			$this->add_control(
				'osm_markers_import_url',
				array(
					'label'       => esc_html( 'URL' ),
					'description' => esc_html__( "Paste the absolute path of the 'geoJSON' file", 'eac-components' ),
					'type'        => Controls_Manager::URL,
					'placeholder' => 'http://your-link.com/file-geojson.json',
					'dynamic'     => array(
						'active' => true,
					),
					'condition'   => array(
						'osm_markers_import_list' => 'yes',
						'osm_markers_import_type' => 'url',
						'osm_is_json_enable'      => 'true',
					),
				)
			);

			$this->add_control(
				'osm_markers_import_url_info',
				array(
					'type'            => Controls_Manager::RAW_HTML,
					'content_classes' => 'elementor-panel-alert elementor-panel-alert-warning',
					'raw'             => esc_html__( "Elementor/Advanced settings, activate option 'Enable Unfiltered File Uploads'", 'eac-components' ),
					'condition'       => array(
						'osm_markers_import_list' => 'yes',
						'osm_markers_import_type' => 'url',
						'osm_is_json_enable'      => 'false',
					),
				)
			);

			$this->add_control(
				'osm_markers_import_file',
				array(
					'label'       => esc_html__( 'Select file', 'eac-components' ),
					'description' => esc_html__( "'geoJSON' data format with 'json' as file extension.", 'eac-components' ),
					'type'        => Controls_Manager::SELECT,
					'default'     => 'none',
					'options'     => \EACCustomWidgets\Core\Utils\Eac_Tools_Util::get_directory_file_list( 'includes/config/osm/markers', 'json', 'application/json' ),
					'label_block' => true,
					'condition'   => array(
						'osm_markers_import_list' => 'yes',
						'osm_markers_import_type' => 'file',
					),
				)
			);

			$this->add_control(
				'osm_markers_import_keywords',
				array(
					'label'       => esc_html__( 'Keywords', 'eac-components' ),
					'description' => esc_html__( "List of 'property|label' separated by the character '|' with one pair per line.", 'eac-components' ),
					'placeholder' => 'property1|label1' . chr( 13 ) . 'property2|label2' . chr( 13 ) . 'property3|label3',
					'type'        => Controls_Manager::TEXTAREA,
					'dynamic'     => array( 'active' => true ),
					'ai'          => array( 'active' => false ),
					'label_block' => true,
					'conditions' => array(
						'relation' => 'or',
						'terms'    => array(
							array(
								'terms' => array(
									array(
										'name'     => 'osm_markers_import_list',
										'operator' => '===',
										'value'    => 'yes',
									),
									array(
										'name'     => 'osm_markers_import_type',
										'operator' => '===',
										'value'    => 'file',
									),
								),
							),
							array(
								'terms' => array(
									array(
										'name'     => 'osm_markers_import_list',
										'operator' => '===',
										'value'    => 'yes',
									),
									array(
										'name'     => 'osm_markers_import_type',
										'operator' => '===',
										'value'    => 'url',
									),
									array(
										'name'     => 'osm_is_json_enable',
										'operator' => '===',
										'value'    => 'true',
									),
								),
							),
						),
					),
				)
			);

			$this->add_control(
				'osm_markers_import_marker',
				array(
					'label'       => esc_html__( 'Select icon', 'eac-components' ),
					'type'        => Controls_Manager::SELECT,
					'options'     => $this->base_icons,
					'default'     => 'default.png',
					'label_block' => true,
					'conditions' => array(
						'relation' => 'or',
						'terms'    => array(
							array(
								'terms' => array(
									array(
										'name'     => 'osm_markers_import_list',
										'operator' => '===',
										'value'    => 'yes',
									),
									array(
										'name'     => 'osm_markers_import_type',
										'operator' => '===',
										'value'    => 'file',
									),
								),
							),
							array(
								'terms' => array(
									array(
										'name'     => 'osm_markers_import_list',
										'operator' => '===',
										'value'    => 'yes',
									),
									array(
										'name'     => 'osm_markers_import_type',
										'operator' => '===',
										'value'    => 'url',
									),
									array(
										'name'     => 'osm_is_json_enable',
										'operator' => '===',
										'value'    => 'true',
									),
								),
							),
						),
					),
				)
			);

			$this->add_control(
				'osm_markers_list',
				array(
					'label'       => esc_html__( 'Marker list', 'eac-components' ),
					'type'        => Controls_Manager::REPEATER,
					'fields'      => $repeater->get_controls(),
					'title_field' => '{{{ osm_markers_tooltip_title }}}',
					'button_text' => esc_html__( 'Add marker', 'eac-components' ),
					'condition'   => array( 'osm_markers_import_list!' => 'yes' ),
					'separator'   => 'before',
				)
			);

		$this->end_controls_section();

		$this->start_controls_section(
			'osm_settings',
			array(
				'label' => esc_html__( 'Settings', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

			$this->add_control(
				'osm_settings_zoom_auto',
				array(
					'label'        => esc_html__( 'Auto zoom', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'description'  => esc_html__( 'Display all markers in the viewport.', 'eac-components' ),
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => '',
					'conditions'   => array(
						'relation' => 'or',
						'terms'    => array(
							array(
								'terms' => array(
									array(
										'name'     => 'osm_markers_import_list',
										'operator' => '===',
										'value'    => 'yes',
									),
									array(
										'name'     => 'osm_markers_import_type',
										'operator' => '===',
										'value'    => 'none',
									),
								),
							),
							array(
								'terms' => array(
									array(
										'name'     => 'osm_markers_import_list',
										'operator' => '!==',
										'value'    => 'yes',
									),
								),
							),
						),
					),
				)
			);

			$this->add_control(
				'osm_settings_zoom',
				array(
					'label'      => esc_html__( 'Zoom factor', 'eac-components' ),
					'type'       => Controls_Manager::NUMBER,
					'min'        => 1,
					'max'        => 20,
					'default'    => 12,
					'step'       => 1,
					'conditions' => array(
						'relation' => 'or',
						'terms'    => array(
							array(
								'terms' => array(
									array(
										'name'     => 'osm_markers_import_list',
										'operator' => '===',
										'value'    => 'yes',
									),
									array(
										'name'     => 'osm_markers_import_type',
										'operator' => '===',
										'value'    => 'none',
									),
									array(
										'name'     => 'osm_settings_zoom_auto',
										'operator' => '!==',
										'value'    => 'yes',
									),
								),
							),
							array(
								'terms' => array(
									array(
										'name'     => 'osm_markers_import_list',
										'operator' => '!==',
										'value'    => 'yes',
									),
									array(
										'name'     => 'osm_settings_zoom_auto',
										'operator' => '!==',
										'value'    => 'yes',
									),

								),
							),
						),
					),
				)
			);

			$this->add_responsive_control(
				'osm_settings_height',
				array(
					'label'       => esc_html__( 'Min. height', 'eac-components' ),
					'type'        => Controls_Manager::SLIDER,
					'size_units'  => array( 'px', 'vh' ),
					'default'     => array(
						'unit' => 'px',
						'size' => 350,
					),
					'range'       => array(
						'px' => array(
							'min'  => 120,
							'max'  => 1500,
							'step' => 50,
						),
						'vh' => array(
							'min'  => 10,
							'max'  => 100,
							'step' => 10,
						),
					),
					'selectors'   => array( '{{WRAPPER}} .osm-map_wrapper-map' => 'min-block-size: {{SIZE}}{{UNIT}};' ),
					'render_type' => 'template',
				)
			);

			$this->add_control(
				'osm_settings_layers',
				array(
					'label'       => esc_html__( 'Select the default layer', 'eac-components' ),
					'type'        => Controls_Manager::SELECT,
					'options'     => $this->base_layers,
					'default'     => 'osm_basic',
					'label_block' => true,
				)
			);

		$this->end_controls_section();

		$this->start_controls_section(
			'osm_content',
			array(
				'label' => esc_html__( 'Controls', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

			$this->add_control(
				'osm_content_fullscreen_control',
				array(
					'label'        => esc_html__( 'Fullscreen mode', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'false',
				)
			);

			$this->add_control(
				'osm_content_zoom_position',
				array(
					'label'        => esc_html__( 'Zoom Bottom-Left', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => '',
				)
			);

			$this->add_control(
				'osm_content_zoom',
				array(
					'label'        => esc_html__( 'Zoom with the mouse', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
				)
			);

			$this->add_control(
				'osm_content_dblclick',
				array(
					'label'        => esc_html__( 'Double click to zoom', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
				)
			);

			$this->add_control(
				'osm_content_draggable',
				array(
					'label'        => esc_html__( 'Dragging map', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
				)
			);

			$this->add_control(
				'osm_content_open_popup',
				array(
					'label'        => esc_html__( 'Tooltip open', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
				)
			);

			$this->add_control(
				'osm_content_click_popup',
				array(
					'label'        => esc_html__( 'Click to close the tooltips', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
				)
			);

		$this->end_controls_section();

		/**
		 * Generale Style Section
		 */
		$this->start_controls_section(
			'osm_global_style',
			array(
				'label' => esc_html__( 'Map', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'osm_global_border',
					'selector' => '{{WRAPPER}} .osm-map_wrapper-map',
				)
			);

			$this->add_control(
				'osm_global_border_radius',
				array(
					'label'      => esc_html__( 'Border radius', 'eac-components' ),
					'type'       => Controls_Manager::DIMENSIONS,
					'size_units' => array( 'px', '%' ),
					'selectors'  => array( '{{WRAPPER}} .osm-map_wrapper-map' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				)
			);

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'osm_global_border_shadow',
					'label'    => esc_html__( 'Shadow', 'eac-components' ),
					'selector' => '{{WRAPPER}} .osm-map_wrapper-map',
				)
			);

			$this->add_group_control(
				Group_Control_Css_Filter::get_type(),
				array(
					'name'     => 'css_filters',
					'selector' => '{{WRAPPER}} .osm-map_wrapper-map',
				)
			);

		$this->end_controls_section();

		$this->start_controls_section(
			'osm_title_style',
			array(
				'label' => esc_html__( 'Tooltip title', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

			$this->add_control(
				'osm_title_color',
				array(
					'label'     => esc_html__( 'Color', 'eac-components' ),
					'type'      => Controls_Manager::COLOR,
					'default'   => '#000000',
					'selectors' => array( '{{WRAPPER}} .leaflet-popup-content .osm-map_popup-title' => 'color: {{VALUE}};' ),
				)
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'     => 'osm_title_typography',
					'label'    => esc_html__( 'Typography', 'eac-components' ),
					'selector' => '{{WRAPPER}} .leaflet-popup-content .osm-map_popup-title',
				)
			);

			$start = is_rtl() ? 'right' : 'left';
			$end   = is_rtl() ? 'left' : 'right';
			$this->add_responsive_control(
				'osm_title_position',
				array(
					'label'     => esc_html__( 'Alignment', 'eac-components' ),
					'type'      => Controls_Manager::CHOOSE,
					'default'   => 'center',
					'options'   => array(
						'left'   => array(
							'title' => is_rtl() ? esc_html__( 'Right', 'eac-components' ) : esc_html__( 'Left', 'eac-components' ),
							'icon'  => "eicon-text-align-{$start}",
						),
						'center' => array(
							'title' => esc_html__( 'Center', 'eac-components' ),
							'icon'  => 'eicon-text-align-center',
						),
						'right'  => array(
							'title' => is_rtl() ? esc_html__( 'Left', 'eac-components' ) : esc_html__( 'Right', 'eac-components' ),
							'icon'  => "eicon-text-align-{$end}",
						),
					),
					'selectors_dictionary' => array(
						'left'  => 'start',
						'right' => 'end',
					),
					'selectors' => array( '{{WRAPPER}} .leaflet-popup-content .osm-map_popup-title' => 'text-align: {{VALUE}};' ),
				)
			);

		$this->end_controls_section();

		$this->start_controls_section(
			'osm_content_style',
			array(
				'label' => esc_html__( 'Tooltip content', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

			$this->add_control(
				'osm_content_color',
				array(
					'label'     => esc_html__( 'Color', 'eac-components' ),
					'type'      => Controls_Manager::COLOR,
					'default'   => '#000000',
					'selectors' => array( '{{WRAPPER}} .leaflet-popup-content .osm-map_popup-content, {{WRAPPER}} .leaflet-popup-content .osm-map_popup-content a' => 'color: {{VALUE}};' ),
				)
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'     => 'osm_content_typography',
					'label'    => esc_html__( 'Typography', 'eac-components' ),
					'selector' => '{{WRAPPER}} .leaflet-popup-content .osm-map_popup-content, {{WRAPPER}} .leaflet-popup-content .osm-map_popup-content a',
				)
			);

		$this->end_controls_section();
	}


	/**
	 * Render widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @access protected
	 */
	protected function render(): void {
		?>
		<div class="eac-open-streetmap">
			<input type="hidden" id="osm_nonce" name="osm_nonce" value="<?php echo esc_attr( wp_create_nonce( 'eac_file_osm_nonce_' . esc_attr( $this->get_id() ) ) ); ?>" />
			<?php $this->render_map(); ?>
		</div>
		<!-- En dehors du widget -->
		<div class='eac-skip-grid' tabindex='0'>
			<span class='visually-hidden'><?php esc_html_e( 'Exit map', 'eac-components' ); ?></span>
		</div>
		<?php
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @access protected
	 */
	protected function render_map(): void {
		$settings = $this->get_settings_for_display();
		$id       = $this->get_id();

		// Les balises acceptées pour le contenu du tooltip
		$allowed_content = array(
			'br'     => array(),
			'p'      => array(),
			'strong' => array(),
			'a'      => array(
				'href'   => array(),
				'target' => array(),
				'rel'    => array(),
			),
			'img'    => array(
				'src'         => array(),
				'alt'         => array(),
				'aria-hidden' => array(),
				'role'        => array(),
			),
		);

		// Les valeurs par défaut: Paris
		$center_lat     = 48.8579;
		$center_lng     = 2.3491;
		$center_title   = ! empty( $settings['osm_settings_center_title'] ) ? $settings['osm_settings_center_title'] : esc_html__( 'Tooltip title', 'eac-components' );
		$center_content = ! empty( $settings['osm_settings_center_content'] ) ? $settings['osm_settings_center_content'] : '';
		$has_mapmarkers = 'yes' === $settings['osm_markers_import_list'] ? false : true;

		// La liste des marqueurs
		$map_markers = true === $has_mapmarkers ? $settings['osm_markers_list'] : array();

		// Coordonnées à partir de l'adresse IP
		$client_ip = 'yes' === $settings['osm_settings_client_ip'] ? true : false;

		// Le moteur de recherche
		$client_search = 'yes' === $settings['osm_settings_search'] ? true : false;

		if ( $client_search && ! empty( $settings['osm_settings_center_lat'] ) && ! empty( $settings['osm_settings_center_lng'] ) ) {
				$center_lat = ! empty( $settings['osm_settings_center_lat'] ) ? $settings['osm_settings_center_lat'] : $center_lat;
				$center_lng = ! empty( $settings['osm_settings_center_lng'] ) ? $settings['osm_settings_center_lng'] : $center_lng;
		} elseif ( $client_ip && isset( $_SERVER['REMOTE_ADDR'] ) && ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
			/** Calcule de l'adresse IP du client */
			$ip_address    = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) );
			$get_geoplugin = wp_safe_remote_get( "http://www.geoplugin.net/json.gp?ip=$ip_address" );

			if ( ! is_wp_error( $get_geoplugin ) && ! empty( wp_remote_retrieve_body( $get_geoplugin ) ) ) {
				$ip = json_decode( wp_remote_retrieve_body( $get_geoplugin ), true );

				if ( 200 === $ip['geoplugin_status'] ) {
					$center_lat   = isset( $ip['geoplugin_latitude'] ) ? $ip['geoplugin_latitude'] : $center_lat;
					$center_lng   = isset( $ip['geoplugin_longitude'] ) ? $ip['geoplugin_longitude'] : $center_lng;
					$center_title = isset( $ip['geoplugin_city'] ) ? $ip['geoplugin_city'] : $center_title;
					if ( isset( $ip['geoplugin_countryName'] ) ) {
						$center_title .= ', ' . $ip['geoplugin_countryName']; }
				}
			}
		}

		// La div wrapper
		$this->add_render_attribute( 'osm_wrapper', 'class', 'osm-map_wrapper' );
		$this->add_render_attribute( 'osm_wrapper', 'data-settings', $this->get_settings_json() );

		// La div du marqueur central
		$this->add_render_attribute( 'osm_marker', 'class', 'osm-map_wrapper-markercenter' );
		$this->add_render_attribute( 'osm_marker', 'data-lat', esc_attr( $center_lat ) );
		$this->add_render_attribute( 'osm_marker', 'data-lng', esc_attr( $center_lng ) );
		?>
		<div <?php $this->print_render_attribute_string( 'osm_wrapper' ); ?>>
			<!-- La div de la carte -->
			<div id="<?php echo esc_attr( $id ); ?>" class="osm-map_wrapper-map"></div>

			<!-- Le marqueur central -->
			<div <?php $this->print_render_attribute_string( 'osm_marker' ); ?>>
				<div class="osm-map_marker-title"><?php echo esc_html( $center_title ); ?></div>
				<div class="osm-map_marker-content"><?php echo wp_kses( $center_content, $allowed_content ); ?></div>
			</div>
			<?php
			/** Les marqueurs du repeater */
			foreach ( $map_markers as $index => $marker ) {
				if ( ! empty( $marker['osm_markers_tooltip_lat'] ) && ! empty( $marker['osm_markers_tooltip_lng'] ) ) {
					$key = 'osm_markers_' . $index;
					$this->add_render_attribute(
						$key,
						array(
							'class'      => 'osm-map_wrapper-marker',
							'data-lat'   => esc_attr( $marker['osm_markers_tooltip_lat'] ),
							'data-lng'   => esc_attr( $marker['osm_markers_tooltip_lng'] ),
							'data-icon'  => $marker['osm_markers_tooltip_marker'],
							'data-sizes' => ! empty( $this->base_icons_sizes ) && isset( $this->base_icons_sizes[ $marker['osm_markers_tooltip_marker'] ] ) ? $this->base_icons_sizes[ $marker['osm_markers_tooltip_marker'] ] : $this->sizes_icons_default,
						)
					);
					?>
					<div <?php $this->print_render_attribute_string( $key ); ?>>
						<div class="osm-map_marker-title"><?php echo esc_html( $marker['osm_markers_tooltip_title'] ); ?></div>
						<div class="osm-map_marker-content"><?php echo wp_kses( $marker['osm_markers_tooltip_content'], $allowed_content ); ?></div>
					</div>
					<?php
				}
			}
			?>
		</div>
		<?php
	}

	/**
	 * get_settings_json
	 *
	 * Retrieve fields values to pass at the widget container
	 * Convert on JSON format
	 * Read by 'openstreetmap.js' file when the component is loaded on the frontend
	 *
	 *  @uses      wp_json_encode()
	 *
	 * @return    JSON oject
	 *
	 * @access    protected
	 * @updated   1.8.8
	 * @updated   1.9.5
	 */
	protected function get_settings_json(): string {
		$module_settings = $this->get_settings_for_display();

		$is_json_actif     = 'true' === $module_settings['osm_is_json_enable'] ? true : false;
		$locate            = false; /** 'yes' === $module_settings['osm_settings_client_geolocate'] ? true : false; */
		$zoomauto          = 'yes' === $module_settings['osm_settings_zoom_auto'] ? true : false;
		$layer             = isset( $this->base_layers[ $module_settings['osm_settings_layers'] ] ) ? $this->base_layers[ $module_settings['osm_settings_layers'] ] : $this->layer_default;
		$has_import        = 'yes' === $module_settings['osm_markers_import_list'] ? true : false;
		$has_import_url    = $has_import && $is_json_actif && 'url' === $module_settings['osm_markers_import_type'];
		$has_import_file   = $has_import && 'file' === $module_settings['osm_markers_import_type'] && 'none' !== $module_settings['osm_markers_import_file'];
		$file_import       = '';
		$import_icon_sizes = $this->sizes_icons_default;

		if ( $has_import_url && ! empty( $module_settings['osm_markers_import_url']['url'] ) ) {
			$file_import = $module_settings['osm_markers_import_url']['url'];
		} elseif ( $has_import_file && ! empty( $module_settings['osm_markers_import_file'] ) ) {
			$file_import = $module_settings['osm_markers_import_file'];
		}

		if ( $has_import && ! empty( $this->base_icons_sizes ) && isset( $this->base_icons_sizes[ $module_settings['osm_markers_import_marker'] ] ) ) {
			$import_icon_sizes = $this->base_icons_sizes[ $module_settings['osm_markers_import_marker'] ];
		}

		$settings = array(
			'data_id'            => esc_attr( $this->get_id() ),
			'data_geolocate'     => $locate,
			'data_zoom'          => $zoomauto && ! empty( $module_settings['osm_settings_zoom'] ) ? absint( $module_settings['osm_settings_zoom'] ) : 12,
			'data_zoompos'       => 'yes' === $module_settings['osm_content_zoom_position'] ? true : false,
			'data_zoomauto'      => $zoomauto,
			'data_layer'         => $layer,
			'data_fullscreen'    => 'yes' === $module_settings['osm_content_fullscreen_control'] ? true : false,
			'data_wheelzoom'     => 'yes' === $module_settings['osm_content_zoom'] ? true : false,
			'data_dblclick'      => 'yes' === $module_settings['osm_content_dblclick'] ? true : false,
			'data_draggable'     => 'yes' === $module_settings['osm_content_draggable'] ? true : false,
			'data_openpopup'     => 'yes' === $module_settings['osm_content_open_popup'] ? true : false,
			'data_clickpopup'    => 'yes' === $module_settings['osm_content_click_popup'] ? true : false,
			'data_import'        => $has_import,
			'data_import_url'    => esc_url( $file_import ),
			'data_import_icon'   => isset( $module_settings['osm_markers_import_marker'] ) ? $module_settings['osm_markers_import_marker'] : 'default.png',
			'data_import_sizes'  => $import_icon_sizes,
			'data_keywords'      => ! empty( $module_settings['osm_markers_import_keywords'] ) ? preg_replace( "/\r|\n/", ',', $module_settings['osm_markers_import_keywords'] ) : '',
			'data_collapse_menu' => true,
		);

		return wp_json_encode( $settings );
	}

	/**
	 * setTilesConfig
	 *
	 * Récupère la liste des tuiles du fichier de configuration
	 * et affecte les variables nécessaires à la constitution de la liste
	 */
	private function setTilesConfig() {
		$filename = $this->config_layers;
		$layers;

		$json = wp_remote_get(
			$filename,
			array(
				'timeout' => 10,
				'headers' => array( 'Accept' => 'application/json' ),
			)
		);

		if ( ! is_wp_error( $json ) && 200 === wp_remote_retrieve_response_code( $json ) && ! empty( wp_remote_retrieve_body( $json ) ) ) {
			$body   = wp_remote_retrieve_body( $json );
			$layers = json_decode( $body, true );
			if ( is_array( $layers ) ) {
				foreach ( $layers as $code => $args ) {
					$this->base_layers[ esc_attr( $code ) ] = isset( $args['options']['title'] ) ? esc_html( $args['options']['title'] ) : esc_html( $this->title_default );
				}
			} else {
				$this->base_layers = $this->base_layers_default;
			}
		} else {
			$this->base_layers = $this->base_layers_default;
		}
	}

	/**
	 * setIconsConfig
	 *
	 * Récupère la liste des icones du fichier de configuration
	 * et affecte les variables nécessaires à la constitution de la liste
	 */
	private function setIconsConfig() {
		$filename = $this->config_icons;
		$icons;

		$json = wp_remote_get(
			$filename,
			array(
				'timeout' => 10,
				'headers' => array( 'Accept' => 'application/json' ),
			)
		);

		if ( ! is_wp_error( $json ) && 200 === wp_remote_retrieve_response_code( $json ) && ! empty( wp_remote_retrieve_body( $json ) ) ) {
			$body  = wp_remote_retrieve_body( $json );
			$icons = json_decode( $body, true );
			if ( is_array( $icons ) ) {
				foreach ( $icons as $code => $args ) {
					$this->base_icons[ esc_attr( $code ) ]       = isset( $args['title'] ) ? esc_html( $args['title'] ) : esc_html( $this->title_default );
					$this->base_icons_sizes[ esc_attr( $code ) ] = isset( $args['sizes'] ) ? $args['sizes'] : $this->sizes_icons_default;
				}
			} else {
				$this->base_icons = $this->base_icons_default;
			}
		} else {
			$this->base_icons = $this->base_icons_default;
		}
	}

	protected function content_template(): void {}
}
