<?php
/**
 * Stats counter SiteOrigin widget.
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Stats Counter
Description: Key metrics with labels.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'SiteOrigin_Widget', false ) ) {
	return;
}

/**
 * Rsmspark_Content_Widgets_Stats_Counter_Widget
 */
class Rsmspark_Content_Widgets_Stats_Counter_Widget extends SiteOrigin_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'stats-counter',
			__( 'RSM Stats Counter', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Highlight key metrics with labels.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-chart-bar',
				'has_preview' => true,
				'keywords'    => array( 'stats', 'counter', 'metrics', 'RSM' ),
			),
			array(),
			array(),
			plugin_dir_path( __FILE__ )
		);
	}

	/**
	 * @return array<string, mixed>
	 */
	public function get_widget_form() {
		return Rsmspark_Content_Widgets_SiteOrigin::prepend_import_export_form(
			'stats_counter',
			array(
			'content' => array(
				'type'   => 'section',
				'label'  => __( 'Stats', 'rsm-content-widgets' ),
				'fields' => array(
					'title' => array(
						'type'  => 'text',
						'label' => __( 'Section title', 'rsm-content-widgets' ),
					),
					'columns' => array(
						'type'    => 'select',
						'label'   => __( 'Columns', 'rsm-content-widgets' ),
						'default' => '3',
						'options' => array(
							'2' => __( '2 columns', 'rsm-content-widgets' ),
							'3' => __( '3 columns', 'rsm-content-widgets' ),
							'4' => __( '4 columns', 'rsm-content-widgets' ),
						),
					),
					'items' => array(
						'type'      => 'repeater',
						'label'     => __( 'Metrics', 'rsm-content-widgets' ),
						'item_name' => __( 'Metric', 'rsm-content-widgets' ),
						'fields'    => array(
							'value' => array(
								'type'        => 'text',
								'label'       => __( 'Value', 'rsm-content-widgets' ),
								'default'     => '100',
								'description' => __( 'Main number, or percent (0–100) for pie/donut charts.', 'rsm-content-widgets' ),
							),
							'display' => array(
								'type'    => 'select',
								'label'   => __( 'Display as', 'rsm-content-widgets' ),
								'default' => 'number',
								'options' => array(
									'number' => __( 'Number', 'rsm-content-widgets' ),
									'pie'    => __( 'Pie chart', 'rsm-content-widgets' ),
									'donut'  => __( 'Donut chart', 'rsm-content-widgets' ),
								),
							),
							'value_color' => array(
								'type'        => 'color',
								'label'       => __( 'Number color', 'rsm-content-widgets' ),
								'description' => __( 'Optional. Defaults to the plugin primary color.', 'rsm-content-widgets' ),
							),
							'prefix' => array(
								'type'        => 'text',
								'label'       => __( 'Prefix', 'rsm-content-widgets' ),
								'description' => __( 'Text before the number (e.g. $ or #).', 'rsm-content-widgets' ),
							),
							'prefix_color' => array(
								'type'  => 'color',
								'label' => __( 'Prefix color', 'rsm-content-widgets' ),
							),
							'suffix' => array(
								'type'        => 'text',
								'label'       => __( 'Suffix', 'rsm-content-widgets' ),
								'description' => __( 'Text after the number (e.g. % or +). Not animated.', 'rsm-content-widgets' ),
							),
							'suffix_color' => array(
								'type'  => 'color',
								'label' => __( 'Suffix color', 'rsm-content-widgets' ),
							),
							'label' => array(
								'type'        => 'text',
								'label'       => __( 'Label', 'rsm-content-widgets' ),
								'description' => __( 'Caption below the value (e.g. Projects delivered).', 'rsm-content-widgets' ),
							),
						),
					),
				),
			),
		)
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_template_dir( $instance ) {
		unset( $instance );
		return '';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_style_name( $instance ) {
		unset( $instance );
		return '';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_template_name( $instance ) {
		unset( $instance );
		return 'stats-counter.tpl';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @param array<string, mixed> $args     Args.
	 * @return array<string, mixed>
	 */
	public function get_template_variables( $instance, $args ) {
		unset( $args );
		return Rsmspark_Content_Widgets_Widget_Stats_Counter::prepare_template_vars(
			Rsmspark_Content_Widgets_Widget_Stats_Counter::prepare_from_instance(
				Rsmspark_Content_Widgets_Loader::section_instance( $instance, 'content' )
			)
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_admin_scripts() {
		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_frontend_scripts( $instance ) {
		unset( $instance );
		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}
}

if ( function_exists( 'siteorigin_widget_register' ) ) {
	siteorigin_widget_register( 'stats-counter', __FILE__, 'Rsmspark_Content_Widgets_Stats_Counter_Widget' );
}
