<?php
namespace SabaiApps\Directories\Component\Location\ViewMode;

use SabaiApps\Directories\Component\View\Mode\AbstractMode;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Map\FieldType\ICoordinates;
use SabaiApps\Directories\Request;
use SabaiApps\Directories\Component\Entity\Type\Query;

class MapViewMode extends AbstractMode {

	protected function _viewModeInfo() {
		return array(
			'label'            => _x( 'Map', 'view mode label', 'directories-pro' )
				. ' ' . _x( '(deprecated)', 'view mode label', 'directories-pro' ),
			'icon'             => 'fas fa-location-dot',
			'default_settings' => array(
				'template' => 'location_map',
				'map'      => array(
					'coordinates_field' => null,
					'height'            => 600,
					'fullscreen'        => true,
					'infobox_width'     => 240,
					'view_marker_icon'  => 'default',
				),
			),
			'default_display'  => 'summary',
			'displays'         => array(
				'summary' => array(
					'label'  => _x( 'Summary', 'display name', 'directories-pro' ),
					'weight' => 1,
				),
			),
			'display_required' => false,
			'mapable'          => false,
		);
	}

	public function viewModeSupports( Bundle $bundle ) {
		if ( ! parent::viewModeSupports( $bundle )
			|| empty( $bundle->info['public'] )
		) {
			return false;
		}

		foreach ( $this->_application->Entity_Field( $bundle ) as $field ) {
			if ( ! $field_type = $this->_application->Field_Type( $field->getFieldType(), true ) ) {
				continue;
			}

			if ( $field_type instanceof ICoordinates ) {
				return true;
			}
		}
		return false;
	}

	public function viewModeSettingsForm( Bundle $bundle, array $settings, array $parents = array() ) {
		$coordinate_field_options = $this->_application->Entity_Field_options(
			$bundle,
			array(
				'interface'       => 'Map\FieldType\ICoordinates',
				'return_disabled' => true,
			)
		);
		$form                     = array(
			'map' => array(
				'coordinates_field' => array(
					'#type'             => 'select',
					'#title'            => __( 'Map coordinates field', 'directories-pro' ),
					'#horizontal'       => true,
					'#options'          => $coordinate_field_options[0],
					'#options_disabled' => array_keys( $coordinate_field_options[1] ),
					'#default_value'    => $settings['map']['coordinates_field'],
					'#required'         => true,
					'#weight'           => 3,
				),
				'height'            => array(
					'#type'          => 'slider',
					'#title'         => __( 'Map height', 'directories-pro' ),
					'#default_value' => $settings['map']['height'],
					'#min_value'     => 100,
					'#max_value'     => 1000,
					'#step'          => 10,
					'#integer'       => true,
					'#field_suffix'  => 'px',
					'#horizontal'    => true,
					'#weight'        => 10,
				),
				'infobox_width'     => array(
					'#type'          => 'slider',
					'#min_value'     => 100,
					'#max_value'     => 500,
					'#step'          => 10,
					'#default_value' => $settings['map']['infobox_width'],
					'#title'         => __( 'Map infobox width', 'directories-pro' ),
					'#field_suffix'  => 'px',
					'#integer'       => true,
					'#horizontal'    => true,
					'#weight'        => 21,
				),
			),
		);
		$marker_icon_options      = $this->_application->Map_Map_markerIconOptions( $bundle );
		if ( count( $marker_icon_options ) > 1 ) {
			$form['map']['view_marker_icon'] = array(
				'#type'          => 'select',
				'#title'         => __( 'Map marker icon', 'directories-pro' ),
				'#default_value' => $settings['map']['view_marker_icon'],
				'#options'       => $marker_icon_options,
				'#weight'        => 12,
				'#horizontal'    => true,
			);
		}

		return $form;
	}

	public function viewModeOnInitSettings( Bundle $bundle, array &$viewSettings, Request $request ) {
		if ( ! $this->_application->Map_Api() ) {
			return $viewSettings;
		}

		$viewSettings['view_settings']['map'] += $this->_application->getComponent( 'Map' )->getConfig( 'map' );
		if ( $request->isAjax() ) {
			$viewSettings['container_template']                    = 'location_map_container';
			$viewSettings['view_settings']['skip_render_entities'] = true;
		}
		if ( empty( $viewSettings['view_settings']['map']['coordinates_field'] ) ) {
			$viewSettings['view_settings']['map']['coordinates_field'] = 'location_address';
		}
		$this->_application->Location_Api_load(
			array(
				'location_map' => true,
			)
		);
	}

	public function viewModeOnQuery( Bundle $bundle, Query $query, array $settings ) {
		if ( empty( $settings['map']['coordinates_field'] )
			|| isset( $settings['query']['fields'][ $settings['map']['coordinates_field'] ] )
		) {
			return;
		}

		if ( ( ! $field = $this->_application->Entity_Field( $bundle, $settings['map']['coordinates_field'] ) )
			|| ( ! $field_type = $this->_application->Field_Type( $field->getFieldType(), true ) )
			|| ! $field_type instanceof ICoordinates
		) {
			$this->_application->logError( 'Invalid coordinates field: ' . $settings['map']['coordinates_field'] );
			return;
		}

		$query->fieldIsNotNull( $field, $field_type->mapCoordinatesInfo( $field )['lat_column'] );
	}
}
