<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
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_Help_Controls;
class Sifency_Widget_Event_Map extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Help_Controls;

	protected function get_allowed_template_types() {
		return 'event';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'     => 'sifency-event-map',
			'title'    => 'Event Map',
			'icon'     => 'eicon-google-maps',
			'category' => array( 'sifency-event-components' ),
			'keywords' => array( 'event', 'map', 'google', 'location', 'embed', 'meta', 'sifency' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_map_content',
			array(
				'label' => esc_html__( 'Map', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'placeholder_embed',
			array(
				'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();
		$this->start_controls_section(
			'section_map_style',
			array(
				'label' => esc_html__( 'Map Container', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'aspect_ratio',
			array(
				'label'        => esc_html__( 'Aspect Ratio', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'options'      => array(
					'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-',
			)
		);
		$this->add_responsive_control(
			'custom_height',
			array(
				'label'      => esc_html__( 'Custom Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vh' ),
				'range'      => array(
					'px' => array(
						'min' => 100,
						'max' => 1000,
					),
					'vh' => array(
						'min' => 10,
						'max' => 100,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}}.sifency-aspect-ratio-custom .sifency-map-container' => 'padding-bottom: 0; height: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array(
					'aspect_ratio' => 'custom',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'map_border',
				'selector' => '{{WRAPPER}} .sifency-map-wrapper',
			)
		);
		$this->add_responsive_control(
			'map_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{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(),
			array(
				'name'     => 'map_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-map-wrapper',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$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      = array(
			'iframe' => array(
				'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 = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() ); ?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" class="sifency-map-wrapper" <?php echo wp_kses_post ( $animation_attrs ); // phpcs:ignore ?>>
			<div class="sifency-map-container">
				<?php echo  wp_kses_post( $sanitized_embed ); // phpcs:ignore ?>
			</div>
		</div>
		<?php
	}
}
