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

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

class Sifency_Widget_Event_Banner extends Widget_Base {
	use Sifency_Animation_Controls;

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

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

	public function get_icon() {
		return 'eicon-meta-data';
	}

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

	public function get_style_depends() {
	    return [ 'sifency-theme-builder-style' ];
    }

	public function get_keywords() {
		return [ 'event', 'banner', 'image', 'featured', 'meta', 'sifency' ];
	}

	protected function register_controls() {
		// --- Content Tab: Image Settings ---
		$this->start_controls_section(
			'section_image_content',
			[
				'label' => esc_html__( 'Image', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'placeholder_image',
			[
				'label'       => esc_html__( 'Placeholder Image', 'sifency-addons' ),
				'type'        => Controls_Manager::MEDIA,
				'description' => esc_html__( 'This image will be shown in the editor when no event banner is set.', 'sifency-addons' ),
			]
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			[
				'name'    => 'image_size',
				'default' => 'large',
			]
		);

		$this->add_control(
			'link',
			[
				'label'       => esc_html__( 'Link', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'dynamic'     => [ 'active' => true ],
				'placeholder' => esc_html__( 'https://your-link.com', 'sifency-addons' ),
				'separator'   => 'before',
			]
		);

		$this->end_controls_section();

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

		$this->add_responsive_control(
			'height',
			[
				'label'      => esc_html__( 'Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'vh' ],
				'range'      => [
					'px' => [ 'min' => 50, 'max' => 1000 ],
					'vh' => [ 'min' => 10, 'max' => 100 ],
				],
				'default'    => [
					'size' => 300,
					'unit' => 'px',
				],
				'selectors'  => [
					'{{WRAPPER}} .sifency-banner-image' => 'height: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'image_border',
				'selector' => '{{WRAPPER}} .sifency-banner-image',
			]
		);

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

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'image_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-banner-image',
			]
		);

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

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => 'overlay_background',
				'types'    => [ 'classic', 'gradient' ],
				'selector' => '{{WRAPPER}} .sifency-banner-overlay',
			]
		);

		$this->add_control(
			'overlay_opacity',
			[
				'label'     => esc_html__( 'Opacity', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => [ 'px' => [ 'min' => 0, 'max' => 1, 'step' => 0.05 ] ],
				'selectors' => [
					'{{WRAPPER}} .sifency-banner-overlay' => 'opacity: {{SIZE}};',
				],
			]
		);

		$this->end_controls_section();
		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$image_url = '';
		$image_id = sifency_get_field( 'event_featured_image' );
		
		if ( ! empty( $image_id ) ) {
			$image_url = wp_get_attachment_image_url( $image_id, $settings['image_size_size'] );
		} 
		elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() && ! empty( $settings['placeholder_image']['url'] ) ) {
			$image_url = $settings['placeholder_image']['url'];
		}
		if ( empty( $image_url ) ) {
			return;
		}

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

		// Link attributes
		$this->add_render_attribute( 'link', 'class', 'sifency-banner-link' );
		if ( ! empty( $settings['link']['url'] ) ) {
			$this->add_link_attributes( 'link', $settings['link'] );
		}

		// Wrapper attributes
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-banner-widget' );

		?>
		<div <?php echo wp_kses ( $this->get_render_attribute_string( 'wrapper' ), [] ); ?> <?php echo wp_kses_post ( $animation_attrs ); // phpcs:ignore ?>>
			<?php if ( ! empty( $settings['link']['url'] ) ) : ?>
				<a <?php echo wp_kses ( $this->get_render_attribute_string( 'link' ), [] ); ?>>
			<?php endif; ?>

			<div class="sifency-banner-image" style="background-image: url('<?php echo esc_url( $image_url ); ?>');">
				<div class="sifency-banner-overlay"></div>
			</div>

			<?php if ( ! empty( $settings['link']['url'] ) ) : ?>
				</a>
			<?php endif; ?>
		</div>
		<?php
	}
}