<?php
/**
 * RSM Image widget design tokens.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * CSS variables and helpers for the Image widget.
 */
final class Rsmspark_Content_Widgets_Image_Styles {

	/**
	 * @return array<string, string>
	 */
	public static function get_horizontal_align_options() {
		return array(
			'left'   => __( 'Left', 'rsm-content-widgets' ),
			'center' => __( 'Center', 'rsm-content-widgets' ),
			'right'  => __( 'Right', 'rsm-content-widgets' ),
		);
	}

	/**
	 * @return array<string, string>
	 */
	public static function get_vertical_align_options() {
		return array(
			'top'    => __( 'Top', 'rsm-content-widgets' ),
			'center' => __( 'Center', 'rsm-content-widgets' ),
			'bottom' => __( 'Bottom', 'rsm-content-widgets' ),
		);
	}

	/**
	 * @return array<string, string>
	 */
	public static function get_corner_options() {
		return array(
			'square'  => __( 'Square corners', 'rsm-content-widgets' ),
			'rounded' => __( 'Rounded corners', 'rsm-content-widgets' ),
		);
	}

	/**
	 * @param string $hex     Hex color.
	 * @param int    $opacity Opacity 0–100.
	 * @return string
	 */
	public static function hex_to_rgba( $hex, $opacity ) {
		$hex = sanitize_hex_color( (string) $hex );
		if ( ! $hex ) {
			$hex = '#e2e8f0';
		}
		$opacity = max( 0, min( 100, (int) $opacity ) ) / 100;
		$hex     = ltrim( $hex, '#' );
		if ( 3 === strlen( $hex ) ) {
			$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
		}
		$r = hexdec( substr( $hex, 0, 2 ) );
		$g = hexdec( substr( $hex, 2, 2 ) );
		$b = hexdec( substr( $hex, 4, 2 ) );
		return 'rgba(' . $r . ', ' . $g . ', ' . $b . ', ' . round( $opacity, 2 ) . ')';
	}

	/**
	 * @param mixed $value Raw checkbox.
	 * @return bool
	 */
	public static function is_enabled( $value ) {
		if ( is_bool( $value ) ) {
			return $value;
		}
		if ( is_string( $value ) ) {
			return in_array( strtolower( $value ), array( '1', 'true', 'yes', 'on' ), true );
		}
		return ! empty( $value );
	}

	/**
	 * @param array<string, mixed> $content Content section.
	 * @param array<string, mixed> $layout  Layout section.
	 * @param array<string, mixed> $design  Design section.
	 * @return array<string, string>
	 */
	public static function build_css_variables( array $content, array $layout, array $design ) {
		$width_pct = max( 10, min( 100, (int) ( $layout['image_width'] ?? 100 ) ) );
		$max_width = max( 0, min( 1600, (int) ( $layout['max_width'] ?? 0 ) ) );
		$min_h     = max( 0, min( 800, (int) ( $layout['min_frame_height'] ?? 0 ) ) );

		$corners = sanitize_key( (string) ( $design['corners'] ?? 'rounded' ) );
		if ( ! in_array( $corners, array( 'square', 'rounded' ), true ) ) {
			$corners = 'rounded';
		}

		$radius = 0;
		if ( 'rounded' === $corners ) {
			$radius = max( 0, min( 64, (int) ( $design['corner_radius'] ?? 12 ) ) );
		}

		$vars = array(
			'--ri-width'      => $width_pct . '%',
			'--ri-max-width'  => $max_width > 0 ? $max_width . 'px' : 'none',
			'--ri-min-height' => $min_h > 0 ? $min_h . 'px' : '0',
			'--ri-radius'     => $radius . 'px',
		);

		$focal_preset = Rsmspark_Content_Widgets_Focal_Point::sanitize_preset( $layout['focal_preset'] ?? 'center' );
		$focal_x      = Rsmspark_Content_Widgets_Focal_Point::sanitize_coord( $layout['focal_x'] ?? 50 );
		$focal_y      = Rsmspark_Content_Widgets_Focal_Point::sanitize_coord( $layout['focal_y'] ?? 50 );
		$aspect_ratio = Rsmspark_Content_Widgets_Focal_Point::sanitize_aspect_ratio( $layout['aspect_ratio'] ?? 'auto' );
		$aspect_css   = Rsmspark_Content_Widgets_Focal_Point::aspect_ratio_css( $aspect_ratio );

		$vars['--ri-object-position'] = Rsmspark_Content_Widgets_Focal_Point::resolve_position( $focal_preset, $focal_x, $focal_y );
		if ( '' !== $aspect_css ) {
			$vars['--ri-aspect-ratio'] = $aspect_css;
		}

		if ( self::is_enabled( $design['border_enable'] ?? false ) ) {
			$vars['--ri-border-width'] = max( 1, min( 12, (int) ( $design['border_width'] ?? 1 ) ) ) . 'px';
			$vars['--ri-border-color'] = self::hex_to_rgba(
				$design['border_color'] ?? '#e2e8f0',
				$design['border_opacity'] ?? 100
			);
		}

		return $vars;
	}

	/**
	 * @param array<string, string> $vars CSS variables.
	 * @return string
	 */
	public static function vars_to_inline_style( array $vars ) {
		$pairs = array();
		foreach ( $vars as $key => $value ) {
			$pairs[] = $key . ':' . $value;
		}
		return implode( ';', $pairs );
	}
}
