<?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;
use Sifency\Traits\Sifency_Separator_Content_Controls_Trait;
use Sifency\Traits\Sifency_Render_Meta_Trait;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Widget_Portfolio_Technologies extends Widget_Base {
	use Sifency_Animation_Controls;
	use Sifency_Label_Content_Controls_Trait;
	use Sifency_Separator_Content_Controls_Trait;
	use Sifency_Render_Meta_Trait;
	use Sifency_Core_Text_Style;

	public function get_name() {
		return 'sifency-portfolio-technologies';
	}

	public function get_title() {
		return esc_html__( 'Portfolio Technologies', 'sifency-addons' );
	}

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

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

	public function get_keywords() {
		return [ 'portfolio', 'technologies', 'repeater', 'list', 'meta', 'dynamic', 'sifency' ];
	}
	public function get_style_depends() {
	    return [ 'sifency-typo-style' ];
    }
	protected function register_controls() {
		
		$this->start_controls_section(
			'section_list_content',
			[
				'label' => esc_html__( 'List', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'layout',
			[
				'label'   => esc_html__( 'Layout', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => [
					'vertical'   => [ 'title' => esc_html__( 'Vertical', 'sifency-addons' ), 'icon' => 'eicon-editor-list-ul' ],
					'horizontal' => [ 'title' => esc_html__( 'Horizontal', 'sifency-addons' ), 'icon' => 'eicon-ellipsis-h' ],
				],
				'default' => 'horizontal',
			]
		);

		$this->add_control(
			'icon',
			[
				'label'     => esc_html__( 'List Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => [
					'value'   => 'fas fa-check',
					'library' => 'fa-solid',
				],
			]
		);

		$this->add_control(
			'divider',
			[
				'label'        => esc_html__( 'Show Divider', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'condition' => [ 'layout' => 'horizontal' ],
			]
		);

		$this->add_control(
			'divider_char',
			[
				'label'     => esc_html__( 'Divider Character', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => '|',
				'condition' => [
					'layout'  => 'horizontal',
					'divider' => 'yes',
				],
			]
		);
		
		$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-tech-list-wrapper' => 'text-align: {{VALUE}};' ],
			]
		);

		$this->end_controls_section();

		$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-tech-list-item-text',
			]
		);
		$this->add_control(
			'item_text_color',
			[
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-tech-list-item-text' => 'color: {{VALUE}};' ],
			]
		);
		$this->add_responsive_control(
			'item_spacing_horizontal',
			[
				'label'      => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px' ],
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 50 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-tech-list--horizontal .sifency-tech-list-item' => 'margin-right: {{SIZE}}{{UNIT}}; margin-bottom: 10px;' ],
				'condition' => [ 'layout' => 'horizontal' ],
			]
		);
		$this->add_responsive_control(
			'item_spacing_vertical',
			[
				'label'      => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px' ],
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 50 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-tech-list--vertical .sifency-tech-list-item' => 'margin-bottom: {{SIZE}}{{UNIT}};' ],
				'condition' => [ 'layout' => 'vertical' ],
			]
		);
		$this->end_controls_section();

		$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-tech-list-icon' => 'color: {{VALUE}};' ],
			]
		);
		$this->add_responsive_control(
			'icon_size',
			[
				'label'      => esc_html__( 'Size', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 100 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-tech-list-icon i' => 'font-size: {{SIZE}}{{UNIT}};' ],
			]
		);
		$this->add_responsive_control(
			'icon_spacing',
			[
				'label'      => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 50 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-tech-list-icon' => 'margin-right: {{SIZE}}{{UNIT}};' ],
			]
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_divider_style',
			[
				'label'     => esc_html__( 'Divider', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 'layout' => 'horizontal', 'divider' => 'yes' ],
			]
		);
		$this->add_control(
			'divider_color',
			[
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-tech-list-divider' => 'color: {{VALUE}};' ],
			]
		);
		$this->add_responsive_control(
			'divider_spacing',
			[
				'label'      => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 50 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-tech-list-divider' => 'margin-left: {{SIZE}}{{UNIT}}; margin-right: {{SIZE}}{{UNIT}};' ],
			]
		);
		$this->end_controls_section();

		// Call the methods from our reusable traits
		$this->add_sifency_label_content_controls();
		$this->add_sifency_separator_content_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'label_style',
            'label'    => 'Label Style',
            'selector' => '.sifency-label-style',
			'target_children' => false, 
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'sep_style',
            'label'    => 'Separator Style',
            'selector' => '.sifency-separator-style',
			'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'meta_style',
            'label'    => 'Portfolio Dynamic Technologies Name',
            'selector' => '.sifency-meta-style',
			'target_children' => false,
        ]);
		$this->add_sifency_animation_controls();
	}

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

		$tech_list = sifency_get_field( 'portfolio_technologies_list' );

		if ( empty( $tech_list ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				$tech_list = [
					[ 'tech_name' => 'WordPress' ],
					[ 'tech_name' => 'Elementor' ],
					[ 'tech_name' => 'PHP' ],
				];
			} else {
				return;
			}
		}

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

		$this->add_render_attribute( 'list', 'class', 'sifency-tech-list' );
		$this->add_render_attribute( 'list', 'class', 'sifency-tech-list--' . $settings['layout'] );
		
		?>
		<div class="sifency-tech-list-wrapper" <?php echo $animation_attrs; // phpcs:ignore ?>>
			<?php if ( 'yes' === $settings['show_label'] && ! empty( $settings['label_text'] ) ) : ?>
				<h3 class="sifency-tech-title sifency-label-style"><?php echo esc_html( $settings['label_text'] ); ?></h3>
			<?php endif; ?>
            
            	<?php if ( 'yes' === $settings['show_separator'] && ! empty( $settings['separator_text'] ) ) : ?>
					<span class="sifency-separator-style">
						<?php echo esc_html( $settings['separator_text'] ); ?>
					</span>
				<?php endif; ?>

			<ul <?php echo wp_kses ( $this->get_render_attribute_string( 'list' ), [] ); ?>>
				<?php
				$total_items = count( $tech_list );
				$counter = 0;
				foreach ( $tech_list as $item ) :
					$counter++;
					if ( empty( $item['tech_name'] ) ) continue;
				?>
					<li class="sifency-tech-list-item">
						<?php if ( ! empty( $settings['icon']['value'] ) ) : ?>
							<span class="sifency-tech-list-icon"><?php Icons_Manager::render_icon( $settings['icon'], [ 'aria-hidden' => 'true' ] ); ?></span>
						<?php endif; ?>
						<span class="sifency-tech-list-item-text sifency-meta-style"><?php echo esc_html( $item['tech_name'] ); ?></span>
						<?php if ( 'yes' === $settings['divider'] && 'horizontal' === $settings['layout'] && $counter < $total_items ) : ?>
							<span class="sifency-tech-list-divider"><?php echo esc_html( $settings['divider_char'] ); ?></span>
						<?php endif; ?>
					</li>
				<?php endforeach; ?>
			</ul>
		</div>
		<?php
	}
}