<?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 \Elementor\Group_Control_Box_Shadow;
use \Elementor\Repeater;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Item_Style_Controls;
use Sifency\Traits\Sifency_Icon_List_Icon_Controls;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Widget_Iconlist extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Item_Style_Controls;
    use Sifency_Icon_List_Icon_Controls;
    use Sifency_Core_Text_Style;
    
    public function get_name() {
        return 'sifency-addons-icon-list';
    }

    public function get_title() {
        return esc_html__( 'Sifency Icon List', 'sifency-addons' );
    }

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

    public function get_categories() {
        return [ 'sifency-addons-widgets' ];
    }

    public function get_keywords() {
        return [ 'icon', 'list', 'animated' ];
    }

    public function get_style_depends() {
        return [ 'sifency-typo-style' , 'sifency-frontend-style', 'sifency-component-style' , 'sifency-icon' ];
    }
    
    protected function register_controls() {
        $this->start_controls_section(
            'section_content',
            [ 'label' => esc_html__( 'Icon List', 'sifency-addons' ) ]
        );
        $repeater = new Repeater();
        $repeater->add_control(
            'text',
            [ 'label' => esc_html__( 'Text', 'sifency-addons' ), 'type' => Controls_Manager::TEXT, 'default' => esc_html__( 'List Item', 'sifency-addons' ), 'label_block' => true ]
        );
        $repeater->add_control(
            'icon',
            [ 'label' => esc_html__( 'Icon', 'sifency-addons' ), 'type' => Controls_Manager::ICONS, 'default' => [ 'value' => 'icon-check', 'library' => 'sifency-icon' ] ]
        );
        $repeater->add_control(
            'link',
            [ 'label' => esc_html__( 'Link', 'sifency-addons' ), 'type' => Controls_Manager::URL, 'default' => [ 'is_external' => 'true' ] ]
        );
        $this->add_control(
            'icon_list',
            [
                'label' => esc_html__( 'List Items', 'sifency-addons' ),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [
                    [ 'text' => esc_html__( 'List Item #1', 'sifency-addons' ) ],
                    [ 'text' => esc_html__( 'List Item #2', 'sifency-addons' ) ],
                ],
                'title_field' => '{{{ text }}}',
            ]
        );
        $this->add_control(
            'layout',
            [
                'label' => esc_html__( 'Layout', 'sifency-addons' ), 'type' => Controls_Manager::CHOOSE,
                'options' => [ 'traditional' => [ 'title' => 'Default', 'icon' => 'eicon-editor-list-ul' ], 'inline' => [ 'title' => 'Inline', 'icon' => 'eicon-ellipsis-h' ] ],
                'default' => 'traditional', 'prefix_class' => 'elementor-icon-list--layout-',
            ]
        );
        $this->end_controls_section();

        // --- Style Section: List ---
        $this->start_controls_section(
            'section_style_list',
            [ 'label' => esc_html__( 'List', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ]
        );
        $this->add_responsive_control(
            'space_between',
            [ 'label' => esc_html__( 'Space Between', 'sifency-addons' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'max' => 50 ] ], 'selectors' => [ '{{WRAPPER}} .sifency-icon-list-items:not(.elementor-inline-items) .sifency-icon-list-item:not(:last-child)' => 'padding-bottom: calc({{SIZE}}{{UNIT}}/2)', '{{WRAPPER}} .sifency-icon-list-items:not(.elementor-inline-items) .sifency-icon-list-item:not(:first-child)' => 'margin-top: calc({{SIZE}}{{UNIT}}/2)', '{{WRAPPER}} .sifency-icon-list-items.elementor-inline-items .sifency-icon-list-item' => 'margin-right: calc({{SIZE}}{{UNIT}}/2); margin-left: calc({{SIZE}}{{UNIT}}/2)', '{{WRAPPER}} .sifency-icon-list-items.elementor-inline-items' => 'margin-right: calc(-{{SIZE}}{{UNIT}}/2); margin-left: calc(-{{SIZE}}{{UNIT}}/2)' ] ]
        );
        $this->end_controls_section();

        $this->add_sifency_item_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Icon List Content',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
         $this->add_sifency_icon_list_icon_controls();

        $this->add_sifency_animation_controls();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        if ( empty($settings['icon_list']) ) {
            return;
        }
        $animation_manager = new Sifency_Animation_Manager($settings);
        
        $wrapper_classes = [ 'sifency-icon-list-items' ];
        if ( 'inline' === $settings['layout'] ) {
            $wrapper_classes[] = 'elementor-inline-items';
        }
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <ul <?php echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php foreach ( $settings['icon_list'] as $index => $item ) :
                $item_classes = [
                    'sifency-icon-list-item',
                    'sifency-item-style',
                    'elementor-repeater-item-' . $item['_id']
                ];

                $item_attrs = $animation_manager->get_item_attributes($item_classes);

                $link_key = 'link_' . $item['_id'];
                $link_url = $item['link']['url'] ?? '';

                if ( ! empty( $link_url ) ) {
                    $this->add_link_attributes($link_key, $item['link']);
                }
                ?>
                <li <?php echo wp_kses_post ($item_attrs); ?>>
                    <a <?php echo wp_kses_post ($this->get_render_attribute_string($link_key)); ?>>
                        <?php if ( ! empty( $item['icon']['value'] ) ) : ?>
                            <span class="sifency-icon-list-icon sifency-icon-style">
                                <?php wp_kses_post (\Elementor\Icons_Manager::render_icon( $item['icon'], [ 'aria-hidden' => 'true' ] )); ?>
                            </span>
                        <?php endif; ?>
                        <span class="sifency-icon-list-text sifency-content-style">
                            <?php echo esc_html($item['text']); ?>
                        </span>
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
        <?php
    }
}