<?php
namespace UltraAddons\Widget;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Repeater;


if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class Advance_List extends Base{
    
    /**
     * Get your widget name
     *
     * Retrieve oEmbed widget title.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string keywords
     */
    public function get_keywords() {
        return [ 'ultraaddons-elementor-lite', 'ua','list', 'item', 'ul', 'ol', 'list item', 'list-item','item-list','advance' ];
    }
    
    /**
     * Register oEmbed widget controls.
     *
     * Adds different input fields to allow the user to change and customize the widget settings.
     *
     * @since 1.0.0
     * @access protected
     */
    protected function register_controls() {
        

        //For General Section
        $this->content_general_controls();

        //For Design Section Style Tab
        $this->style_design_controls();
        
        //For Typography Section Style Tab
        $this->style_typography_controls();

        $this->style_box_controls();

       
    }
    
    /**
     * Render oEmbed widget output on the frontend.
     *
     * Written in PHP and used to generate the final HTML.
     *
     * @since 1.0.0
     * @access protected
     */
    protected function render() {
        $settings           = $this->get_settings_for_display();
        
        $this->add_render_attribute( 'wrapper', 'class', 'ua-list-item-wrapper' );
        
        
        $ultraaddons_items = $settings['list_items'];

        ?>
    <div <?php echo esc_attr( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
        <ul class="ua-list-items">
            <?php
            $serial = 1;
            foreach( $ultraaddons_items as $ultraaddons_key => $ultraaddons_item ){

                $_id = !empty( $ultraaddons_item['_id'] ) ? $ultraaddons_item['_id'] : false;
                $title = !empty( $ultraaddons_item['title'] ) ? $ultraaddons_item['title'] : false;
                $description = !empty( $ultraaddons_item['description'] ) ? $ultraaddons_item['description'] : false;
                $ultraaddons_icon     = !empty( $ultraaddons_item['icon']['value'] ) && is_string( $ultraaddons_item['icon']['value'] ) ? $ultraaddons_item['icon']['value'] : false;

                ?>
            <li class="list-item list-item-<?php echo esc_attr( $serial ); ?> elementor-repeater-item-<?php echo esc_attr( $_id ); ?>"> 
                <div class="list-item-inside">
                
                    <?php if( $ultraaddons_icon ){ ?>
                    <i class="<?php echo esc_attr( $ultraaddons_icon ); ?>"></i>
                    <?php } ?>

                    <?php if( $title ){  ?>
                    <h4 class="list-item-title"><?php echo esc_html( $title ); ?></h4>
                    <?php } ?>

                    <?php if( $description ){  ?>
                    <span class="list-item-description"><?php echo wp_kses_post( $description ); ?></span>
                    <?php } ?>
                </div>
            </li>    
            <?php
                
                $serial++;
            }
            ?>
            
        </ul> 
    </div>
          
        <?php
        
    }
    
    /**
     * General Section for Content Controls
     * 
     * @since 1.0.0.9
     */
    protected function content_general_controls() {
        $this->start_controls_section(
            'general_content',
            [
                'label'     => esc_html__( 'General', 'ultraaddons-elementor-lite' ),
                'tab'       => Controls_Manager::TAB_CONTENT,
            ]
        );
        
        
        $repeater = new Repeater();
        
        $default_icon = [
                                'value' => 'far fa-check-square',
                                'library' => 'regular',
                        ];
        
        $repeater->add_control(
                'icon',
                [
                        'label'     => __( 'Icon', 'ultraaddons-elementor-lite' ),
                        'type'      => Controls_Manager::ICONS,
                        'default'   => $default_icon,
                ]
        );
        
        $repeater->add_control(
                'title',
                [
                        'label'     => __( 'Title', 'ultraaddons-elementor-lite' ),
                        'label_block'=> true,	
                        'type'      => Controls_Manager::TEXT,
                        'default'   => __( 'List Item', 'ultraaddons-elementor-lite' ),
                ]
        );
        
        
        $repeater->add_control(
                'description',
                [
                        'label'     => __( 'Description', 'ultraaddons-elementor-lite' ),
                        'type'      => Controls_Manager::TEXTAREA,
                        'default'   => __( 'Description of List Item', 'ultraaddons-elementor-lite' ),
                ]
        );
        
        $repeater->add_control(
            'icon_color',
            [
                'label'     => __( 'Icon Color', 'ultraaddons-elementor-lite' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .ua-list-item-wrapper .ua-list-items {{CURRENT_ITEM}} i' => 'color: {{VALUE}}',
                ],
                'default'   => '#ffffff',
            ]
        );
        
        
        $repeater->add_control(
            'icon_each_bg_color',
            [
                'label'     => __( 'Icon Background', 'ultraaddons-elementor-lite' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .ua-list-item-wrapper .ua-list-items {{CURRENT_ITEM}} i,.elementor-element.ua-list-temp-temp-2 .ua-list-items {{CURRENT_ITEM}}::after' => 'background-color: {{VALUE}}',
                ],
                'default'   => '#003B8F',
                
            ]
        );
        
        $repeater->add_control(
            'item_background',
            [
                'label'     => __( 'Item Background', 'ultraaddons-elementor-lite' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .ua-list-item-wrapper .ua-list-items {{CURRENT_ITEM}}' => 'background-color: {{VALUE}}',
                ],
                'default'   => '#003B8F',
                
            ]
        );
        
        $this->add_control(
                'list_items',
                [
                        'type' => Controls_Manager::REPEATER,
                        'fields' => $repeater->get_controls(),
                        'default' => [
                                [
                                        'icon' => $default_icon,
                                        'title' => __( 'List Item #1', 'ultraaddons-elementor-lite' ),
                                        'description' => __( 'Description of List Item', 'ultraaddons-elementor-lite' ),
                                ],
                                
                                [
                                        'icon' => $default_icon,
                                        'title' => __( 'List Item #2', 'ultraaddons-elementor-lite' ),
                                        'description' => __( 'Description of List Item', 'ultraaddons-elementor-lite' ),
                                ],
                                
                                [
                                        'icon' => $default_icon,
                                        'title' => __( 'List Item #3', 'ultraaddons-elementor-lite' ),
                                        'description' => __( 'Description of List Item', 'ultraaddons-elementor-lite' ),
                                ],
                                
                        ],
                        'title_field' => '{{{ title }}}',
                ]
        );
        
               
        
        $this->end_controls_section();
    }
    
    /**
     * Alignment Section for Style Tab
     * 
     * @since 1.0.0.9
     */
    protected function style_design_controls() {
        $this->start_controls_section(
            'style_general',
            [
                'label'     => esc_html__( 'Content', 'ultraaddons-elementor-lite' ),
                'tab'       => Controls_Manager::TAB_STYLE,
            ]
        );
        
        $this->add_control(
            'template',
                [
                    'label'         => esc_html__( 'Template', 'ultraaddons-elementor-lite' ),
                    'type'          => Controls_Manager::SELECT,
                    'options' => [
                            'default'   => __( 'Default', 'ultraaddons-elementor-lite' ),
                            'temp-2'    => __( 'Template Two', 'ultraaddons-elementor-lite' ),
                            'temp-3'    => __( 'Template Three', 'ultraaddons-elementor-lite' ),
                    ],
                    'default' => 'default',
                    'prefix_class' => 'ua-list-temp-',
                ]
        );
        
        $this->add_responsive_control(
            'list-column',
                [
                    'label'         => esc_html__( 'Column', 'ultraaddons-elementor-lite' ),
                    'type'          => Controls_Manager::SELECT,
                    'options' => [
                            '100%'    => __( 'One Column', 'ultraaddons-elementor-lite' ),
                            '48%'     => __( 'Two Column', 'ultraaddons-elementor-lite' ),
                            '30.33%'  => __( 'Three Column', 'ultraaddons-elementor-lite' ),
                            '24.8%'     => __( 'Four Column', 'ultraaddons-elementor-lite' ),
                    ],
                    'desktop_default' => '30.33%',
                    'tablet_default' => '48%',
                    'mobile_default' => '100%',
                    'selectors' => [
                                        '{{WRAPPER}} .ua-list-items li' => 'width: {{VALUE}};',
                                ],
                ]
        );
        
        $this->add_control(
            'title_color',
            [
                'label'     => __( 'Title Color', 'ultraaddons-elementor-lite' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .ua-list-item-wrapper .ua-list-items li .list-item-title' => 'color: {{VALUE}}',
                ],
                'default'   => '#ffffff',
            ]
        );
         $this->add_responsive_control(
                'title_margin',
                [
                        'label' => __( 'Title Margin', 'ultraaddons-elementor-lite' ),
                        'type' => Controls_Manager::DIMENSIONS,
                        'size_units' => [ 'px' ],
                        'default'   => [
                                'size' => 10,
                                'unit' => 'px',
                        ],
                        'selectors' => [
                                '{{WRAPPER}} .list-item-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                        ],
                ]
        );
        
        $this->add_control(
            'description_color',
            [
                'label'     => __( 'Description Color', 'ultraaddons-elementor-lite' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .ua-list-item-wrapper .ua-list-items li .list-item-description' => 'color: {{VALUE}}',
                ],
                'default'   => '#dddddd',
            ]
        );
         $this->add_responsive_control(
                    'icon_size',
                    [
                            'label' => __( 'Icon Size', 'ultraaddons-elementor-lite' ),
                            'type' => Controls_Manager::SLIDER,
                            'default' => [
                                    'size' => 20,
                            ],
                            'range' => [
                                    'px' => [
                                            'min' => 0,
                                            'max' => 100,
                                    ],
                            ],
                            'selectors' => [
                                    '{{WRAPPER}} .ua-list-item-wrapper .ua-list-items li i' => 'font-size: {{SIZE}}{{UNIT}};',
                            ],
                    ]
            );
             $this->add_responsive_control(
                    'icon_space',
                    [
                            'label' => __( 'Icon Space', 'ultraaddons-elementor-lite' ),
                            'type' => Controls_Manager::SLIDER,
                            'default' => [
                                    'size' => 5,
                            ],
                            'range' => [
                                    'px' => [
                                            'min' => 0,
                                            'max' => 50,
                                    ],
                            ],
                            'selectors' => [
                                    '{{WRAPPER}} .ua-list-item-wrapper .list-item-title ' => 'margin-left: {{SIZE}}{{UNIT}};',
                            ],
                    ]
            );
        
        $this->end_controls_section();
    }
    
    /**
     * Typography Section for Style Tab
     * 
     * @since 1.0.0.9
     */
    protected function style_typography_controls() {
        $this->start_controls_section(
            'typography',
            [
                'label'     => esc_html__( 'Typography', 'ultraaddons-elementor-lite' ),
                'tab'       => Controls_Manager::TAB_STYLE,
            ]
        );
        
        
        $this->add_group_control(
                Group_Control_Typography::get_type(),
                [
                        'name' => 'title_typography',
                        'label' => 'Title Typography',
                        'selector' => '{{WRAPPER}} .ua-list-item-wrapper .list-item-title',

                ]
        );
        
        $this->add_group_control(
                Group_Control_Typography::get_type(),
                [
                        'name' => 'desc_typography',
                        'label' => 'Description Typography',
                        'selector' => '{{WRAPPER}} .ua-list-item-wrapper span.list-item-description',

                ]
        );
        
        
        $this->end_controls_section();
    }

     /**
      * @author B M Rafiul Alam
     * Typography Section for Style Tab
     * 
     * @since 1.1.0.11
     */
    protected function style_box_controls() {
        $this->start_controls_section(
            'list_style',
            [
                'label'     => esc_html__( 'Box', 'ultraaddons-elementor-lite' ),
                'tab'       => Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_responsive_control(
                'padding',
                [
                        'label' => __( 'Padding', 'ultraaddons-elementor-lite' ),
                        'type' => Controls_Manager::DIMENSIONS,
                        'size_units' => [ 'px', '%' ],
                        'default'   => [
                                'size' => 55,
                                'unit' => 'px',
                        ],
                        'selectors' => [
                                '{{WRAPPER}} .ua-list-items li.list-item .list-item-inside' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                        ],
                ]
        );
        
        $this->add_responsive_control(
                'margin',
                [
                        'label' => __( 'Margin', 'ultraaddons-elementor-lite' ),
                        'type' => Controls_Manager::DIMENSIONS,
                        'size_units' => [ 'px', '%' ],
                        'default'   => [
                                'size' => 55,
                                'unit' => 'px',
                        ],
                        'selectors' => [
                                '{{WRAPPER}} .ua-list-items li.list-item' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                        ],
                ]
        );
        $this->add_responsive_control(
			'list_box_radius',
			[
				'label'       => esc_html__( 'Box Radius', 'ultraaddons-elementor-lite' ),
				'type'        => Controls_Manager::DIMENSIONS,
				'size_units'  => [ 'px', '%' ],
				'placeholder' => [
					'top'    => '',
					'right'  => '',
					'bottom' => '',
					'left'   => '',
				],
				'selectors'   => [
					'{{WRAPPER}} .list-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
        $this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'box_shadow',
				'label' => __( 'Box Shadow', 'ultraaddons-elementor-lite' ),
				'selector' => '{{WRAPPER}} .list-item',
			]
		);
        
        $this->end_controls_section();
    }
       
       
    
}