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

class IDSC_Elementor_Counter extends \Elementor\Widget_Base {

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

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

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

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

    public function get_keywords() {
        return array( 'counter', 'number', 'stats' );
    }

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

        $this->add_control( 'number', array(
            'label'   => __( 'Target Number', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::NUMBER,
            'default' => 100,
        ) );

        $this->add_control( 'prefix', array(
            'label'       => __( 'Prefix', 'idsc' ),
            'type'        => \Elementor\Controls_Manager::TEXT,
            'description' => __( 'Text before the number (e.g. $).', 'idsc' ),
        ) );

        $this->add_control( 'suffix', array(
            'label'       => __( 'Suffix', 'idsc' ),
            'type'        => \Elementor\Controls_Manager::TEXT,
            'description' => __( 'Text after the number (e.g. + or %).', 'idsc' ),
        ) );

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

        $this->add_control( 'duration', array(
            'label'   => __( 'Duration (ms)', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::NUMBER,
            'default' => 2000,
            'min'     => 100,
        ) );

        $this->end_controls_section();
    }

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

        $atts  = ' number="' . esc_attr( $s['number'] ) . '"';
        $atts .= ! empty( $s['prefix'] ) ? ' prefix="' . esc_attr( $s['prefix'] ) . '"' : '';
        $atts .= ! empty( $s['suffix'] ) ? ' suffix="' . esc_attr( $s['suffix'] ) . '"' : '';
        $atts .= ! empty( $s['label'] ) ? ' label="' . esc_attr( $s['label'] ) . '"' : '';
        $atts .= ! empty( $s['duration'] ) ? ' duration="' . esc_attr( $s['duration'] ) . '"' : '';

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