<?php
/**
 * Logo grid SiteOrigin widget.
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Logo Grid
Description: Grid of client or partner logos with optional links.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

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

/**
 * Rsmspark_Content_Widgets_Logo_Grid_Widget
 */
class Rsmspark_Content_Widgets_Logo_Grid_Widget extends SiteOrigin_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'logo-grid',
			__( 'RSM Logo Grid', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Display client or partner logos in a responsive grid.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-images-alt2',
				'has_preview' => true,
				'keywords'    => array( 'logo', 'clients', 'partners', 'RSM' ),
			),
			array(),
			array(),
			plugin_dir_path( __FILE__ )
		);
	}

	/**
	 * SiteOrigin wrapper chrome reset (default SO widget border).
	 */
	public function initialize() {
		parent::initialize();
		if ( class_exists( 'Rsmspark_Content_Widgets_Widget_Logo_Grid', false ) ) {
			Rsmspark_Content_Widgets_Widget_Logo_Grid::register_so_shell_hooks();
		}
	}

	/**
	 * @return array<string, mixed>
	 */
	public function get_widget_form() {
		return Rsmspark_Content_Widgets_SiteOrigin::prepend_import_export_form(
			'logo_grid',
			array(
			'content' => array(
				'type'   => 'section',
				'label'  => __( 'Logos', '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' => '4',
						'options' => array(
							'2' => __( '2 columns', 'rsm-content-widgets' ),
							'3' => __( '3 columns', 'rsm-content-widgets' ),
							'4' => __( '4 columns', 'rsm-content-widgets' ),
							'5' => __( '5 columns', 'rsm-content-widgets' ),
							'6' => __( '6 columns', 'rsm-content-widgets' ),
						),
					),
					'items' => array(
						'type'      => 'repeater',
						'label'     => __( 'Logos', 'rsm-content-widgets' ),
						'item_name' => __( 'Logo', 'rsm-content-widgets' ),
						'fields'    => array(
							'image' => array(
								'type'        => 'media',
								'label'       => __( 'Logo image', 'rsm-content-widgets' ),
								'description' => __( 'PNG or SVG recommended. Shown centered in the cell.', 'rsm-content-widgets' ),
							),
							'name' => array(
								'type'        => 'text',
								'label'       => __( 'Name', 'rsm-content-widgets' ),
								'description' => __( 'Used for hover overlay, text fallback when no image, and default alt text.', 'rsm-content-widgets' ),
							),
							'url' => array(
								'type'        => 'link',
								'label'       => __( 'Link (optional)', 'rsm-content-widgets' ),
								'description' => __( 'Makes the whole cell clickable.', 'rsm-content-widgets' ),
							),
							'alt' => array(
								'type'        => 'text',
								'label'       => __( 'Alt text', 'rsm-content-widgets' ),
								'description' => __( 'Image accessibility label. Defaults to Name when empty.', 'rsm-content-widgets' ),
							),
						),
						'default'   => array(
							array(
								'name' => __( 'Client One', 'rsm-content-widgets' ),
							),
							array(
								'name' => __( 'Client Two', 'rsm-content-widgets' ),
							),
							array(
								'name' => __( 'Client Three', 'rsm-content-widgets' ),
							),
							array(
								'name' => __( 'Client Four', 'rsm-content-widgets' ),
							),
						),
					),
				),
			),
			'appearance' => array(
				'type'   => 'section',
				'label'  => __( 'Headline & cells', 'rsm-content-widgets' ),
				'fields' => array(
					'title_color' => array(
						'type'  => 'color',
						'label' => __( 'Headline color', 'rsm-content-widgets' ),
					),
					'title_size' => array(
						'type'        => 'number',
						'label'       => __( 'Headline size (px, 0 = default)', 'rsm-content-widgets' ),
						'default'     => 0,
						'min'         => 0,
						'max'         => 72,
					),
					'border_enable' => array(
						'type'    => 'checkbox',
						'label'   => __( 'Cell borders', 'rsm-content-widgets' ),
						'default' => false,
					),
					'border_width' => array(
						'type'    => 'number',
						'label'   => __( 'Border width (px)', 'rsm-content-widgets' ),
						'default' => 1,
						'min'     => 1,
						'max'     => 8,
					),
					'border_color' => array(
						'type'    => 'color',
						'label'   => __( 'Border color', 'rsm-content-widgets' ),
						'default' => '#e2e8f0',
					),
					'border_radius' => array(
						'type'    => 'select',
						'label'   => __( 'Cell corners', 'rsm-content-widgets' ),
						'default' => 'square',
						'options' => array(
							'square'  => __( 'Square', 'rsm-content-widgets' ),
							'rounded' => __( 'Rounded', 'rsm-content-widgets' ),
						),
					),
					'container_border' => array(
						'type'    => 'checkbox',
						'label'   => __( 'Widget border', 'rsm-content-widgets' ),
						'default' => false,
						'description' => __( 'Optional border around the entire grid. Off by default.', 'rsm-content-widgets' ),
					),
					'container_border_width' => array(
						'type'    => 'number',
						'label'   => __( 'Widget border width (px)', 'rsm-content-widgets' ),
						'default' => 1,
						'min'     => 1,
						'max'     => 8,
					),
					'container_border_color' => array(
						'type'    => 'color',
						'label'   => __( 'Widget border color', 'rsm-content-widgets' ),
						'default' => '#e2e8f0',
					),
					'container_border_radius' => array(
						'type'    => 'select',
						'label'   => __( 'Widget corners', 'rsm-content-widgets' ),
						'default' => 'square',
						'options' => array(
							'square'  => __( 'Square', 'rsm-content-widgets' ),
							'rounded' => __( 'Rounded', '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 'logo-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_Logo_Grid::prepare_template_vars(
			Rsmspark_Content_Widgets_Widget_Logo_Grid::prepare_from_siteorigin_instance(
				is_array( $instance ) ? $instance : array()
			)
		);
	}

	/**
	 * @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( 'logo-grid', __FILE__, 'Rsmspark_Content_Widgets_Logo_Grid_Widget' );
}
