<?php
/**
 * Impresión de rótulos para marcar rollos.
 *
 * @package Logistica
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Configuración y utilidades de color para rótulos de rollos.
 */
class Logistica_Rotulo_Rollos_Print {

	public const OPTION_WIDTH_MM  = 'logistica_rotulo_rollos_width_mm';
	public const OPTION_HEIGHT_MM = 'logistica_rotulo_rollos_height_mm';

	/**
	 * Paleta de colores distintos por tamaño.
	 *
	 * @return string[]
	 */
	public static function color_palette() {
		return array(
			'#7CB342',
			'#29B6F6',
			'#FF7043',
			'#AB47BC',
			'#FFA726',
			'#26A69A',
			'#EC407A',
			'#5C6BC0',
			'#8D6E63',
			'#78909C',
			'#66BB6A',
			'#42A5F5',
		);
	}

	/**
	 * Color determinístico según el tamaño.
	 *
	 * @param string $tamano Tamaño (ej. 68 CM).
	 * @return string Hex color.
	 */
	public static function color_for_tamano( $tamano ) {
		$key = strtolower( trim( (string) $tamano ) );
		if ( '' === $key ) {
			return '#64748b';
		}

		$palette = self::color_palette();
		$index   = abs( crc32( $key ) ) % count( $palette );

		return $palette[ $index ];
	}

	/**
	 * @return array<string, int>
	 */
	public static function defaults() {
		// Media carta horizontal (mitad de Carta): 2 rótulos apilados por hoja.
		return array(
			self::OPTION_WIDTH_MM  => 216,
			self::OPTION_HEIGHT_MM => 140,
		);
	}

	/**
	 * @return array<string, int>
	 */
	public static function get_settings() {
		$stored   = get_option( 'logistica_rotulo_rollos_print_settings', array() );
		$defaults = self::defaults();

		return array(
			self::OPTION_WIDTH_MM  => max( 60, min( 250, (int) ( $stored[ self::OPTION_WIDTH_MM ] ?? $defaults[ self::OPTION_WIDTH_MM ] ) ) ),
			self::OPTION_HEIGHT_MM => max( 40, min( 200, (int) ( $stored[ self::OPTION_HEIGHT_MM ] ?? $defaults[ self::OPTION_HEIGHT_MM ] ) ) ),
		);
	}

	/**
	 * @return array<string, mixed>
	 */
	public static function client_config() {
		$settings = self::get_settings();

		return array(
			'widthMm'      => (int) $settings[ self::OPTION_WIDTH_MM ],
			'heightMm'     => (int) $settings[ self::OPTION_HEIGHT_MM ],
			'cssUrl'       => LOGISTICA_PLUGIN_URL . 'assets/css/rotulo-rollos-print.css',
			'colorPalette' => self::color_palette(),
		);
	}

	/**
	 * @param array<string, mixed> $raw POST settings.
	 * @return void
	 */
	public static function save_settings( $raw ) {
		if ( ! is_array( $raw ) ) {
			$raw = array();
		}

		$defaults = self::defaults();
		$width    = (int) ( $raw[ self::OPTION_WIDTH_MM ] ?? $defaults[ self::OPTION_WIDTH_MM ] );
		$height   = (int) ( $raw[ self::OPTION_HEIGHT_MM ] ?? $defaults[ self::OPTION_HEIGHT_MM ] );

		update_option(
			'logistica_rotulo_rollos_print_settings',
			array(
				self::OPTION_WIDTH_MM  => max( 60, min( 250, $width ) ),
				self::OPTION_HEIGHT_MM => max( 40, min( 200, $height ) ),
			),
			false
		);
	}

	/**
	 * Registra CSS/JS de rótulos de rollos.
	 *
	 * @return void
	 */
	public static function register_assets() {
		wp_enqueue_style(
			'logistica-rotulo-rollos-print',
			LOGISTICA_PLUGIN_URL . 'assets/css/rotulo-rollos-print.css',
			array( 'logistica-admin' ),
			logistica_asset_version( 'assets/css/rotulo-rollos-print.css' )
		);

		wp_enqueue_style(
			'logistica-rotulo-estibas-print',
			LOGISTICA_PLUGIN_URL . 'assets/css/rotulo-estibas-print.css',
			array( 'logistica-admin' ),
			logistica_asset_version( 'assets/css/rotulo-estibas-print.css' )
		);

		wp_enqueue_script(
			'logistica-rotulo-rollos-print',
			LOGISTICA_PLUGIN_URL . 'assets/js/rotulo-rollos-print.js',
			array(),
			logistica_asset_version( 'assets/js/rotulo-rollos-print.js' ),
			true
		);

		wp_enqueue_script(
			'logistica-rotulo-estibas-print',
			LOGISTICA_PLUGIN_URL . 'assets/js/rotulo-estibas-print.js',
			array( 'logistica-rotulo-rollos-print' ),
			logistica_asset_version( 'assets/js/rotulo-estibas-print.js' ),
			true
		);
	}
}
