<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Advanced_Map extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'     => 'sifency-google-map',
			'title'    => 'Sifency Google Map',
			'icon'     => 'eicon-google-maps',
			'category' => array( 'sifency-addons-widgets' ),
			'keywords' => array( 'map', 'google', 'location', 'address', 'iframe' ),
		);}

	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_map',
			array(
				'label' => 'Map Configuration',
			)
		);
		$this->add_control(
			'address',
			array(
				'label'       => 'Location Address',
				'type'        => Controls_Manager::TEXTAREA,
				'rows'        => 3,
				'default'     => 'London Eye, London, UK',
				'placeholder' => 'Enter any street address or place name',
				'label_block' => true,
			)
		);
		$this->add_control(
			'zoom',
			array(
				'label'   => 'Zoom Level',
				'type'    => Controls_Manager::SLIDER,
				'default' => array( 'size' => 14 ),
				'range'   => array(
					'min' => 1,
					'max' => 20,
				),
			)
		);
		$this->add_responsive_control(
			'height',
			array(
				'label'     => 'Map Height',
				'type'      => Controls_Manager::SLIDER,
				'default'   => array( 'size' => 450 ),
				'range'     => array(
					'min' => 100,
					'max' => 1000,
				),
				'selectors' => array( '{{WRAPPER}} .sf-free-map-iframe' => 'height: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style',
			array(
				'label' => 'Map Styling',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Css_Filter::get_type(),
			array(
				'name'     => 'map_filters',
				'selector' => '{{WRAPPER}} .sf-free-map-iframe iframe',
			)
		);
		$this->add_control(
			'hover_animation',
			array(
				'label'     => 'Transition Duration (s)',
				'type'      => Controls_Manager::SLIDER,
				'default'   => array( 'size' => 0.4 ),
				'selectors' => array( '{{WRAPPER}} .sf-free-map-iframe iframe' => 'transition: all {{SIZE}}s ease;' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'map_border',
				'selector' => '{{WRAPPER}} .sf-free-map-iframe',
			)
		);
		$this->add_control(
			'border_radius',
			array(
				'label'      => 'Border Radius',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array( '{{WRAPPER}} .sf-free-map-iframe' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;' ),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'map_shadow',
				'selector' => '{{WRAPPER}} .sf-free-map-iframe',
			)
		);
		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();

		if ( empty( $settings['address'] ) ) {
			echo 'Please enter an address.';
			return;
		}

		$encoded_address = urlencode( $settings['address'] );
		$zoom            = absint( $settings['zoom']['size'] );

		$map_url = "https://maps.google.com/maps?q={$encoded_address}&t=&z={$zoom}&ie=UTF8&iwloc=&output=embed";
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div class="sf-free-map-wrapper">
			<div class="sf-free-map-iframe">
				<iframe 
					width="100%" 
					height="100%" 
					src="<?php echo esc_url( $map_url ); ?>" 
					frameborder="0" 
					scrolling="no" 
					marginheight="0" 
					marginwidth="0"
					loading="lazy">
				</iframe>
			</div>
		</div>

		<?php
	}
}
