<?php
/**
 * Feature / Icon Box Grid widget.
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Icon Box Grid
Description: Grid of icon, title, and description cards.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

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

/**
 * Rsmspark_Content_Widgets_Icon_Box_Grid_Widget
 */
class Rsmspark_Content_Widgets_Icon_Box_Grid_Widget extends SiteOrigin_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'icon-box-grid',
			__( 'RSM Icon Box Grid', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Simple grid of icon, title, and description cards.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-grid-view',
				'has_preview' => true,
				'keywords'    => array( 'icon', 'grid', 'features', '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(
			'icon_box_grid',
			array(
			'content' => array(
				'type'   => 'section',
				'label'  => __( 'Grid', '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'     => __( 'Boxes', 'rsm-content-widgets' ),
						'item_name' => __( 'Box', 'rsm-content-widgets' ),
						'fields'    => array(
							'icon' => array(
								'type'    => 'icon',
								'label'   => __( 'Icon', 'rsm-content-widgets' ),
								'default' => 'dashicons dashicons-yes-alt',
							),
							'title' => array(
								'type'    => 'text',
								'label'   => __( 'Title', 'rsm-content-widgets' ),
								'default' => __( 'Feature title', 'rsm-content-widgets' ),
							),
							'description' => array(
								'type'    => 'textarea',
								'label'   => __( 'Description', 'rsm-content-widgets' ),
								'default' => __( 'Short description for this feature.', 'rsm-content-widgets' ),
							),
						),
						'default'   => array(
							array(
								'icon'        => 'dashicons dashicons-yes-alt',
								'title'       => __( 'Easy to use', 'rsm-content-widgets' ),
								'description' => __( 'Drop widgets into any SiteOrigin layout.', 'rsm-content-widgets' ),
							),
							array(
								'icon'        => 'dashicons dashicons-admin-customizer',
								'title'       => __( 'Customizable', 'rsm-content-widgets' ),
								'description' => __( 'Tune colors and spacing from plugin settings.', 'rsm-content-widgets' ),
							),
							array(
								'icon'        => 'dashicons dashicons-performance',
								'title'       => __( 'Lightweight', 'rsm-content-widgets' ),
								'description' => __( 'Assets load only when widgets are on the page.', '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 'icon-box-grid.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_Icon_Box_Grid::prepare_template_vars(
			Rsmspark_Content_Widgets_Widget_Icon_Box_Grid::prepare_from_instance(
				Rsmspark_Content_Widgets_Loader::section_instance( $instance, 'content' )
			)
		);
	}

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

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

if ( function_exists( 'siteorigin_widget_register' ) ) {
	siteorigin_widget_register( 'icon-box-grid', __FILE__, 'Rsmspark_Content_Widgets_Icon_Box_Grid_Widget' );
}
