<?php
/**
 * Hero / Split Hero widget.
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Hero
Description: Hero section with headline, text, background image, and CTA button.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

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

/**
 * Rsmspark_Content_Widgets_Hero_Widget
 */
class Rsmspark_Content_Widgets_Hero_Widget extends SiteOrigin_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'hero',
			__( 'RSM Hero', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Image background hero with headline and call to action.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-cover-image',
				'has_preview' => true,
				'keywords'    => array( 'hero', 'banner', 'cta', 'RSM' ),
			),
			array(),
			array(),
			plugin_dir_path( __FILE__ )
		);
	}

	/**
	 * @return array<string, mixed>
	 */
	public function get_widget_form() {
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Hero_Layers', false ) ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-hero-layers.php';
		}
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Focal_Point', false ) ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-focal-point.php';
		}
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Hero_Presets', false ) ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-hero-presets.php';
		}

		return array(
			'presets' => array(
				'type'   => 'section',
				'label'  => __( 'Presets', 'rsm-content-widgets' ),
				'fields' => array(
					'style_preset' => array(
						'type'        => 'select',
						'label'       => __( 'Layout preset', 'rsm-content-widgets' ),
						'default'     => '',
						'options'     => Rsmspark_Content_Widgets_Hero_Presets::select_options(),
						'description' => __( 'Choose a bundled layout, then click Apply preset. Your background image is kept when the preset omits one.', 'rsm-content-widgets' )
							. '<p class="rsmspark-cw-hero-preset-actions"><button type="button" class="button rsmspark-cw-hero-apply-preset">'
							. esc_html__( 'Apply preset', 'rsm-content-widgets' )
							. '</button></p>',
					),
					'export_json'  => array(
						'type'        => 'textarea',
						'label'       => __( 'Export JSON', 'rsm-content-widgets' ),
						'rows'        => 6,
						'description' => __( 'Generate JSON from the current hero settings, then copy or save the text below.', 'rsm-content-widgets' )
							. '<p class="rsmspark-cw-hero-preset-actions"><button type="button" class="button rsmspark-cw-hero-export-json">'
							. esc_html__( 'Export JSON', 'rsm-content-widgets' )
							. '</button></p>',
					),
					'import_json'  => array(
						'type'        => 'textarea',
						'label'       => __( 'Import JSON', 'rsm-content-widgets' ),
						'rows'        => 6,
						'description' => __( 'Paste an RSM Hero export, then click Apply import.', 'rsm-content-widgets' )
							. '<p class="rsmspark-cw-hero-preset-actions"><button type="button" class="button button-primary rsmspark-cw-hero-import-json">'
							. esc_html__( 'Apply import', 'rsm-content-widgets' )
							. '</button></p>',
					),
				),
			),
			'content' => array(
				'type'   => 'section',
				'label'  => __( 'Content', 'rsm-content-widgets' ),
				'fields' => array(
					'headline' => array(
						'type'    => 'text',
						'label'   => __( 'Headline', 'rsm-content-widgets' ),
						'default' => __( 'Welcome to your site', 'rsm-content-widgets' ),
					),
					'subheadline' => array(
						'type'    => 'textarea',
						'label'   => __( 'Subheadline', 'rsm-content-widgets' ),
						'default' => __( 'Add a short supporting message and a call to action.', 'rsm-content-widgets' ),
					),
					'background_image' => array(
						'type'  => 'media',
						'label' => __( 'Background image', 'rsm-content-widgets' ),
					),
					'cta_text' => array(
						'type'  => 'text',
						'label' => __( 'Button text', 'rsm-content-widgets' ),
					),
					'cta_url' => array(
						'type'  => 'link',
						'label' => __( 'Button URL', 'rsm-content-widgets' ),
					),
					'layout' => array(
						'type'    => 'select',
						'label'   => __( 'Layout', 'rsm-content-widgets' ),
						'default' => 'centered',
						'options' => array(
							'centered' => __( 'Centered overlay', 'rsm-content-widgets' ),
							'split'    => __( 'Split (text + image)', 'rsm-content-widgets' ),
						),
					),
					'split_ratio' => array(
						'type'        => 'select',
						'label'       => __( 'Split column ratio', 'rsm-content-widgets' ),
						'description' => __( 'Applies when Layout is Split.', 'rsm-content-widgets' ),
						'default'     => '50-50',
						'options'     => array(
							'50-50' => __( '50 / 50', 'rsm-content-widgets' ),
							'40-60' => __( '40 / 60 (wider image)', 'rsm-content-widgets' ),
							'60-40' => __( '60 / 40 (wider text)', 'rsm-content-widgets' ),
						),
					),
					'image_side' => array(
						'type'    => 'select',
						'label'   => __( 'Image side', 'rsm-content-widgets' ),
						'default' => 'right',
						'options' => array(
							'left'  => __( 'Image left', 'rsm-content-widgets' ),
							'right' => __( 'Image right', 'rsm-content-widgets' ),
						),
					),
					'vertical_align' => array(
						'type'    => 'select',
						'label'   => __( 'Content vertical align', 'rsm-content-widgets' ),
						'default' => 'center',
						'options' => array(
							'top'    => __( 'Top', 'rsm-content-widgets' ),
							'center' => __( 'Center', 'rsm-content-widgets' ),
							'bottom' => __( 'Bottom', 'rsm-content-widgets' ),
						),
					),
					'mobile_order' => array(
						'type'    => 'select',
						'label'   => __( 'Mobile stack order', 'rsm-content-widgets' ),
						'default' => 'content-first',
						'options' => array(
							'content-first' => __( 'Content first', 'rsm-content-widgets' ),
							'image-first'   => __( 'Image first', 'rsm-content-widgets' ),
						),
					),
					'focal_preset' => array(
						'type'    => 'select',
						'label'   => __( 'Image 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,
					),
				),
			),
			'appearance' => array(
				'type'   => 'section',
				'label'  => __( 'Appearance', 'rsm-content-widgets' ),
				'fields' => array(
					'overlay_opacity' => array(
						'type'    => 'slider',
						'label'   => __( 'Overlay opacity', 'rsm-content-widgets' ),
						'default' => 45,
						'min'     => 0,
						'max'     => 100,
					),
					'min_height' => array(
						'type'    => 'number',
						'label'   => __( 'Minimum height (px)', 'rsm-content-widgets' ),
						'default' => 320,
					),
					'background_layers' => array(
						'type'        => 'repeater',
						'label'       => __( 'Background layers', 'rsm-content-widgets' ),
						'item_name'   => __( 'Layer', 'rsm-content-widgets' ),
						'description' => __( 'Stack background images for centered layout. Bottom row renders first.', 'rsm-content-widgets' ),
						'item_label'  => array(
							'selector'     => "[id*='layer_type']",
							'update_event' => 'change',
							'value_method' => 'val',
						),
						'fields'      => array(
							'type' => array(
								'type'    => 'select',
								'label'   => __( 'Layer type', 'rsm-content-widgets' ),
								'default' => 'image',
								'options' => array(
									'image' => __( 'Image', 'rsm-content-widgets' ),
								),
							),
							'image' => array(
								'type'    => 'media',
								'label'   => __( 'Layer image', 'rsm-content-widgets' ),
								'library' => 'image',
							),
							'opacity' => array(
								'type'    => 'slider',
								'label'   => __( 'Layer opacity', 'rsm-content-widgets' ),
								'min'     => 0,
								'max'     => 100,
								'integer' => true,
								'default' => 100,
							),
							'blend_mode' => array(
								'type'    => 'select',
								'label'   => __( 'Blend mode', 'rsm-content-widgets' ),
								'default' => 'normal',
								'options' => Rsmspark_Content_Widgets_Hero_Layers::blend_mode_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,
							),
						),
					),
				),
			),
		);
	}

	/**
	 * @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 'hero.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 );
		$content    = Rsmspark_Content_Widgets_Loader::section_instance( $instance, 'content' );
		$appearance = Rsmspark_Content_Widgets_Loader::section_instance( $instance, 'appearance' );

		$merged = array_merge(
			$content,
			$appearance,
			array(
				'cta_url' => Rsmspark_Content_Widgets_Render::link_url( $content['cta_url'] ?? '' ),
			)
		);

		$vars = Rsmspark_Content_Widgets_Widget_Hero::prepare_from_instance( $merged );
		$vars['style_vars'] = Rsmspark_Content_Widgets_Loader::inline_style_vars();
		return $vars;
	}

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

		if ( ! class_exists( 'Rsmspark_Content_Widgets_Hero_Presets', false ) ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/class-rsmspark-content-widgets-hero-presets.php';
		}

		if ( class_exists( 'Rsmspark_Content_Widgets_Assets', false ) ) {
			Rsmspark_Content_Widgets_Assets::enqueue_export_json_ui_assets();
		}

		wp_enqueue_style(
			'rsmspark-cw-admin-widgets',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/admin-widgets.css',
			array( 'rsmspark-cw-export-json-ui' ),
			RSMSPARK_CONTENT_WIDGETS_VERSION
		);

		wp_enqueue_script(
			'rsmspark-cw-hero-admin',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-cw-hero-admin.js',
			array( 'jquery', 'rsmspark-cw-export-json-ui' ),
			RSMSPARK_CONTENT_WIDGETS_VERSION,
			true
		);

		wp_localize_script(
			'rsmspark-cw-hero-admin',
			'rsmsparkCwHeroAdmin',
			array(
				'presets'      => Rsmspark_Content_Widgets_Hero_Presets::choices_for_editor(),
				'exportPlugin' => Rsmspark_Content_Widgets_Hero_Presets::EXPORT_PLUGIN,
				'version'      => RSMSPARK_CONTENT_WIDGETS_VERSION,
				'i18n'         => array(
					'applyPreset'  => __( 'Apply preset', 'rsm-content-widgets' ),
					'exportJson'   => __( 'Export JSON', 'rsm-content-widgets' ),
					'importApply'  => __( 'Apply import', 'rsm-content-widgets' ),
					'confirm'      => __( 'Apply this hero preset? Layout and appearance values will be overwritten.', 'rsm-content-widgets' ),
					'importError'  => __( 'Paste valid RSM Hero JSON export data.', 'rsm-content-widgets' ),
					'exportLabel'  => __( 'Generated export (also copied to your clipboard):', 'rsm-content-widgets' ),
					'exportPanelTitle' => __( 'JSON export ready', 'rsm-content-widgets' ),
					'copied'       => __( 'Hero JSON copied to clipboard and filled in Export JSON.', 'rsm-content-widgets' ),
					'exportReady'  => __( 'Export generated below the Export JSON button. It was also copied to your clipboard.', 'rsm-content-widgets' ),
					'selectPreset' => __( 'Choose a layout preset first.', 'rsm-content-widgets' ),
					'layersManual' => __( 'This preset includes background layers. Configure them under Appearance → Background layers after applying.', 'rsm-content-widgets' ),
				),
			)
		);
	}

	/**
	 * @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( 'hero', __FILE__, 'Rsmspark_Content_Widgets_Hero_Widget' );
}
