<?php

/**
 * Modules | Map Google
 *
 * @package Dotspice
 * @version 1.3.0
 */


if (defined('DOTSPICE_MAP_GOOGLE_ENABLE') && DOTSPICE_MAP_GOOGLE_ENABLE) {

	/**
	 * Map Google | Admin | Post Type
	 */
	function dotspice_mg_post_type($post_types)
	{
		$post_types[] = array(
			'post_type'          => 'map-google',
			'menu_name'          => esc_html__('Maps', 'dotspice'),
			'singular_name'      => esc_html__('Map', 'dotspice'),
			'multiple_name'      => esc_html__('Maps', 'dotspice'),
			'show_in_menu'       => 'theme-settings',
			'public'             => false,
			'publicly_queryable' => false,
			'menu_icon'          => 'dashicons-location-alt',
			'supports'           => array('title')
		);

		return $post_types;
	}
	add_filter('dotspice_cpt', 'dotspice_mg_post_type');


	/**
	 * Map Google | Admin | Scortcode
	 */
	function dotspice_mg_get_shortcode($post_id)
	{
		if (!empty($post_id)) {
			$shortcode = '[map-google ids="' . $post_id . '"]';

			echo '<span class="shortcode wp-ui-highlight"><input type="text"'
				. ' onfocus="this.select();" readonly="readonly"'
				. ' value="' . esc_attr($shortcode) . '"'
				. ' class="large-text code" /></span>';
		}
	}


	/**
	 * Map Google | Admin | After Title
	 */
	function dotspice_mg_add_shortcode_after_title($post)
	{
		if ($post->post_type === 'map-google') {
			dotspice_mg_get_shortcode($post->ID);
		}
	}
	add_action('edit_form_after_title', 'dotspice_mg_add_shortcode_after_title');


	/**
	 * Map Google | Admin | Post Type Columns
	 */
	function dotspice_mg_add_columns($columns)
	{
		$columns = array_slice($columns, 0, 2, true) + array('shortcode' => __('Shortcode', 'dotspice')) + array_slice($columns, 2, count($columns) - 2, true);

		if (isset($columns['pur'])) {
			unset($columns['pur']);
		}

		return $columns;
	}
	add_filter('manage_map-google_posts_columns', 'dotspice_mg_add_columns', 99);

	function dotspice_mg_manage_columns($column_name, $post_id)
	{
		if ($column_name == 'shortcode') {
			dotspice_mg_get_shortcode($post_id);
		}
		return $column_name;
	}
	add_filter('manage_map-google_posts_custom_column', 'dotspice_mg_manage_columns', 10, 2);


	/**
	 * Slider Swiper | Admin | Register Google API key
	 */
	function dotspice_mg_register_api_key()
	{
		acf_update_setting('google_api_key', 'AIzaSyArV6CtaMQvN44fC3OR8fs8TXyuSvITukk');
	}
	add_action('acf/init', 'dotspice_mg_register_api_key');


	/**
	 * Map Google | Shortcode
	 */
	function dotspice_mg_shortcode($atts)
	{
		if (is_feed()) {
			return '[map-google]';
		}
		if (!class_exists('acf_pro')) {
			return '[map-google 404 "Advanced Custom Fields PRO"]';
		}

		$atts = shortcode_atts(array(
			'ids' => ''
		), $atts);
		$markers = array();

		// Post
		$post_IDs     = wp_parse_id_list($atts['ids']);
		$theme_colors = dotspice_get_theme_option('colors');
		$marker_color = !empty($theme_colors['primary']) ? $theme_colors['primary'] : '#EA4335';

		if (empty($post_IDs)) {
			$post_IDs = get_posts(array(
				'fields'         => 'ids',
				'posts_per_page' => -1,
				'post_type'      => 'map-google'
			));

			if (empty($post_IDs)) {
				return '[map-google 404 "IDs"]';
			}
		}

		foreach ($post_IDs as $post_ID) {
			$post = get_post($post_ID);

			if (empty($post)) {
				return '[map-google 404 "post ' . esc_attr($post_ID) . '"]';
			}
			if ($post->post_status != 'publish') {
				return '[map-google post_status ' . esc_attr($post_ID) . ' "' . esc_attr($post->post_status) . '"]';
			}
			if ($post->post_type != 'map-google') {
				return '[map-google post_type ' . esc_attr($post_ID) . ' "' . esc_attr($post->post_type) . '"]';
			}

			$markers[] = array(
				'marker'   => get_field('marker', $post_ID) ? get_field('marker', $post_ID) : esc_attr($marker_color),
				'content'  => get_field('content', $post_ID),
				'location' => get_field('location', $post_ID),
			);
		}

		if (empty($markers)) {
			return '[map-google 404 "markers"]';
		}

		// Google Map
		ob_start();
		$map_default_zoom = dotspice_get_theme_option('map_default_zoom') ?: '12';
		$map_default_multiple_zoom = dotspice_get_theme_option('map_default_multiple_zoom') ?: '17';
		$map_default_style = dotspice_get_theme_option('google_maps_json') ?: '[]';
?>

		<div class="acf-map" data-style='<?php echo $map_default_style; ?>' data-zoom="<?php echo $map_default_zoom; ?>" data-zoom-multiple="<?php echo $map_default_multiple_zoom; ?>">
			<?php
			foreach ($markers as $marker) {
				echo sprintf(
					'<div class="marker" data-lat="%s" data-lng="%s" data-marker="%s">%s</div>',
					esc_attr($marker['location']['lat']),
					esc_attr($marker['location']['lng']),
					esc_attr($marker['marker']),
					wp_kses_post($marker['content'])
				);
			}
			?>
		</div>
<?php
		$map_html = ob_get_clean();

		// Enqueue Scripts
		wp_enqueue_script('map-google-googleapis', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyArV6CtaMQvN44fC3OR8fs8TXyuSvITukk', array(), '', true);
		wp_enqueue_script('map-google', get_template_directory_uri() . '/includes/modules/map-google/js/map-google.js', array('jquery'), '', true);


		// Output
		return $map_html;
	}
	add_shortcode('map-google', 'dotspice_mg_shortcode');
}
