<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  

class Sifency_Widget_Event_Map extends Widget_Base {
	use Sifency_Animation_Controls;

	public function get_name() {
		return 'sifency-event-map';
	}

	public function get_title() {
		return esc_html__( 'Event Map', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-google-maps';
	}

	public function get_categories() {
		return [ 'sifency-event-components' ];
	}

	public function get_keywords() {
		return [ 'event', 'map', 'google', 'location', 'embed', 'meta', 'sifency' ];
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_map_content',
			[
				'label' => esc_html__( 'Map', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'placeholder_embed',
			[
				'label'       => esc_html__( 'Placeholder', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXTAREA,
				'description' => esc_html__( 'Paste a default Google Maps iframe code. This will be shown in the editor if the event has no map.', 'sifency-addons' ),
				'default'     => '<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3151.83543450937!2d144.9537353155042!3d-37.81720974202113!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x6ad642af0f115555%3A0x5045675218ce7e0!2sMelbourne%20VIC%2C%20Australia!5e0!3m2!1sen!2sus!4v1617123456789!5m2!1sen!2sus" width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy"></iframe>',
			]
		);

		$this->end_controls_section();

		// --- Style Tab: Map Styling ---
		$this->start_controls_section(
			'section_map_style',
			[
				'label' => esc_html__( 'Map Container', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_responsive_control(
			'aspect_ratio',
			[
				'label'   => esc_html__( 'Aspect Ratio', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'16_9'  => '16:9',
					'21_9'  => '21:9',
					'4_3'   => '4:3',
					'3_2'   => '3:2',
					'1_1'   => '1:1 (Square)',
					'custom' => esc_html__( 'Custom', 'sifency-addons' ),
				],
				'default' => '16_9',
				'prefix_class' => 'sifency-aspect-ratio-', // e.g., 'sifency-aspect-ratio-16_9'
			]
		);

		$this->add_responsive_control(
			'custom_height',
			[
				'label'      => esc_html__( 'Custom Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'vh' ],
				'range'      => [
					'px' => [ 'min' => 100, 'max' => 1000 ],
					'vh' => [ 'min' => 10, 'max' => 100 ],
				],
				'selectors'  => [
					'{{WRAPPER}}.sifency-aspect-ratio-custom .sifency-map-container' => 'padding-bottom: 0; height: {{SIZE}}{{UNIT}};',
				],
				'condition' => [
					'aspect_ratio' => 'custom',
				],
			]
		);
		
		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'map_border',
				'selector' => '{{WRAPPER}} .sifency-map-wrapper',
			]
		);

		$this->add_responsive_control(
			'map_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [
					'{{WRAPPER}} .sifency-map-wrapper, {{WRAPPER}} .sifency-map-wrapper iframe' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'map_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-map-wrapper',
			]
		);

		$this->end_controls_section();

		// Add animation controls from the trait
		$this->add_sifency_animation_controls();
	}

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

		$real_embed_code = sifency_get_field( 'event_map_embed' );
		
		if ( ! empty( $real_embed_code ) ) {
			$embed_code = $real_embed_code;
		} 
		elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() && ! empty( $settings['placeholder_embed'] ) ) {
			$embed_code = $settings['placeholder_embed'];
		}

		if ( empty( $embed_code ) ) {
			return;
		}

		$allowed_html = [
			'iframe' => [
				'src'             => true,
				'width'           => true,
				'height'          => true,
				'style'           => true,
				'frameborder'     => true,
				'allowfullscreen' => true,
				'loading'         => true,
				'referrerpolicy'  => true,
			],
		];
		$sanitized_embed = wp_kses ( $embed_code, $allowed_html );

		// Animation manager
		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );

		?>
		<div class="sifency-map-wrapper" <?php echo wp_kses_post ( $animation_attrs ); // phpcs:ignore ?>>
			<div class="sifency-map-container">
				<?php echo wp_kses ( $sanitized_embed ); ?>
			</div>
		</div>
		<?php
	}
}