<?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\Group_Control_Background;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Widget_Progressbar extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Core_Text_Style;

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

    public function get_title() {
        return __( 'Sifency Progress Bar', 'sifency-addons' );
    }

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

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

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

    public function get_script_depends() {
        return [ 'sifency-progressbar-script' ];
    } 
    
    protected function register_controls() {
        // --- Content Section ---
        $this->start_controls_section( 'section_content', [ 'label' => 'Progress Bars' ] );
        $repeater = new Repeater();
        $repeater->add_control( 'title', [ 'label' => 'Title', 'type' => Controls_Manager::TEXT, 'default' => 'Web Design' ] );
        $repeater->add_control( 'percent', [ 'label' => 'Percentage', 'type' => Controls_Manager::SLIDER, 'default' => [ 'size' => 75 ] ] );
        $this->add_control(
            'progress_bars',
            [
                'label' => 'Items', 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(),
                'default' => [
                    [ 'title' => 'Web Design', 'percent' => [ 'size' => 85 ] ],
                    [ 'title' => 'Development', 'percent' => [ 'size' => 92 ] ],
                ],
                'title_field' => '{{{ title }}}',
            ]
        );
        $this->add_control( 'display_percentage', [ 'label' => 'Display Percentage', 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] );
        $this->end_controls_section();

        $this->start_controls_section(
            'section_style_progressbar',
            [
                'label' => __( 'Bar Style', 'sifency-addons' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'bar_color',
            [
                'label' => __( 'Color', 'sifency-addons' ),
                'type' => Controls_Manager::COLOR,
                'types' => [ 'classic', 'gradient' ],
                'selector' => '{{WRAPPER}} .sifency-progress-bar__fill',
                'description' => 'Controls the color of the filled part of the progress bar.',
            ]
        );

        $this->add_control(
            'bar_bg_color',
            [
                'label' => __( 'Background Color', 'sifency-addons' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sifency-progress-bar' => 'background-color: {{VALUE}};',
                ],
                'description' => 'Controls the color of the bar track (the background).',
            ]
        );

        $this->add_responsive_control(
            'bar_height',
            [
                'label' => __( 'Height', 'sifency-addons' ),
                'type' => Controls_Manager::SLIDER,
                'size_units' => [ 'px' ],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 50,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .sifency-progress-bar' => 'height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'bar_border_radius',
            [
                'label' => __( 'Border Radius', 'sifency-addons' ),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors' => [
                    '{{WRAPPER}} .sifency-progress-bar, {{WRAPPER}} .sifency-progress-bar__fill' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Heading 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_animation_controls();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        if ( empty($settings['progress_bars']) ) {
            return;
        }

        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = ['sifency-progress-wrapper'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <div <?php 
            
            echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php foreach ($settings['progress_bars'] as $index => $item) : ?>
                <?php
                $item_classes = ['sifency-progress-item'];
                
                $item_attrs = $animation_manager->get_item_attributes($item_classes);
                ?>
                <div <?php echo wp_kses_post ( $item_attrs ); ?>>
                    <div class="sifency-progress-header">
                        <span class="sifency-progress-title sifency-heading-style"><?php echo esc_html($item['title']); ?></span>
                        <?php if ( 'yes' === $settings['display_percentage'] ) : ?>
                            <span class="sifency-progress-percent sifency-number-style"><?php echo (int) $item['percent']['size']; ?>%</span>
                        <?php endif; ?>
                    </div>
                    <div class="sifency-progress-bar">
                        <div class="sifency-progress-bar__fill" data-percent="<?php echo (int) $item['percent']['size']; ?>"></div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        <?php
    }
}