<?php
/**
 * RSM Headline widget — overline, headline, and subheadline typography block.
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Headline
Description: Overline (eyebrow), headline, and subheadline with per-line fonts, colors, sizes, alignment, and container styling.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

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

/**
 * Rsmspark_Content_Widgets_Headline_Widget
 */
class Rsmspark_Content_Widgets_Headline_Widget extends SiteOrigin_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'headline',
			__( 'RSM Headline', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Styled headline stack with overline, headline, and subheadline typography controls.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-heading',
				'has_preview' => true,
				'keywords'    => array( 'headline', 'title', 'heading', 'eyebrow', 'overline', '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(
			'headline',
			array(
			'content' => array(
				'type'   => 'section',
				'label'  => __( 'Text', 'rsm-content-widgets' ),
				'fields' => array(
					'overline' => array_merge(
						Rsmspark_Rich_Text_Registry::siteorigin_tinymce_field( Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE, 4 ),
						array(
							'label'          => __( 'Overline (eyebrow)', 'rsm-content-widgets' ),
							'description'    => __( 'Use Text size and Color in the toolbar. Line typography below sets defaults for unstyled text.', 'rsm-content-widgets' ),
							'default'        => '',
							'rsm_rt_profile' => Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE,
						)
					),
					'headline' => array_merge(
						Rsmspark_Rich_Text_Registry::siteorigin_tinymce_field( Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE, 6 ),
						array(
							'label'          => __( 'Headline', 'rsm-content-widgets' ),
							'description'    => __( 'Select words and apply Display, Hero, or Jumbo size plus Accent or Gradient. Line typography below sets defaults.', 'rsm-content-widgets' ),
							'default'        => __( 'Your headline here', 'rsm-content-widgets' ),
							'rsm_rt_profile' => Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE,
						)
					),
					'subheadline' => array_merge(
						Rsmspark_Rich_Text_Registry::siteorigin_tinymce_field( Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE, 4 ),
						array(
							'label'          => __( 'Subheadline', 'rsm-content-widgets' ),
							'description'    => __( 'Use Lead or Body size for supporting copy.', 'rsm-content-widgets' ),
							'default'        => '',
							'rsm_rt_profile' => Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE,
						)
					),
					'headline_tag' => array(
						'type'    => 'select',
						'label'   => __( 'Headline HTML tag', 'rsm-content-widgets' ),
						'default' => 'h2',
						'options' => Rsmspark_Content_Widgets_Headline_Styles::get_headline_tag_options(),
					),
				),
			),
			'overline_typography'    => Rsmspark_Content_Widgets_Headline_Styles::siteorigin_typography_section(
				'overline',
				__( 'Overline typography', 'rsm-content-widgets' )
			),
			'headline_typography'    => Rsmspark_Content_Widgets_Headline_Styles::siteorigin_typography_section(
				'headline',
				__( 'Headline typography', 'rsm-content-widgets' )
			),
			'subheadline_typography' => Rsmspark_Content_Widgets_Headline_Styles::siteorigin_typography_section(
				'subheadline',
				__( 'Subheadline typography', 'rsm-content-widgets' )
			),
			'container' => array(
				'type'   => 'section',
				'label'  => __( 'Container', 'rsm-content-widgets' ),
				'fields' => array(
					'width_pct' => array(
						'type'        => 'slider',
						'label'       => __( 'Width', 'rsm-content-widgets' ),
						'description' => __( 'Width relative to the column.', '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.', 'rsm-content-widgets' ),
						'min'         => 0,
						'max'         => 1600,
						'integer'     => true,
						'default'     => 0,
					),
					'background_color' => array(
						'type'        => 'color',
						'label'       => __( 'Background color', 'rsm-content-widgets' ),
						'description' => __( 'Leave empty for transparent (page background).', 'rsm-content-widgets' ),
						'default'     => '',
					),
					'corners' => array(
						'type'    => 'select',
						'label'   => __( 'Corners', 'rsm-content-widgets' ),
						'default' => 'square',
						'options' => Rsmspark_Content_Widgets_Headline_Styles::get_corner_options(),
					),
					'corner_radius' => array(
						'type'        => 'slider',
						'label'       => __( 'Corner radius', 'rsm-content-widgets' ),
						'description' => __( 'Applies when Corners is 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 'headline.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_Headline', false ) ) {
			return array();
		}
		return Rsmspark_Content_Widgets_Widget_Headline::prepare_template_vars(
			Rsmspark_Content_Widgets_Widget_Headline::prepare_from_instance( $instance )
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return void
	 */
	public function enqueue_admin_scripts() {
		if ( class_exists( 'Rsmspark_Rich_Text_Assets', false ) ) {
			Rsmspark_Rich_Text_Assets::enqueue_admin();
		}

		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return void
	 */
	public function enqueue_frontend_scripts( $instance ) {
		unset( $instance );
		if ( class_exists( 'Rsmspark_Rich_Text_Assets', false ) ) {
			Rsmspark_Rich_Text_Assets::enqueue_siteorigin_frontend();
		}
		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}
}

if ( function_exists( 'siteorigin_widget_register' ) ) {
	siteorigin_widget_register( 'headline', __FILE__, 'Rsmspark_Content_Widgets_Headline_Widget' );
}
