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

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  
use Sifency\Traits\Sifency_Label_Content_Controls_Trait;

class Sifency_Widget_Service_Features extends Widget_Base {
	use Sifency_Animation_Controls;
	use Sifency_Label_Content_Controls_Trait;

	public function get_name() {
		return 'sifency-service-features';
	}

	public function get_title() {
		return esc_html__( 'Service Features', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-bullet-list';
	}

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

	public function get_keywords() {
		return [ 'service', 'features', 'list', 'repeater', 'meta', 'dynamic', 'sifency' ];
	}
	public function get_style_depends() {
	    return [ 'sifency-typo-style', 'sifency-theme-builder-service-style' ];
    }
	protected function register_controls() {
		$this->add_sifency_label_content_controls();

		$this->update_control( 'label_text', [ 'default' => esc_html__( 'Key Features', 'sifency-addons' ) ] );
		$this->update_control( 'label_spacing', [ 'selectors' => [ '{{WRAPPER}} .sifency-features-title' => 'margin-bottom: {{SIZE}}{{UNIT}};' ] ] );
		$this->update_control( 'section_label_style', [ 'label' => esc_html__( 'Title', 'sifency-addons' ) ] );
		

		// --- Content Tab: List Settings ---
		$this->start_controls_section(
			'section_list_content',
			[ 'label' => esc_html__( 'List Settings', 'sifency-addons' ) ]
		);

		$this->add_control(
			'icon',
			[
				'label'     => esc_html__( 'List Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => [
					'value'   => 'fas fa-check-circle',
					'library' => 'fa-solid',
				],
			]
		);
		
		$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}} .sifency-features-list' => 'align-items: {{VALUE}};' ],
			]
		);

		$this->end_controls_section();

		// --- Style Tab: List Items ---
		$this->start_controls_section(
			'section_list_style',
			[ 'label' => esc_html__( 'List Items', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[ 'name' => 'item_typography', 'selector' => '{{WRAPPER}} .sifency-feature-item-text' ]
		);
		$this->add_control(
			'item_text_color',
			[ 'label' => esc_html__( 'Text Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-feature-item-text' => 'color: {{VALUE}};' ] ]
		);
		$this->add_responsive_control(
			'item_spacing',
			[
				'label'      => esc_html__( 'Spacing Between', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'selectors'  => [ '{{WRAPPER}} .sifency-features-list' => 'gap: {{SIZE}}{{UNIT}};' ],
			]
		);
		$this->end_controls_section();

		// --- Style Tab: Icon ---
		$this->start_controls_section(
			'section_icon_style',
			[ 'label' => esc_html__( 'Icon', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'icon[value]!' => '' ] ]
		);
		$this->add_control(
			'icon_color',
			[ 'label' => esc_html__( 'Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-feature-icon' => 'color: {{VALUE}};' ] ]
		);
		$this->add_responsive_control(
			'icon_size',
			[ 'label' => esc_html__( 'Size', 'sifency-addons' ), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-feature-icon i' => 'font-size: {{SIZE}}px;' ] ]
		);
		$this->add_responsive_control(
			'icon_spacing',
			[ 'label' => esc_html__( 'Spacing', 'sifency-addons' ), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-feature-icon' => 'margin-right: {{SIZE}}{{UNIT}};' ] ]
		);
		$this->end_controls_section();

		$this->add_sifency_animation_controls();
	}

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

		$features = sifency_get_field( 'service_features' );

		if ( empty( $features ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				$features = [
					[ 'feature_text' => 'High-Quality Design' ],
					[ 'feature_text' => '24/7 Premium Support' ],
					[ 'feature_text' => 'Responsive on All Devices' ],
				];
			} else {
				return;
			}
		}

		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );
		
		?>
		<div class="sifency-features-wrapper" <?php echo $animation_attrs; // phpcs:ignore ?>>
			<?php if ( 'yes' === $settings['show_label'] && ! empty( $settings['label_text'] ) ) : ?>
				<h3 class="sifency-features-title sifency-label-style"><?php echo esc_html( $settings['label_text'] ); ?></h3>
			<?php endif; ?>

			<ul class="sifency-features-list">
				<?php
				foreach ( $features as $item ) :
					if ( empty( $item['feature_text'] ) ) continue;
				?>
					<li class="sifency-feature-item">
						<?php if ( ! empty( $settings['icon']['value'] ) ) : ?>
							<span class="sifency-feature-icon"><?php Icons_Manager::render_icon( $settings['icon'], [ 'aria-hidden' => 'true' ] ); ?></span>
						<?php endif; ?>
						<span class="sifency-feature-item-text"><?php echo esc_html( $item['feature_text'] ); ?></span>
					</li>
				<?php endforeach; ?>
			</ul>
		</div>
		<?php
	}
}