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

class IDSC_Elementor_Progress extends \Elementor\Widget_Base {

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

    public function get_title() {
        return __( 'Progress Bar', 'idsc' );
    }

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

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

    public function get_keywords() {
        return array( 'progress', 'bar', 'percentage', 'skill' );
    }

    protected function register_controls() {
        $this->start_controls_section( 'section_content', array(
            'label' => __( 'Progress Bar', 'idsc' ),
        ) );

        $this->add_control( 'value', array(
            'label'   => __( 'Value (%)', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SLIDER,
            'range'   => array( 'px' => array( 'min' => 0, 'max' => 100 ) ),
            'default' => array( 'size' => 75 ),
        ) );

        $this->add_control( 'label', array(
            'label' => __( 'Label', 'idsc' ),
            'type'  => \Elementor\Controls_Manager::TEXT,
        ) );

        $this->add_control( 'color', array(
            'label'   => __( 'Bar Color', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::COLOR,
            'default' => '#3b82f6',
        ) );

        $this->add_control( 'bg_color', array(
            'label'   => __( 'Background Color', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::COLOR,
            'default' => '#e5e7eb',
        ) );

        $this->add_control( 'height', array(
            'label'   => __( 'Height (px)', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SLIDER,
            'range'   => array( 'px' => array( 'min' => 4, 'max' => 60 ) ),
            'default' => array( 'size' => 20 ),
        ) );

        $this->add_control( 'show_pct', array(
            'label'        => __( 'Show Percentage', 'idsc' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'label_on'     => __( 'Yes', 'idsc' ),
            'label_off'    => __( 'No', 'idsc' ),
            'return_value' => 'yes',
            'default'      => 'yes',
        ) );

        $this->add_control( 'animated', array(
            'label'        => __( 'Animated', 'idsc' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'label_on'     => __( 'Yes', 'idsc' ),
            'label_off'    => __( 'No', 'idsc' ),
            'return_value' => 'yes',
            'default'      => '',
        ) );

        $this->end_controls_section();
    }

    protected function render() {
        $s = $this->get_settings_for_display();

        $atts  = ' value="' . esc_attr( $s['value']['size'] ?? 75 ) . '"';
        $atts .= ! empty( $s['label'] ) ? ' label="' . esc_attr( $s['label'] ) . '"' : '';
        $atts .= ! empty( $s['color'] ) ? ' color="' . esc_attr( $s['color'] ) . '"' : '';
        $atts .= ! empty( $s['bg_color'] ) ? ' bg_color="' . esc_attr( $s['bg_color'] ) . '"' : '';
        $atts .= ' height="' . esc_attr( $s['height']['size'] ?? 20 ) . '"';
        $atts .= $s['show_pct'] !== 'yes' ? ' show_pct="no"' : '';
        $atts .= $s['animated'] === 'yes' ? ' animated="yes"' : '';

        echo do_shortcode( '[idsc_progress' . $atts . ']' );
    }
}
