<?php

namespace Elementor;

if (!defined('ABSPATH')) exit; // If this file is called directly, abort.

class Themeraft_Google_Map extends Widget_Base {
	public function get_name() {
		return 'themedraft-map';
	}

	public function is_reload_preview_required() {
		return true;
	}

	public function get_title() {
		return __('Google Map', 'themedraft-core');
	}

	public function get_icon() {
		return 'eicon-google-maps';
	}

	public function get_categories() {
		return ['themedraft_elements'];
	}

	public function get_script_depends() {
		return ['google-map-js', 'google-maps-script'];
	}


	protected function register_controls() {

		$this->start_controls_section('map_marker_section_accordion',
			[
				'label' => __('Map Markers', 'themedraft-core'),
			]
		);

		$td_map_api_key = induxter_option('google_map_api_key');

		if(empty($tdMapKey)){
			$this->add_control('td_maps_api_url',
				[
					'label'         => '<span style="line-height: 22px;">Map Required an API key.To generate your own API key follow <a target="_blank" href="https://developers.google.com/maps/documentation/javascript/get-api-key">this</a> and after generate add it to Dashboard -> Induxter Options -> General -> Google Map Api Key</span>',
					'type'          => Controls_Manager::RAW_HTML,
					'separator' => 'after'
				]
			);
		}

		$repeater = new Repeater();


		$repeater->add_control(
			'map_latitude', [
				'label' => __( 'Latitude', 'themedraft-core' ),
				'type' => Controls_Manager::TEXT,
				'default' => 55.8674395,
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'map_longitude', [
				'label' => __( 'Longitude', 'themedraft-core' ),
				'type' => Controls_Manager::TEXT,
				'default' => -4.2499492,
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'marker_title', [
				'label' => __( 'Title', 'themedraft-core' ),
				'type' => Controls_Manager::TEXT,
				'default' => __('Induxter UK', 'themedraft-core'),
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'marker_dsc', [
				'label' => __( 'Marker Description', 'themedraft-core' ),
				'type' => Controls_Manager::WYSIWYG,
				'default' => __('Marker Description Here.', 'themedraft-core'),
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'marker_icon', [
				'label' => __( 'Marker Icon', 'themedraft-core' ),
				'type' => Controls_Manager::MEDIA,
				'label_block' => true,
			]
		);

		$this->add_control(
			'marker_list',
			[
				'label' => __( 'Marker List', 'themedraft-core' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[
						'map_latitude' => 55.8674395,
						'map_longitude' => -4.2499492,
						'marker_title' => __('Glasgow UK', 'themedraft-core'),
						'marker_dsc' => __('Marker Description Here.', 'themedraft-core'),
					],
				],
				'title_field' => '{{{ marker_title }}} : {{{map_latitude}}}, {{{map_longitude}}}',
			]
		);

		$this->end_controls_section();


		$this->start_controls_section(
		    'map_center_option',
		    [
		        'label' => esc_html__( 'Map Center', 'themedraft-core' ),
		        'tab'   => Controls_Manager::TAB_CONTENT,
		    ]
		);

		$this->add_control('td_center_text',
			[
				'label'         => '<span style="line-height: 22px;">By default first marker position is set as map center. But you can change that from here.</span>',
				'type'          => Controls_Manager::RAW_HTML,
				'separator' => 'after'
			]
		);

		$this->add_control(
		    'map_center_lat',
		    [
		        'label'       => __( 'Center Latitude', 'themedraft-core' ),
		        'label_block'       => true,
		        'type'        => Controls_Manager::TEXT,
		    ]
		);

		$this->add_control(
			'map_center_long',
			[
				'label'       => __( 'Center Longitude', 'themedraft-core' ),
				'label_block'       => true,
				'type'        => Controls_Manager::TEXT,
			]
		);

		$this->end_controls_section();




		$this->start_controls_section('map_control_section_accordion',
			[
				'label' => esc_html__('Map Controls', 'themedraft-core'),
			]
		);

		$this->add_responsive_control(
			'map_height',
			[
				'label' => __( 'Map Height (px)', 'themedraft-core' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [
					'px' => [
						'min' => 200,
						'max' => 1000,
					],
				],
				'devices' => [ 'desktop', 'tablet', 'mobile' ],
				'selectors' => [
					'{{WRAPPER}} .td-gmap' => 'height: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_control('map_type',
			[
				'label'   => __('Map Type', 'themedraft-core'),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'roadmap'   => __('Road Map', 'themedraft-core'),
					'satellite' => __('Satellite', 'themedraft-core'),
					'hybrid'    => __('Hybrid', 'themedraft-core'),
					'terrain'   => __('Terrain', 'themedraft-core'),
				],
				'default' => 'roadmap',
			]
		);


		$this->add_control('map_zoom',
			[
				'label'   => __('Map Zoom', 'themedraft-core'),
				'type'    => Controls_Manager::SLIDER,
				'default' => [
					'size' => 9,
				],
				'range'   => [
					'px' => [
						'min' => 0,
						'max' => 23,
					],
				],
			]
		);

		$this->add_control('enable_map_type_control',
			[
				'label' => __('Enable Map Type Controls?', 'themedraft-core'),
				'type'  => Controls_Manager::SWITCHER,
				'default' => 'no',
			]
		);

		$this->add_control('enable_zoom_controls',
			[
				'label' => esc_html__('Enable Zoom Controls?', 'themedraft-core'),
				'type'  => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control('enable_street_control',
			[
				'label' => esc_html__('Enable Street Control?', 'themedraft-core'),
				'type'  => Controls_Manager::SWITCHER,
				'default' => 'no',
			]
		);


		$this->add_control('enable_fullscreen_control',
			[
				'label' => esc_html__('Enable Fullscreen Control?', 'themedraft-core'),
				'type'  => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control('zoom_on_scroll',
			[
				'label' => esc_html__('Zoom On Scroll?', 'themedraft-core'),
				'type'  => Controls_Manager::SWITCHER,
				'default' => 'no',
			]
		);

		$this->add_control('info_window_always_open',
			[
				'label' => esc_html__('Info Window Always Open?', 'themedraft-core'),
				'type'  => Controls_Manager::SWITCHER,
				'default' => 'no',
			]
		);

		$this->add_control('info_window_hover_open',
			[
				'label' => __('Info Window Opene On Hover?', 'themedraft-core'),
				'type'  => Controls_Manager::SWITCHER,
				'default' => 'no',
			]
		);

		$this->add_control('info_window_hide_mouseout',
			[
				'label'     => __('Info Window Hide When Mouse Out?', 'themedraft-core'),
				'type'      => Controls_Manager::SWITCHER,
				'default' => 'yes',
				'condition' => [
					'info_window_hover_open' => 'yes'
				]
			]
		);

		$this->end_controls_section();



		$this->start_controls_section('map_custom_style_accordion',
			[
				'label' => __('Map Style', 'themedraft-core'),
			]
		);

		$this->add_control('map_custom_style',
			[
				'label'       => __('JSON Code', 'themedraft-core'),
				'type'        => Controls_Manager::TEXTAREA,
				'description' => 'Get your custom styling from <a href="https://snazzymaps.com/" target="_blank">here</a>',
				'label_block' => true,
			]
		);

		$this->end_controls_section();


		$this->start_controls_section(
			'info_title_color_accordion',
			[
				'label' => __( 'Info Window Style', 'themedraft-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'info_window_title_color',
			[
				'label' => __( 'Title Color', 'themedraft-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .td-map-info-title' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'info_window_tetx_color',
			[
				'label' => __( 'Description Color', 'themedraft-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .td-map-info-desc' => 'color: {{VALUE}};',
				],
			]
		);


		$this->end_controls_section();
	}

	protected function render($instance = []) {
		// get our input from the widget settings.
		$settings = $this->get_settings_for_display();

		$map_markers = $settings['marker_list'];

		if ($settings['enable_zoom_controls'] == 'yes') {
			$enable_zoom_control = 'true';
		} else {
			$enable_zoom_control = 'false';
		}

		if ($settings['enable_map_type_control'] == 'yes') {
			$map_type_control = 'true';
		} else {
			$map_type_control = 'false';
		}

		if ($settings['enable_street_control'] == 'yes') {
			$enable_street_control = 'true';
		} else {
			$enable_street_control = 'false';
		}

		if ($settings['enable_fullscreen_control'] == 'yes') {
			$enable_fullscreen_control = 'true';
		} else {
			$enable_fullscreen_control = 'false';
		}

		if ($settings['zoom_on_scroll'] == 'yes') {
			$zoom_on_scroll = 'true';
		} else {
			$zoom_on_scroll = 'false';
		}

		if ($settings['info_window_always_open'] == 'yes') {
			$automatic_open = 'true';
		} else {
			$automatic_open = 'false';
		}

		if ($settings['info_window_hover_open'] == 'yes') {
			$hover_open = 'true';
		} else {
			$hover_open = 'false';
		}

		if ($settings['info_window_hide_mouseout'] == 'yes') {
			$hover_close = 'true';
		} else {
			$hover_close = 'false';
		}

		if (!empty($settings['map_custom_style'])) {
			$map_custom_style = $settings['map_custom_style'];
		} else {
			$map_custom_style = '';
		}

        if($settings['map_center_lat'] && $settings['map_center_long']){
	        $centerlat = $settings['map_center_lat'];
	        $centerlong = $settings['map_center_long'];
        }else{
	        foreach($map_markers as $index => $value) {
		        if ($index == 0) {
			        $centerlat= $map_markers[0]['map_latitude'];
			        $centerlong= $map_markers[0]['map_longitude'];
		        }
	        }
        }


		$map_settings = [
			'zoom'              => $settings['map_zoom']['size'],
			'maptype'           => $settings['map_type'],
			'streetViewControl' => $enable_street_control,
			'centerlat'         => $centerlat,
			'centerlong'        => $centerlong,
			'scrollwheel'       => $zoom_on_scroll,
			'fullScreen'        => $enable_fullscreen_control,
			'zoomControl'       => $enable_zoom_control,
			'typeControl'       => $map_type_control,
			'automaticOpen'     => $automatic_open,
			'hoverOpen'         => $hover_open,
			'hoverClose'        => $hover_close,
		];

		$this->add_render_attribute('style_wrapper', 'data-style', $settings['map_custom_style']);
		?>


		<div class="td-map-wrapper" id="td-map-wrapper">
			<?php if (count($map_markers)) {
				?>
				<div class="td-gmap"
				     data-settings='<?php echo wp_json_encode($map_settings); ?>' <?php echo $this->get_render_attribute_string('style_wrapper'); ?>>
					<?php
					foreach ( $map_markers as $marker ) {
						?>
						<div class="td-map-marker" data-lng="<?php echo esc_html($marker['map_longitude']); ?>"
						     data-lat="<?php echo esc_html($marker['map_latitude']); ?>"
						     data-icon="<?php echo esc_url($marker['marker_icon']['url']); ?>">
							<?php if (!empty($marker['marker_title']) || !empty($marker['marker_dsc'])): ?>

								<div class='td-map-info-wrapper'>
									<h6 class='td-map-info-title'><?php echo esc_html($marker['marker_title']); ?></h6>
									<div class='td-map-info-desc'><?php echo wp_kses_post($marker['marker_dsc']); ?></div>
								</div>
							<?php endif; ?>
						</div>
						<?php
					}
					?>
				</div>
				<?php
			} ?>
		</div>


		<?php
	}
}

Plugin::instance()->widgets_manager->register(new Themeraft_Google_Map());
