<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class IDSC_Elementor_Icon_List extends \Elementor\Widget_Base {

    public function get_name() {
        return 'idsc_icon_list';
    }

    public function get_title() {
        return __( 'Icon List', 'idsc' );
    }

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

    public function get_categories() {
        return array( 'idsc' );
    }

    public function get_keywords() {
        return array( 'icon', 'list', 'features', 'check' );
    }

    protected function register_controls() {
        $this->start_controls_section( 'section_items', array(
            'label' => __( 'List Items', 'idsc' ),
        ) );

        $repeater = new \Elementor\Repeater();

        $repeater->add_control( 'icon', array(
            'label'       => __( 'Icon Override', 'idsc' ),
            'type'        => \Elementor\Controls_Manager::SELECT,
            'default'     => '',
            'options'     => array(
                ''            => __( 'Default', 'idsc' ),
                'check'       => __( 'Check', 'idsc' ),
                'x'           => __( 'X', 'idsc' ),
                'arrow-right' => __( 'Arrow Right', 'idsc' ),
                'star'        => __( 'Star', 'idsc' ),
                'heart'       => __( 'Heart', 'idsc' ),
                'info'        => __( 'Info', 'idsc' ),
                'plus'        => __( 'Plus', 'idsc' ),
            ),
        ) );

        $repeater->add_control( 'text', array(
            'label'   => __( 'Text', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::TEXT,
            'default' => __( 'List item', 'idsc' ),
        ) );

        $this->add_control( 'items', array(
            'label'       => __( 'Items', 'idsc' ),
            'type'        => \Elementor\Controls_Manager::REPEATER,
            'fields'      => $repeater->get_controls(),
            'default'     => array(
                array( 'icon' => '', 'text' => __( 'Feature one', 'idsc' ) ),
                array( 'icon' => '', 'text' => __( 'Feature two', 'idsc' ) ),
                array( 'icon' => '', 'text' => __( 'Feature three', 'idsc' ) ),
            ),
            'title_field' => '{{{ text }}}',
        ) );

        $this->end_controls_section();

        $this->start_controls_section( 'section_settings', array(
            'label' => __( 'Settings', 'idsc' ),
        ) );

        $this->add_control( 'icon', array(
            'label'   => __( 'Default Icon', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'check',
            'options' => array(
                'check'       => __( 'Check', 'idsc' ),
                'x'           => __( 'X', 'idsc' ),
                'arrow-right' => __( 'Arrow Right', 'idsc' ),
                'star'        => __( 'Star', 'idsc' ),
                'heart'       => __( 'Heart', 'idsc' ),
                'info'        => __( 'Info', 'idsc' ),
                'plus'        => __( 'Plus', 'idsc' ),
            ),
        ) );

        $this->add_control( 'icon_color', array(
            'label'   => __( 'Icon Color', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::COLOR,
            'default' => '#22c55e',
        ) );

        $this->add_control( 'icon_size', array(
            'label'   => __( 'Icon Size', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'medium',
            'options' => array(
                'small'  => __( 'Small', 'idsc' ),
                'medium' => __( 'Medium', 'idsc' ),
                'large'  => __( 'Large', 'idsc' ),
            ),
        ) );

        $this->end_controls_section();
    }

    protected function render() {
        $s     = $this->get_settings_for_display();
        $items = $s['items'] ?? array();

        if ( empty( $items ) ) {
            return;
        }

        $atts  = ' icon="' . esc_attr( $s['icon'] ) . '"';
        $atts .= ! empty( $s['icon_color'] ) ? ' icon_color="' . esc_attr( $s['icon_color'] ) . '"' : '';
        $atts .= ' icon_size="' . esc_attr( $s['icon_size'] ) . '"';

        $inner = '';
        foreach ( $items as $item ) {
            $icon_attr = ! empty( $item['icon'] ) ? ' icon="' . esc_attr( $item['icon'] ) . '"' : '';
            $inner .= '[idsc_icon_list_item' . $icon_attr . ']' . esc_html( $item['text'] ) . '[/idsc_icon_list_item]';
        }

        echo do_shortcode( '[idsc_icon_list' . $atts . ']' . $inner . '[/idsc_icon_list]' );
    }
}
