<?php
/**
 * RSM Image widget — controllable media image for Page Builder layouts.
 *
 * SiteOrigin requires widget id, folder name, and PHP basename to match (id "rsm-image").
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Image
Description: Place a media image with size, alignment, corner radius, and optional border controls.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

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

/**
 * Rsmspark_Content_Widgets_Image_Widget
 */
class Rsmspark_Content_Widgets_Image_Widget extends SiteOrigin_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'rsm-image',
			__( 'RSM Image', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Flexible image placement with size, alignment, corners, and border options.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-format-image',
				'has_preview' => true,
				'keywords'    => array( 'image', 'photo', 'picture', 'media', '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_Image', false ) ) {
			Rsmspark_Content_Widgets_Widget_Image::register_so_shell_hooks();
		}
	}

	/**
	 * @return array<string, mixed>
	 */
	public function get_widget_form() {
		return Rsmspark_Content_Widgets_SiteOrigin::prepend_import_export_form(
			'image',
			array(
			'content' => array(
				'type'   => 'section',
				'label'  => __( 'Image', 'rsm-content-widgets' ),
				'fields' => array(
					'image' => array(
						'type'    => 'media',
						'label'   => __( 'Image', 'rsm-content-widgets' ),
						'choose'  => __( 'Choose image', 'rsm-content-widgets' ),
						'update'  => __( 'Set image', 'rsm-content-widgets' ),
						'library' => 'image',
						'default' => '',
					),
					'alt_text' => array(
						'type'        => 'text',
						'label'       => __( 'Alt text', 'rsm-content-widgets' ),
						'description' => __( 'Describe the image for accessibility. Leave empty to use the media library alt text.', 'rsm-content-widgets' ),
						'default'     => '',
					),
					'link' => array(
						'type'        => 'link',
						'label'       => __( 'Optional link', 'rsm-content-widgets' ),
						'description' => __( 'Wrap the image in a link (optional). When set, lightbox is disabled.', 'rsm-content-widgets' ),
						'default'     => '',
					),
					'lightbox_enable' => array(
						'type'        => 'checkbox',
						'label'       => __( 'Open in lightbox', 'rsm-content-widgets' ),
						'description' => __( 'Click the image to view the full-size version in an overlay. Ignored when a link URL is set.', 'rsm-content-widgets' ),
						'default'     => false,
					),
					'lightbox_caption' => array(
						'type'        => 'text',
						'label'       => __( 'Lightbox caption', 'rsm-content-widgets' ),
						'description' => __( 'Optional caption below the enlarged image.', 'rsm-content-widgets' ),
						'default'     => '',
					),
				),
			),
			'layout' => array(
				'type'   => 'section',
				'label'  => __( 'Layout', 'rsm-content-widgets' ),
				'fields' => array(
					'image_width' => array(
						'type'        => 'slider',
						'label'       => __( 'Image width', 'rsm-content-widgets' ),
						'description' => __( 'Width of the image relative to the available column space.', 'rsm-content-widgets' ),
						'min'         => 10,
						'max'         => 100,
						'integer'     => true,
						'default'     => 100,
					),
					'max_width' => array(
						'type'        => 'slider',
						'label'       => __( 'Max width (px)', 'rsm-content-widgets' ),
						'description' => __( '0 = no pixel cap (uses width % only).', 'rsm-content-widgets' ),
						'min'         => 0,
						'max'         => 1600,
						'integer'     => true,
						'default'     => 0,
					),
					'min_frame_height' => array(
						'type'        => 'slider',
						'label'       => __( 'Frame min-height (px)', 'rsm-content-widgets' ),
						'description' => __( 'Minimum height of the placement area for vertical alignment. 0 = shrink to the image.', 'rsm-content-widgets' ),
						'min'         => 0,
						'max'         => 800,
						'integer'     => true,
						'default'     => 0,
					),
					'align_horizontal' => array(
						'type'    => 'select',
						'label'   => __( 'Horizontal position', 'rsm-content-widgets' ),
						'default' => 'center',
						'options' => Rsmspark_Content_Widgets_Image_Styles::get_horizontal_align_options(),
					),
					'align_vertical' => array(
						'type'    => 'select',
						'label'   => __( 'Vertical position', 'rsm-content-widgets' ),
						'default' => 'center',
						'options' => Rsmspark_Content_Widgets_Image_Styles::get_vertical_align_options(),
					),
					'focal_preset' => array(
						'type'    => 'select',
						'label'   => __( 'Focal point', 'rsm-content-widgets' ),
						'default' => 'center',
						'options' => Rsmspark_Content_Widgets_Focal_Point::preset_options(),
					),
					'focal_x' => array(
						'type'    => 'slider',
						'label'   => __( 'Focal X (%)', 'rsm-content-widgets' ),
						'default' => 50,
						'min'     => 0,
						'max'     => 100,
						'integer' => true,
					),
					'focal_y' => array(
						'type'    => 'slider',
						'label'   => __( 'Focal Y (%)', 'rsm-content-widgets' ),
						'default' => 50,
						'min'     => 0,
						'max'     => 100,
						'integer' => true,
					),
					'aspect_ratio' => array(
						'type'    => 'select',
						'label'   => __( 'Aspect ratio crop', 'rsm-content-widgets' ),
						'default' => 'auto',
						'options' => Rsmspark_Content_Widgets_Focal_Point::aspect_ratio_options(),
					),
				),
			),
			'design' => array(
				'type'   => 'section',
				'label'  => __( 'Design', 'rsm-content-widgets' ),
				'fields' => array(
					'corners' => array(
						'type'    => 'select',
						'label'   => __( 'Corners', 'rsm-content-widgets' ),
						'default' => 'rounded',
						'options' => Rsmspark_Content_Widgets_Image_Styles::get_corner_options(),
					),
					'corner_radius' => array(
						'type'        => 'slider',
						'label'       => __( 'Corner radius', 'rsm-content-widgets' ),
						'description' => __( 'Applies when Corners is set to Rounded.', 'rsm-content-widgets' ),
						'min'         => 0,
						'max'         => 64,
						'integer'     => true,
						'default'     => 12,
					),
					'border_enable' => array(
						'type'    => 'checkbox',
						'label'   => __( 'Show border', 'rsm-content-widgets' ),
						'default' => false,
					),
					'border_width' => array(
						'type'    => 'slider',
						'label'   => __( 'Border thickness', 'rsm-content-widgets' ),
						'min'     => 1,
						'max'     => 12,
						'integer' => true,
						'default' => 1,
					),
					'border_color' => array(
						'type'    => 'color',
						'label'   => __( 'Border color', 'rsm-content-widgets' ),
						'default' => '#e2e8f0',
					),
					'border_opacity' => array(
						'type'        => 'slider',
						'label'       => __( 'Border opacity', 'rsm-content-widgets' ),
						'description' => __( '0 = transparent, 100 = solid.', 'rsm-content-widgets' ),
						'min'         => 0,
						'max'         => 100,
						'integer'     => true,
						'default'     => 100,
					),
				),
			),
		)
		);
	}

	/**
	 * @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 'rsm-image.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 );
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Widget_Image', false ) ) {
			return array();
		}
		return Rsmspark_Content_Widgets_Widget_Image::prepare_template_vars(
			Rsmspark_Content_Widgets_Widget_Image::prepare_from_instance( $instance )
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_admin_scripts() {
		if ( class_exists( 'Rsmspark_Content_Widgets_Widget_Image', false ) ) {
			Rsmspark_Content_Widgets_Widget_Image::mark_so_rendered( array(), $this );
		}
		if ( class_exists( 'Rsmspark_Content_Widgets_Media_Picker', false ) ) {
			Rsmspark_Content_Widgets_Media_Picker::enqueue_admin( 'post.php' );
		}
		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( 'rsm-image', __FILE__, 'Rsmspark_Content_Widgets_Image_Widget' );
}
