<?php
namespace OnScheduleEdgeNamespace\Modules\Shortcodes\GoogleMap;

use OnScheduleEdgeNamespace\Modules\Shortcodes\Lib\ShortcodeInterface;

class GoogleMap implements ShortcodeInterface{
	private $base;

	function __construct() {
		$this->base = 'edgtf_google_map';
		add_action('vc_before_init', array($this, 'vcMap'));
	}
	
	public function getBase() {
		return $this->base;
	}
	
	public function vcMap() {
		vc_map(
			array(
				'name' => esc_html__('Edge Google Map', 'onschedule'),
				'base' => $this->base,
				'category' => esc_html__('by EDGE', 'onschedule'),
				'icon' => 'icon-wpb-google-map extended-custom-icon',
				'show_settings_on_create' => true,
				'params' => array(
					array(
						'type'       => 'textfield',
						'param_name' => 'address1',
						'heading'    => esc_html__('Address 1', 'onschedule')
					),
					array(
						'type'       => 'textfield',
						'param_name' => 'address2',
						'heading'    => esc_html__('Address 2', 'onschedule'),
						'dependency' => Array('element' => 'address1', 'not_empty' => true)
					),
					array(
						'type'       => 'textfield',
						'param_name' => 'address3',
						'heading'    => esc_html__('Address 3', 'onschedule'),
						'dependency' => Array('element' => 'address2', 'not_empty' => true)
					),
					array(
						'type'       => 'textfield',
						'param_name' => 'address4',
						'heading'    => esc_html__('Address 4', 'onschedule'),
						'dependency' => Array('element' => 'address3', 'not_empty' => true)
					),
					array(
						'type'       => 'textfield',
						'param_name' => 'address5',
						'heading'    => esc_html__('Address 5', 'onschedule'),
						'dependency' => Array('element' => 'address4', 'not_empty' => true)
					),
					array(
						'type'        => 'dropdown',
						'param_name'  => 'custom_map_style',
						'heading'     => esc_html__('Custom Map Style', 'onschedule'),
						'value'       => array_flip(onschedule_edge_get_yes_no_select_array(false)),
						'description' => esc_html__('Enabling this option will allow Map editing', 'onschedule')
					),
					array(
						'type'        => 'colorpicker',
						'param_name'  => 'color_overlay',
						'heading'     => esc_html__('Color Overlay', 'onschedule'),
						'description' => esc_html__('Choose a Map color overlay', 'onschedule'),
						'dependency'  => Array('element' => 'custom_map_style', 'value' => array('yes'))
					),
					array(
						'type'        => 'textfield',
						'param_name'  => 'saturation',
						'heading'     => esc_html__('Saturation', 'onschedule'),
						'description' => esc_html__('Choose a level of saturation (-100 = least saturated, 100 = most saturated)', 'onschedule'),
						'dependency'  => Array('element' => 'custom_map_style', 'value' => array('yes'))
					),
					array(
						'type'        => 'textfield',
						'param_name'  => 'lightness',
						'heading'     => esc_html__('Lightness', 'onschedule'),
						'description' => esc_html__('Choose a level of lightness (-100 = darkest, 100 = lightest)', 'onschedule'),
						'dependency'  => Array('element' => 'custom_map_style', 'value' => array('yes'))
					),
					array(
						'type'        => 'attach_image',
						'param_name'  => 'pin',
						'heading'     => esc_html__('Pin', 'onschedule'),
						'description' => esc_html__('Select a pin image to be used on Google Map', 'onschedule')
					),
					array(
						'type'        => 'textfield',
						'param_name'  => 'zoom',
						'heading'     => esc_html__('Map Zoom', 'onschedule'),
						'description' => esc_html__('Enter a zoom factor for Google Map (0 = whole worlds, 19 = individual buildings)', 'onschedule')
					),
					array(
						'type'        => 'dropdown',
						'param_name'  => 'scroll_wheel',
						'heading'     => esc_html__('Zoom Map on Mouse Wheel', 'onschedule'),
						'value'       => array_flip(onschedule_edge_get_yes_no_select_array(false)),
						'description' => esc_html__('Enabling this option will allow users to zoom in on Map using mouse wheel', 'onschedule')
					),
					array(
						'type'       => 'textfield',
						'param_name' => 'map_height',
						'heading'    => esc_html__('Map Height', 'onschedule')
					),
					array(
						'type'        => 'textarea_html',
						'param_name'  => 'content',
						'heading'     => esc_html__('Map Info Content', 'onschedule'),
						'description' => esc_html__('Add the info that will appear in the content box inside the map.', 'onschedule')
					),
					array(
						'type'        => 'dropdown',
						'param_name'  => 'info_position',
						'heading'     => esc_html__('Info Position', 'onschedule'),
						'value'       => array(
							esc_html__('Left', 'onschedule')  => 'left',
							esc_html__('Right', 'onschedule') => 'right'
						),
						'save_always' => true
					),
					array(
						'type'        => 'textfield',
						'param_name'  => 'info_width',
						'heading'     => esc_html__('Info Content Width (px or %)', 'onschedule')
					),
					array(
						'type'        => 'textfield',
						'param_name'  => 'info_top_offset',
						'heading'     => esc_html__('Info Content Top Offset (px or %)', 'onschedule')
					),
					array(
						'type'        => 'textfield',
						'param_name'  => 'info_side_offset',
						'heading'     => esc_html__('Info Content Side Offset (px or %)', 'onschedule')
					)
				)
			) );
	}

	public function render($atts, $content = null) {
		$args = array(
			'address1'         => '',
			'address2'         => '',
			'address3'         => '',
			'address4'         => '',
			'address5'         => '',
			'custom_map_style' => 'no',
			'color_overlay'    => '#393939',
			'saturation'       => '-100',
			'lightness'        => '-60',
			'zoom'             => '12',
			'pin'              => '',
			'scroll_wheel'     => 'no',
			'map_height'       => '600',
			'info_position'    => 'left',
			'info_width'       => '',
			'info_top_offset'  => '',
			'info_side_offset' => ''
		);
		
		$params = shortcode_atts($args, $atts);
		extract($params);

		$rand_id = mt_rand(100000,3000000);

		$params['map_data'] = $this->getMapDate($params, $rand_id);
		$params['map_id'] = 'edgtf-map-'.$rand_id;

		$params['info_styles'] = $this->getInfoContentStyles($params);
		
		$params['content'] = preg_replace('#^<\/p>|<p>$#', '', $content);

		$html = onschedule_edge_get_shortcode_module_template_part('templates/google-map-template', 'google-map', '', $params);

		return $html;
	}

	/**
	 * Return Elements Holder Item style
	 *
	 * @param $params
	 * @return array
	 */
	private function getMapDate($params, $id) {
		$map_data = array();

		$addresses_array = array();

		if ($params['address1'] != '') {
			array_push($addresses_array, esc_attr($params['address1']));
		}

		if ($params['address2'] != '') {
			array_push($addresses_array, esc_attr($params['address2']));
		}

		if ($params['address3'] != '') {
			array_push($addresses_array, esc_attr($params['address3']));
		}

		if ($params['address4'] != '') {
			array_push($addresses_array, esc_attr($params['address4']));
		}

		if ($params['address5'] != '') {
			array_push($addresses_array, esc_attr($params['address5']));
		}

		if ($params['pin'] != "") {
			$map_pin = wp_get_attachment_image_src($params['pin'], 'full', true);
			$map_pin = $map_pin[0];
		} else {
			$map_pin = get_template_directory_uri() . "/assets/img/pin.png";
		}

		$map_data[] = "data-addresses='[\"". implode('","', $addresses_array) . "\"]'";
		$map_data[] = 'data-custom-map-style='. $params['custom_map_style'];
		$map_data[] = 'data-color-overlay='. $params['color_overlay'];
		$map_data[] = 'data-saturation='. $params['saturation'];
		$map_data[] = 'data-lightness='. $params['lightness'];
		$map_data[] = 'data-zoom='. $params['zoom'];
		$map_data[] = 'data-pin='. $map_pin;
		$map_data[] = 'data-unique-id='. $id;
		$map_data[] = 'data-scroll-wheel='. $params['scroll_wheel'];
		$map_data[] = 'data-height='. $params['map_height'];

		return implode(' ', $map_data);
	}

	/**
	 * Return Google Map Info Content style
	 *
	 * @param $params
	 * @return array
	 */
	private function getInfoContentStyles($params) {
		$styles = array();

		if($params['info_position'] !== 'left') {
			$styles[] = 'left: auto';
		}

		if($params['info_width'] !== '') {
			$styles[] = 'width: '. $params['info_width'];
		}

		if($params['info_top_offset'] !== '') {
			$styles[] = 'top: '. $params['info_top_offset'];
		}

		if($params['info_side_offset'] !== '') {
			$styles[] = $params['info_position']. ': '. $params['info_side_offset'];
		}

		return $styles;
	}
}
