<?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 Static Map', 'google-maps-divi-simplified' );
	}

	public function get_fields() {
		$zoom_options = array();

		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',
			),
		);
	}

	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, '' );
		$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';

		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 Maps Divi Simplified to display this map.', 'google-maps-divi-simplified' )
				);
			}

			return '';
		}

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

new GMDS_GoogleStaticMap();
