<?php
namespace SabaiApps\Directories\Component\Map\Api;

class GoogleMapsApi extends AbstractApi {

	protected function _mapApiInfo() {
		return array(
			'label'                => __( 'Google Maps', 'directories-pro' ),
			'default_settings'     => array(
				'key' => null,
				'no'  => false,
			),
			'default_map_settings' => array(
				'type'                 => 'roadmap',
				'style'                => '',
				'marker_cluster_color' => null,
			),
			'privacy_policy'       => array(
				'url'      => 'https://policies.google.com/privacy',
				'provider' => 'Google',
			),
		);
	}

	public function mapApiLoad( array $settings, array $mapSettings ) {
		$platform          = $this->_application->getPlatform();
		$googlemaps_handle = $this->_application->Map_GoogleMapsApi_load( $settings );
		$js_deps           = is_string( $googlemaps_handle ) ? array( $googlemaps_handle ) : array();
		$js_deps[]         = 'drts-map-api';
		if ( ! empty( $mapSettings['marker_clusters'] ) ) {
			$platform->addJsFile( 'https://unpkg.com/@googlemaps/markerclusterer/dist/index.min.js', 'markerclusterer', null, false );
			$js_deps[] = 'markerclusterer';
		}
		$platform->addJsFile( 'map-googlemaps.min.js', 'drts-map-googlemaps', $js_deps, 'directories-pro' );
		// if (!empty($mapSettings['style'])) {
		// $platform->addJsFile($this->_mapStyle($mapSettings['style'], true), 'drts-map-googlemaps-style-' . str_replace(' ', '-', strtolower($mapSettings['style'])), 'drts-map-google-maps', false);
		// }
		// if (!empty($mapSettings['marker_spiderify'])) {
		// $platform->addJsFile('oms.min.js', 'spiderifier', null, 'directories-pro', true, true);
		// }
	}

	public function mapApiSettingsForm( array $settings, array $parents ) {
		return array(
			'#title' => __( 'Google Maps API Settings (Browser)', 'directories-pro' ),
			'#class' => 'drts-form-label-lg',
			'key'    => array(
				'#type'          => 'textfield',
				'#title'         => __( 'API key', 'directories-pro' ),
				'#default_value' => $settings['key'],
				'#horizontal'    => true,
				'#required'      => function ( $form ) {
					return $form->getValue( array( 'Map', 'lib', 'map' ) ) === 'googlemaps'
						|| $form->getValue( array( 'Map', 'lib', 'location_autocomplete' ) ) === 'location_googlemaps'
						|| $form->getValue( array( 'Map', 'lib', 'location_geocoding' ) ) === 'location_googlemaps';
				},
			),
			'no'     => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Do not load API', 'directories-pro' ),
				'#default_value' => ! empty( $settings['no'] ),
				'#horizontal'    => true,
				'#description'   => __( 'Enable this option if you are seeing a JavaScript API key conflict error with another application.', 'directories-pro' ),
			),
		);
	}

	public function mapApiMapSettingsForm( array $mapSettings, array $parents ) {
		return array(
			'type'                        => array(
				'#type'          => 'select',
				'#title'         => __( 'Default map type', 'directories-pro' ),
				'#options'       => array(
					'roadmap'   => __( 'Google (roadmap)', 'directories-pro' ),
					'satellite' => __( 'Google (satellite)', 'directories-pro' ),
					'hybrid'    => __( 'Google (hybrid)', 'directories-pro' ),
				),
				'#default_value' => $mapSettings['type'],
				'#horizontal'    => true,
				'#weight'        => -1,
			),
			'map_id'                      => array(
				'#type'          => 'textfield',
				'#title'         => __( 'Map ID', 'directories-pro' ),
				'#default_value' => isset( $mapSettings['map_id'] ) ? $mapSettings['map_id'] : '',
				'#horizontal'    => true,
				'#weight'        => 0,
				'#description'   => __( 'A Map ID is required to use custom map styles. If you don’t have one, you can leave this field blank. For more information, please visit: https://developers.google.com/maps/documentation/javascript/map-ids/get-map-id', 'directories-pro' ),
			),
			'marker_cluster_color'        => array(
				'#type'          => 'colorpicker',
				'#title'         => __( 'Marker cluster color', 'directories-pro' ),
				'#default_value' => $mapSettings['marker_cluster_color'],
				'#states'        => array(
					'visible' => array(
						sprintf( 'input[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'marker_clusters' ) ) ) ) => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
				'#horizontal'    => true,
				'#weight'        => 46,
			),
			'street_view_control'         => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Enable street view control', 'directories-pro' ),
				'#default_value' => ! empty( $mapSettings['street_view_control'] ),
				'#horizontal'    => true,
				'#weight'        => 51,
			),
			'disable_animate_draw_marker' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Disable animated marker drops', 'directories-pro' ),
				'#default_value' => ! empty( $mapSettings['disable_animate_draw_marker'] ),
				'#horizontal'    => true,
				'#weight'        => 52,
			),
		);
	}

	protected function _mapStyle( $style = null, $url = false ) {
		if ( ! $styles = $this->_application->getPlatform()->getCache( 'map_api_styles' ) ) {
			$styles = array(
				'Red'               => null,
				'Blue'              => null,
				'Greyscale'         => null,
				'Night'             => null,
				'Sepia'             => null,
				'Chilled'           => null,
				'Mixed'             => null,
				'Pale Dawn'         => null,
				'Apple Maps-esque'  => null,
				'Paper'             => null,
				'Hot Pink'          => null,
				'Flat Map'          => null,
				'Subtle'            => null,
				'Light Monochrome'  => null,
				'Bright and Bubbly' => null,
				'Clean Grey'        => null,
				'Subtle Greyscale'  => null,
				'Light Dream'       => null,
			);
			$styles = $this->_application->Filter( 'map_api_styles', $styles );
			ksort( $styles );
			$this->_application->getPlatform()->setCache( $styles, 'map_api_styles' );
		}

		if ( ! isset( $style ) ) {
			return array_combine( array_keys( $styles ), array_keys( $styles ) );
		}

		$file = isset( $styles[ $style ] ) ? $styles[ $style ] : 'map-googlemaps-style-' . str_replace( ' ', '-', strtolower( $style ) ) . '.min.js';

		if ( ! $url ) {
			return $file;
		}

		return strpos( $file, 'http' ) === 0 ? $file : $this->_application->System_Url_js( $file, 'directories-pro' );
	}
}
