<?php

class GMDS_GoogleStaticMap extends ET_Builder_Module {
	public $slug       = 'gmds_google_static_map';
	public $vb_support = 'on';

	protected $module_credits = array(
		'module_uri' => '',
		'author'     => 'Codex',
		'author_uri' => '',
	);

	public function init() {
		$this->name = esc_html__( 'Google Map Directions', 'google-maps-divi-simplified' );
	}

	public function get_fields() {
		$zoom_options = array();
		$overlay_position_options = array(
			'top_left'      => esc_html__( 'Top Left', 'google-maps-divi-simplified' ),
			'top_center'    => esc_html__( 'Top Center', 'google-maps-divi-simplified' ),
			'top_right'     => esc_html__( 'Top Right', 'google-maps-divi-simplified' ),
			'middle_left'   => esc_html__( 'Middle Left', 'google-maps-divi-simplified' ),
			'center'        => esc_html__( 'Centered', 'google-maps-divi-simplified' ),
			'middle_right'  => esc_html__( 'Middle Right', 'google-maps-divi-simplified' ),
			'lower_left'    => esc_html__( 'Lower Left', 'google-maps-divi-simplified' ),
			'lower_middle'  => esc_html__( 'Lower Middle', 'google-maps-divi-simplified' ),
			'lower_right'   => esc_html__( 'Lower Right', 'google-maps-divi-simplified' ),
		);

		for ( $i = 1; $i <= 20; $i++ ) {
			$zoom_options[ (string) $i ] = (string) $i;
		}

		return array(
			'address' => array(
				'label'           => esc_html__( 'Address', 'google-maps-divi-simplified' ),
				'type'            => 'text',
				'option_category' => 'basic_option',
				'toggle_slug'     => 'main_content',
				'description'     => esc_html__( 'Enter an address and the map will update automatically.', 'google-maps-divi-simplified' ),
			),
			'zoom_level' => array(
				'label'           => esc_html__( 'Zoom Level', 'google-maps-divi-simplified' ),
				'type'            => 'select',
				'option_category' => 'basic_option',
				'options'         => $zoom_options,
				'default'         => '15',
				'toggle_slug'     => 'main_content',
			),
			'allow_mouse_wheel_scroll' => array(
				'label'           => esc_html__( 'Allow Mouse Wheel Scroll', 'google-maps-divi-simplified' ),
				'type'            => 'yes_no_button',
				'option_category' => 'basic_option',
				'options'         => array(
					'off' => esc_html__( 'No', 'google-maps-divi-simplified' ),
					'on'  => esc_html__( 'Yes', 'google-maps-divi-simplified' ),
				),
				'default'         => 'off',
				'toggle_slug'     => 'main_content',
			),
				'allow_touch_operations' => array(
					'label'           => esc_html__( 'Allow Touch Operations', 'google-maps-divi-simplified' ),
					'type'            => 'yes_no_button',
				'option_category' => 'basic_option',
				'options'         => array(
					'off' => esc_html__( 'No', 'google-maps-divi-simplified' ),
					'on'  => esc_html__( 'Yes', 'google-maps-divi-simplified' ),
				),
					'default'         => 'on',
					'toggle_slug'     => 'main_content',
				),
				'search_box_position' => array(
					'label'           => esc_html__( 'Search Box Position', 'google-maps-divi-simplified' ),
					'type'            => 'select',
					'option_category' => 'layout',
					'options'         => $overlay_position_options,
					'default'         => 'lower_middle',
					'toggle_slug'     => 'main_content',
				),
				'search_box_offset_x' => array(
					'label'           => esc_html__( 'Horizontal Edge Offset (px)', 'google-maps-divi-simplified' ),
					'type'            => 'range',
					'option_category' => 'layout',
					'default'         => '20',
					'default_unit'    => 'px',
					'range_settings'  => array(
						'min'  => '0',
						'max'  => '200',
						'step' => '1',
					),
					'toggle_slug'     => 'main_content',
				),
				'search_box_offset_y' => array(
					'label'           => esc_html__( 'Vertical Edge Offset (px)', 'google-maps-divi-simplified' ),
					'type'            => 'range',
					'option_category' => 'layout',
					'default'         => '20',
					'default_unit'    => 'px',
					'range_settings'  => array(
						'min'  => '0',
						'max'  => '200',
						'step' => '1',
					),
					'toggle_slug'     => 'main_content',
				),
			);
	}

	/**
	 * Build overlay positioning styles from the selected anchor and edge offsets.
	 *
	 * @param string $position Position slug.
	 * @param int    $offset_x Horizontal edge offset in pixels.
	 * @param int    $offset_y Vertical edge offset in pixels.
	 * @return string
	 */
	private function get_overlay_position_style( $position, $offset_x, $offset_y ) {
		$style = array(
			'position:absolute',
			'display:flex',
			'pointer-events:none',
		);

		switch ( $position ) {
			case 'top_left':
				$style[] = 'top:' . $offset_y . 'px';
				$style[] = 'left:' . $offset_x . 'px';
				$style[] = 'justify-content:flex-start';
				$style[] = 'align-items:flex-start';
				break;
			case 'top_center':
				$style[] = 'top:' . $offset_y . 'px';
				$style[] = 'left:50%';
				$style[] = 'transform:translateX(-50%)';
				$style[] = 'justify-content:center';
				$style[] = 'align-items:flex-start';
				break;
			case 'top_right':
				$style[] = 'top:' . $offset_y . 'px';
				$style[] = 'right:' . $offset_x . 'px';
				$style[] = 'justify-content:flex-end';
				$style[] = 'align-items:flex-start';
				break;
			case 'middle_left':
				$style[] = 'top:50%';
				$style[] = 'left:' . $offset_x . 'px';
				$style[] = 'transform:translateY(-50%)';
				$style[] = 'justify-content:flex-start';
				$style[] = 'align-items:center';
				break;
			case 'center':
				$style[] = 'top:50%';
				$style[] = 'left:50%';
				$style[] = 'transform:translate(-50%,-50%)';
				$style[] = 'justify-content:center';
				$style[] = 'align-items:center';
				break;
			case 'middle_right':
				$style[] = 'top:50%';
				$style[] = 'right:' . $offset_x . 'px';
				$style[] = 'transform:translateY(-50%)';
				$style[] = 'justify-content:flex-end';
				$style[] = 'align-items:center';
				break;
			case 'lower_left':
				$style[] = 'bottom:' . $offset_y . 'px';
				$style[] = 'left:' . $offset_x . 'px';
				$style[] = 'justify-content:flex-start';
				$style[] = 'align-items:flex-end';
				break;
			case 'lower_right':
				$style[] = 'bottom:' . $offset_y . 'px';
				$style[] = 'right:' . $offset_x . 'px';
				$style[] = 'justify-content:flex-end';
				$style[] = 'align-items:flex-end';
				break;
			case 'lower_middle':
			default:
				$style[] = 'bottom:' . $offset_y . 'px';
				$style[] = 'left:50%';
				$style[] = 'transform:translateX(-50%)';
				$style[] = 'justify-content:center';
				$style[] = 'align-items:flex-end';
				break;
		}

		return implode( ';', $style ) . ';';
	}

	public function render( $attrs, $content = null, $render_slug = '' ) {
		$address = isset( $this->props['address'] ) ? trim( $this->props['address'] ) : '';
		$api_key = get_option( GMDS_OPTION_API_KEY, '' );
		$directions_destination = $address;
		$zoom_level = isset( $this->props['zoom_level'] ) ? absint( $this->props['zoom_level'] ) : 15;
		$allow_mouse_wheel_scroll = isset( $this->props['allow_mouse_wheel_scroll'] ) ? $this->props['allow_mouse_wheel_scroll'] : 'off';
		$allow_touch_operations = isset( $this->props['allow_touch_operations'] ) ? $this->props['allow_touch_operations'] : 'on';
		$search_box_position = isset( $this->props['search_box_position'] ) ? $this->props['search_box_position'] : 'lower_middle';
		$search_box_offset_x = isset( $this->props['search_box_offset_x'] ) ? absint( $this->props['search_box_offset_x'] ) : 20;
		$search_box_offset_y = isset( $this->props['search_box_offset_y'] ) ? absint( $this->props['search_box_offset_y'] ) : 20;
		$overlay_position_style = $this->get_overlay_position_style( $search_box_position, $search_box_offset_x, $search_box_offset_y );

		if ( empty( $address ) ) {
			return '';
		}

		if ( empty( $api_key ) ) {
			if ( current_user_can( 'manage_options' ) ) {
				return sprintf(
						'<div class="gmds-map-notice">%s</div>',
						esc_html__( 'Set a Google Maps API key in Settings > Google Map Directions for Divi to display this map.', 'google-maps-divi-simplified' )
					);
			}

			return '';
		}

		$directions_overlay = sprintf(
			'<div class="gmds-google-map__overlay" style="%1$s"><form class="gmds-directions-form" action="%2$s" method="get" target="_blank" rel="noopener" style="%3$s"><input type="hidden" name="api" value="1" /><input type="hidden" name="travelmode" value="driving" /><input type="hidden" name="destination" value="%4$s" /><input type="text" class="gmds-directions-input" name="origin" placeholder="%5$s" autocomplete="street-address" spellcheck="false" style="%6$s" /><button type="submit" class="gmds-directions-button" style="%7$s">%8$s</button></form></div>',
			esc_attr( $overlay_position_style ),
			esc_url( 'https://www.google.com/maps/dir/' ),
			esc_attr( 'display:flex;align-items:center;gap:12px;width:min(100%,560px);padding:12px 14px;border-radius:999px;background:rgba(255,255,255,0.96);box-shadow:0 12px 30px rgba(0,0,0,0.18);backdrop-filter:blur(8px);pointer-events:auto;' ),
				esc_attr( $directions_destination ),
			esc_attr__( 'Starting point', 'google-maps-divi-simplified' ),
			esc_attr( 'flex:1 1 auto;height:48px;padding:0 16px;border:0;outline:none;background:transparent;color:#202124;font-size:15px;line-height:48px;' ),
			esc_attr( 'flex:0 0 auto;height:48px;padding:0 22px;border:0;border-radius:999px;background:#1a73e8;color:#fff;font-size:14px;font-weight:600;cursor:pointer;white-space:nowrap;' ),
			esc_html__( 'Get Directions', 'google-maps-divi-simplified' )
		);

		return sprintf(
			'<div class="gmds-google-map" data-address="%1$s" data-api-key="%2$s" data-directions-origin="%3$s" data-zoom-level="%4$d" data-allow-mouse-wheel-scroll="%5$s" data-allow-touch-operations="%6$s" style="%7$s" role="img" aria-label="%1$s"><div class="gmds-google-map__canvas" style="%8$s"></div>%9$s</div>',
			esc_attr( $address ),
			esc_attr( $api_key ),
				esc_attr( $directions_destination ),
			max( 1, min( 20, $zoom_level ) ),
			esc_attr( $allow_mouse_wheel_scroll ),
			esc_attr( $allow_touch_operations ),
			esc_attr( 'position:relative;width:100%;height:350px;border-radius:8px;overflow:hidden;background:#e5e3df;' ),
			esc_attr( 'width:100%;height:100%;' ),
			$directions_overlay
		);
	}
}

new GMDS_GoogleStaticMap();
