<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       http://example.com
 * @since      1.0.0
 *
 * @package    Plugin_Name
 * @subpackage Plugin_Name/public
 */

/**
 * 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    Plugin_Name
 * @subpackage Plugin_Name/public
 * @author     Your Name <email@example.com>
 */
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/functions.php';

class WosmPL_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() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Plugin_Name_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Plugin_Name_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wosmpl-public.css', array(), $this->version.'.1', 'all' );

	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Plugin_Name_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Plugin_Name_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wosmpl-public.js', array( 'jquery' ), $this->version.'.1', false );
		if (null !== get_post() && get_post()->post_type == 'wosmpl_partners') {
			wosmpl_leaflet_activate();
		}

	}

/* TODO : Add config options to choose if a menu elt has to be inserted and where if true
	public function wosmpl_register_nav_menu() {
		//register_nav_menu('wosmpl_partner_list',__( 'Partners' ));
		add_filter( 'wp_nav_menu_items','add_partner_categories', 10, 2 );
		function add_partner_categories( $items, $args ) {
			if ($args->menu->slug == 'menu-gauche') {
				$map_url = wosmpl_get_map_url();
				if (strlen($map_url) == 0) {
					$map_url = '/catalogue-des-prestataires-partenaires/';
				}
				$menu = '<li class="menu-item menu-item-has-children partners">';
				$menu .= '<a href="'.$map_url.'" class="parent">Prestataires</a>';
				if ($options['wosmpl_partner_sub_menu_show'] == 'yes') {
					$menu .= wosmpl_get_categories_ul();
				}
				$menu .= '</li>';

				$items .= $menu;
			}

		return $items;
		}
	}
*/
    public function wosmpl_api() {
	register_rest_route( 'wosmpl/v1', '/sync_partners/(?P<ids>[0-9\,]+)', array(
	    'methods' => 'GET',
	    'callback' => 'retrieve_partners_from_kohinos',
	    'permission_callback' => [$this, 'check_api_permission'],
	    //example : /wp-json/wosmpl/v1/sync_partners/877
	) );
	register_rest_route( 'wosmpl/v1', '/sync_partners', array(
	    'methods' => 'GET',
	    'callback' => 'retrieve_partners_from_kohinos',
	    'permission_callback' => [$this, 'check_api_permission'],
	    //example : /wp-json/wosmpl/v1/sync_partners
	) );
    }

    /**
     * @author Mathieu Poisbeau (contact@freepius.net)
     * @date   2020-11-17
     *
     * Check if the caller has the permission or not to access to the API endpoint.
     * In our case, the caller must have the good Kohinos API key.
     */
    public function check_api_permission(WP_REST_Request $req)
    {
	$options = get_option( 'wosmpl_options' );
	$kohinosApiKey = $options['wosmpl_kohinos_api_key'];

	return $kohinosApiKey === $req->get_header('api-auth-token');
    }
}
