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

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

class Sifency_Widget_Event_Status extends Widget_Base {
	use Sifency_Animation_Controls;

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

	public function get_title() {
		return esc_html__( 'Event Status', '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', 'status', 'badge', 'tag', 'meta', 'sifency' ];
	}

	protected function register_controls() {
		// --- Style Tab: Badge Styling ---
		$this->start_controls_section(
			'section_badge_style',
			[
				'label' => esc_html__( 'Badge Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} .sifency-status-badge',
			]
		);
		$this->add_responsive_control(
			'badge_padding',
			[
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'selectors'  => [ '{{WRAPPER}} .sifency-status-badge' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
			]
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'badge_border',
				'selector' => '{{WRAPPER}} .sifency-status-badge',
			]
		);
		$this->add_responsive_control(
			'badge_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [ '{{WRAPPER}} .sifency-status-badge' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
			]
		);
		$this->add_responsive_control(
			'align',
			[
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'left'   => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon' => 'eicon-text-align-left' ],
					'center' => [ 'title' => esc_html__( 'Center', 'sifency-addons' ), 'icon' => 'eicon-text-align-center' ],
					'right'  => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon' => 'eicon-text-align-right' ],
				],
				'selectors' => [ '{{WRAPPER}}' => 'text-align: {{VALUE}};' ],
			]
		);
		$this->end_controls_section();

		// --- Style Tab: Color Settings ---
		$this->start_controls_section(
			'section_color_style',
			[
				'label' => esc_html__( 'Colors', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_control( 'heading_upcoming', [ 'type' => Controls_Manager::HEADING, 'label' => esc_html__( 'Upcoming', 'sifency-addons' ) ] );
		$this->add_control( 'color_upcoming_text', [ 'label' => esc_html__( 'Text', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-status-badge--upcoming' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'color_upcoming_bg', [ 'label' => esc_html__( 'Background', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-status-badge--upcoming' => 'background-color: {{VALUE}};' ] ] );
		
		$this->add_control( 'heading_ongoing', [ 'type' => Controls_Manager::HEADING, 'label' => esc_html__( 'Ongoing', 'sifency-addons' ), 'separator' => 'before' ] );
		$this->add_control( 'color_ongoing_text', [ 'label' => esc_html__( 'Text', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-status-badge--ongoing' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'color_ongoing_bg', [ 'label' => esc_html__( 'Background', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-status-badge--ongoing' => 'background-color: {{VALUE}};' ] ] );
		
		$this->add_control( 'heading_past', [ 'type' => Controls_Manager::HEADING, 'label' => esc_html__( 'Past', 'sifency-addons' ), 'separator' => 'before' ] );
		$this->add_control( 'color_past_text', [ 'label' => esc_html__( 'Text', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-status-badge--past' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'color_past_bg', [ 'label' => esc_html__( 'Background', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-status-badge--past' => 'background-color: {{VALUE}};' ] ] );
		
		$this->add_control( 'heading_canceled', [ 'type' => Controls_Manager::HEADING, 'label' => esc_html__( 'Canceled', 'sifency-addons' ), 'separator' => 'before' ] );
		$this->add_control( 'color_canceled_text', [ 'label' => esc_html__( 'Text', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-status-badge--canceled' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'color_canceled_bg', [ 'label' => esc_html__( 'Background', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-status-badge--canceled' => 'background-color: {{VALUE}};' ] ] );

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

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

		$status = sifency_get_field( 'event_status' );
		
		if ( ! empty( $status ) ) {
			$display_status = $status;
		} 
		elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$display_status = 'upcoming';
		} else {
			return; 
		}
		
		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );
		
		$this->add_render_attribute( 'badge', 'class', 'sifency-status-badge' );
		$this->add_render_attribute( 'badge', 'class', 'sifency-status-badge--' . esc_attr( $display_status ) );

		?>
		<div <?php echo $animation_attrs; // phpcs:ignore ?>>
			<span <?php echo wp_kses ( $this->get_render_attribute_string( 'badge' ), [] ); ?>>
				<?php echo esc_html( ucfirst( $display_status ) ); ?>
			</span>
		</div>
		<?php
	}
}