<?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_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_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Get_The_Title;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Event_Banner extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Get_The_Title;
	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-banner',
			'title'         => 'Event Banner',
			'icon'          => 'eicon-meta-data',
			'category'      => array( 'sifency-event-components' ),
			'style_depends' => array( 'sifency-theme-builder-style' ),
			'keywords'      => array( 'event', 'banner', 'image', 'featured', 'meta', 'sifency' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_image_content',
			array(
				'label' => esc_html__( 'Image', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'placeholder_image',
			array(
				'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(),
			array(
				'name'    => 'image_size',
				'default' => 'large',
			)
		);
		$this->add_control(
			'link',
			array(
				'label'       => esc_html__( 'Link', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => esc_html__( 'https://your-link.com', 'sifency-addons' ),
			)
		);
		$this->add_sifency_addons_title_controls();
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_image_style',
			array(
				'label' => esc_html__( 'Image', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'height',
			array(
				'label'      => esc_html__( 'Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vh' ),
				'range'      => array(
					'px' => array(
						'min' => 50,
						'max' => 1000,
					),
					'vh' => array(
						'min' => 10,
						'max' => 100,
					),
				),
				'default'    => array(
					'size' => 300,
					'unit' => 'px',
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-banner-image' => 'height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'image_border',
				'selector' => '{{WRAPPER}} .sifency-banner-image',
			)
		);
		$this->add_responsive_control(
			'image_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{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(),
			array(
				'name'     => 'image_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-banner-image',
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_overlay_style',
			array(
				'label' => esc_html__( 'Overlay', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'overlay_background',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .sifency-banner-overlay',
			)
		);
		$this->add_control(
			'overlay_opacity',
			array(
				'label'     => esc_html__( 'Opacity', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1,
						'step' => 0.05,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-banner-overlay' => 'opacity: {{SIZE}};',
				),
			)
		);
		$this->end_controls_section();
		$this->sifency_addons_post_title_style_controller();
		$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();
		$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 = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );

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

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

		?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" <?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' ), array() ); ?>>
			<?php endif; ?>

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

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