<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Repeater;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Core_Text_Style;
use Sifency\Traits\Sifency_Icon_Style_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_FunFact extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Core_Text_Style;
    use Sifency_Icon_Style_Controls;

	public function get_name() {
		return 'sifency-addons-funfact';
	}

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

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

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

	public function get_style_depends() {
	    return [ 'sifency-typo-style' , 'sifency-icon', 'sifency-component-style' ];
    }

    public function get_script_depends() {
        return [ 'sifency-counter-script' ];
    }
    
	protected function register_controls() {
		// --- Content Section ---
		$this->start_controls_section( 'section_content', [ 'label' => 'Counters' ] );
		$repeater = new Repeater();
		$repeater->add_control( 'title', [ 'label' => 'Title', 'type' => Controls_Manager::TEXT, 'default' => 'Happy Clients' ] );
        $repeater->add_control( 'icon', [ 'label' => 'Icon', 'type' => Controls_Manager::ICONS, ] );
		$repeater->add_control( 'ending_number', [ 'label' => 'Ending Number', 'type' => Controls_Manager::NUMBER, 'default' => 1200 ] );
        $repeater->add_control( 'prefix', [ 'label' => 'Number Prefix', 'type' => Controls_Manager::TEXT ] );
        $repeater->add_control( 'suffix', [ 'label' => 'Number Suffix', 'type' => Controls_Manager::TEXT, 'default' => '+' ] );
		$this->add_control(
			'counters',
			[
				'label' => 'Counter Items', 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(),
				'default' => [
					[ 'title' => 'Happy Clients', 'ending_number' => 1200, 'suffix' => '+' ],
                    [ 'title' => 'Projects Done', 'ending_number' => 850, 'suffix' => 'K' ],
				],
				'title_field' => '{{{ title }}}',
			]
		);
        $this->add_responsive_control(
			'columns',
			[
				'label' => 'Columns', 'type' => Controls_Manager::SELECT,
				'options' => [ '1' => '1', '2' => '2', '3' => '3', '4' => '4' ],
				'default' => '2',
                'selectors' => [ '{{WRAPPER}} .sifency-fun-facts' => 'grid-template-columns: repeat({{VALUE}}, 1fr);' ]
			]
		);
        $this->add_control(
            'duration',
            [ 'label' => 'Animation Duration (ms)', 'type' => Controls_Manager::NUMBER, 'default' => 2000, 'min' => 100 ]
        );
		$this->end_controls_section();
        
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'title_style',
            'label'    => 'Title Style',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'number_style',
            'label'    => 'Number Style',
            'selector' => '.sifency-number-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Prefix / Suffix Style',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_icon_style_controls();
        $this->add_sifency_animation_controls();
	}

    /**
     * Render the widget output on the frontend.
     * @since 1.0.4
     * @access protected
     */
    protected function render() {
        $settings = $this->get_settings_for_display();
        if ( empty($settings['counters']) ) { return; }
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = ['sifency-fun-facts'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <div <?php 
            
            echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php foreach ($settings['counters'] as $item) : ?>
                <?php
                $item_classes = ['sifency-fun-fact-item'];
                $item_attrs = $animation_manager->get_item_attributes($item_classes);
                ?>
                <div <?php echo wp_kses_post ($item_attrs); ?>>
                    <?php if ( ! empty($item['icon']['value']) ) : ?>
                        <div class="sifency-fun-fact-icon sifency-icon-style">
                            <?php \Elementor\Icons_Manager::render_icon($item['icon']); ?>
                        </div>
                    <?php endif; ?>
                    <div class="sifency-fun-fact-content">
                        <div class="sifency-fun-fact-number">
                            <?php if ( ! empty($item['prefix']) ) : ?>
                                <span class="sifency-fun-fact-prefix sifency-content-style"><?php echo esc_html($item['prefix']); ?></span>
                            <?php endif; ?>
                            <span class="sifency-counter sifency-number-style" data-to-value="<?php echo esc_attr($item['ending_number']); ?>" data-duration="<?php echo esc_attr($settings['duration']); ?>">0</span>
                            <?php if ( ! empty($item['suffix']) ) : ?>
                                <span class="sifency-fun-fact-suffix sifency-content-style"><?php echo esc_html($item['suffix']); ?></span>
                            <?php endif; ?>
                        </div>
                        <?php if ( ! empty($item['title']) ) : ?>
                            <h3 class="sifency-fun-fact-title sifency-heading-style"><?php echo esc_html($item['title']); ?></h3>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        <?php
    }
}