<?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_Typography;
use Elementor\Group_Control_Border;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Label_Content_Controls;
use Sifency\Traits\Sifency_Addons_TB_Separator_Content_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Digital_Badge extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Label_Content_Controls;
	use Sifency_Addons_TB_Separator_Content_Controls;
	use Sifency_Addons_Help_Controls;

	protected function get_allowed_template_types() {
		return 'digital';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-digital-badge',
			'title'         => 'Digital Badge',
			'icon'          => 'eicon-meta-data',
			'category'      => array( 'sifency-digital-components' ),
			'style_depends' => array( 'sifency-theme-builder-style' ),
			'keywords'      => array( 'digital', 'badge', 'tag', 'meta', 'sifency' ),
		);}

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

		// --- Style Tab: Color Settings ---
		$this->start_controls_section(
			'section_color_style',
			array(
				'label' => esc_html__( 'Colors', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'heading_upcoming',
			array(
				'type'  => Controls_Manager::HEADING,
				'label' => esc_html__( 'Upcoming', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'color_upcoming_text',
			array(
				'label'     => esc_html__( 'Text', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-status-badge--upcoming' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'color_upcoming_bg',
			array(
				'label'     => esc_html__( 'Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-status-badge--upcoming' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'heading_ongoing',
			array(
				'type'      => Controls_Manager::HEADING,
				'label'     => esc_html__( 'Ongoing', 'sifency-addons' ),
				'separator' => 'before',
			)
		);
		$this->add_control(
			'color_ongoing_text',
			array(
				'label'     => esc_html__( 'Text', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-status-badge--ongoing' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'color_ongoing_bg',
			array(
				'label'     => esc_html__( 'Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-status-badge--ongoing' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'heading_past',
			array(
				'type'      => Controls_Manager::HEADING,
				'label'     => esc_html__( 'Past', 'sifency-addons' ),
				'separator' => 'before',
			)
		);
		$this->add_control(
			'color_past_text',
			array(
				'label'     => esc_html__( 'Text', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-status-badge--past' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'color_past_bg',
			array(
				'label'     => esc_html__( 'Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-status-badge--past' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'heading_canceled',
			array(
				'type'      => Controls_Manager::HEADING,
				'label'     => esc_html__( 'Canceled', 'sifency-addons' ),
				'separator' => 'before',
			)
		);
		$this->add_control(
			'color_canceled_text',
			array(
				'label'     => esc_html__( 'Text', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-status-badge--canceled' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'color_canceled_bg',
			array(
				'label'     => esc_html__( 'Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-status-badge--canceled' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'label_style',
				'label'     => esc_html__( 'Label Style', 'sifency-addons' ),
				'selector'  => '.sifency-label-style',
				'exclude'   => array( 'alignment', 'tabs', 'transition' ),
				'condition' => array( 'show_label' => 'yes' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'sep_style',
				'label'      => esc_html__( 'Separator Style', 'sifency-addons' ),
				'selector'   => '.sifency-separator-style',
				'color_only' => true,
				'exclude'    => array( 'alignment', 'tabs', 'transition' ),
				'condition'  => array( 'show_separator' => 'yes' ),
			)
		);
		$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();

		$status = sifency_get_field( 'digital_badge_text' );

		if ( ! empty( $status ) ) {
			$display_status = $status;
		} elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$display_status = 'Best Seller';
		} else {
			return;
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );

		$this->add_render_attribute( 'badge', 'class', 'sifency-status-badge' );
		$this->add_render_attribute( 'badge', 'class', 'sifency-status-badge--' . esc_attr( $display_status ) );

		?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" <?php echo $animation_attrs; // phpcs:ignore ?>>
			<span <?php echo wp_kses( $this->get_render_attribute_string( 'badge' ), array() ); ?>>
				<?php echo esc_html( ucfirst( $display_status ) ); ?>
			</span>
		</div>
		<?php
	}
}
