<?php
/**
 * Theme Presets — FREE
 *
 * Each preset also defines an auto-calculated button_text_color
 * to keep contrast readable at all times.
 *
 * @package Panelra
 */

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

class Pnlrdb_Theme_Presets {

	/**
	 * Calculate the perceived brightness of a HEX color (0–255).
	 * Used to decide whether button text should be light or dark.
	 *
	 * @param  string $hex Hex color (e.g. #6366f1).
	 * @return int         Perceived brightness 0–255.
	 */
	public static function perceived_brightness( $hex ) {
		$hex = ltrim( $hex, '#' );
		if ( strlen( $hex ) === 3 ) {
			$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 ) );
		// Formula WCAG perceived brightness
		return ( $r * 299 + $g * 587 + $b * 114 ) / 1000;
	}

	/**
	 * Return the text color (#fff or #1e293b) that gives the best
	 * contrast against a given background color.
	 *
	 * @param  string $bg_hex Background hex color.
	 * @return string         #ffffff or #1e293b
	 */
	public static function contrast_text( $bg_hex ) {
		return self::perceived_brightness( $bg_hex ) > 128 ? '#1e293b' : '#ffffff';
	}

	public static function get_presets() {
		return array(

			'default' => array(
				'name'        => 'Default WordPress',
				'description' => 'Use native WordPress colors without Panelra color overrides.',
				'colors'      => array(),
				'font'        => '',
				'font_size'   => 14,
				'font_color'  => '#2c3338',
				'btn_color'   => Pnlrdb_Dashboard_Colors::best_text_color( '#2271b1' ),
				'menu_width'  => 160,
				'is_reset'    => true,
				'preview'     => array( '#1d2327', '#2271b1', '#f0f0f1' ),
				'features'    => array(
					'Colors: native WordPress admin scheme',
					'Links: native WordPress styling',
					'Buttons: native WordPress styling',
					'Font: system default',
					'Menu width: 160px',
				),
			),

			'midnight' => array(
				'name'        => 'Midnight',
				'description' => 'Deep black sidebar, bright indigo accent. Elegant and modern.',
				'colors'      => array( 'primary' => '#6366f1', 'sidebar_bg' => '#0f1117' ),
				'font'        => '"DM Sans", sans-serif',
				'font_size'   => 14,
				'font_color'  => '#1e293b',
				'btn_color'   => Pnlrdb_Dashboard_Colors::best_text_color( '#6366f1' ),
				'menu_width'  => 220,
				'is_reset'    => false,
				'preview'     => array( '#0f1117', '#6366f1', '#f8fafc' ),
				'features'    => array(
					'Sidebar: black #0f1117',
					'Accent: indigo #6366f1',
					'Button text: white #ffffff',
					'Font: DM Sans',
					'Menu width: 220px',
				),
			),

			'slate' => array(
				'name'        => 'Slate',
				'description' => 'Professional dark slate. Ideal for corporate environments.',
				'colors'      => array( 'primary' => '#334155', 'sidebar_bg' => '#1e293b' ),
				'font'        => '"Segoe UI", Arial, sans-serif',
				'font_size'   => 14,
				'font_color'  => '#0f172a',
				'btn_color'   => Pnlrdb_Dashboard_Colors::best_text_color( '#334155' ),
				'menu_width'  => 200,
				'is_reset'    => false,
				'preview'     => array( '#1e293b', '#334155', '#f1f5f9' ),
				'features'    => array(
					'Sidebar: slate #1e293b',
					'Accent: mid slate #334155',
					'Button text: white #ffffff',
					'Font: Segoe UI',
					'Menu width: 200px',
				),
			),

			'forest' => array(
				'name'        => 'Forest',
				'description' => 'Forest green with emerald accent. Fresh and calming.',
				'colors'      => array( 'primary' => '#059669', 'sidebar_bg' => '#0d1f1a' ),
				'font'        => 'Verdana, Geneva, sans-serif',
				'font_size'   => 13,
				'font_color'  => '#0f2419',
				'btn_color'   => Pnlrdb_Dashboard_Colors::best_text_color( '#059669' ),
				'menu_width'  => 220,
				'is_reset'    => false,
				'preview'     => array( '#0d1f1a', '#059669', '#ecfdf5' ),
				'features'    => array(
					'Sidebar: forest green #0d1f1a',
					'Accent: emerald #059669',
					'Button text: white #ffffff',
					'Font: Verdana',
					'Menu width: 220px',
				),
			),

			'rose' => array(
				'name'        => 'Rose',
				'description' => 'Dark rose sidebar, crimson accent. For creative studios.',
				'colors'      => array( 'primary' => '#e11d48', 'sidebar_bg' => '#1f0d14' ),
				'font'        => 'Georgia, serif',
				'font_size'   => 14,
				'font_color'  => '#1a0810',
				'btn_color'   => Pnlrdb_Dashboard_Colors::best_text_color( '#e11d48' ),
				'menu_width'  => 230,
				'is_reset'    => false,
				'preview'     => array( '#1f0d14', '#e11d48', '#fff1f2' ),
				'features'    => array(
					'Sidebar: dark rose #1f0d14',
					'Accent: crimson #e11d48',
					'Button text: white #ffffff',
					'Font: Georgia',
					'Menu width: 230px',
				),
			),
		);
	}

	public function __construct() {
		add_action( 'wp_ajax_pnlrdb_apply_theme_preset', array( $this, 'ajax_apply_preset' ) );
	}

	public function ajax_apply_preset() {
		check_ajax_referer( 'pnlrdb_nonce', 'nonce' );
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( 'Permission denied.' );
		}

		$preset_key = sanitize_key( $_POST['preset'] ?? '' );
		$presets    = self::get_presets();

		if ( ! isset( $presets[ $preset_key ] ) ) {
			wp_send_json_error( 'Invalid preset.' );
		}

		$p = $presets[ $preset_key ];

		if ( ! empty( $p['is_reset'] ) ) {
			update_option( 'pnlrdb_dashboard_colors', array() );
			delete_option( 'pnlrdb_button_text_color' );
		} else {
			update_option( 'pnlrdb_dashboard_colors', $p['colors'] );
			update_option( 'pnlrdb_button_text_color', $p['btn_color'] );
		}

		update_option( 'pnlrdb_active_theme_preset',  $preset_key );
		update_option( 'pnlrdb_dashboard_font',       $p['font'] );
		update_option( 'pnlrdb_dashboard_font_size',  $p['font_size'] );
		update_option( 'pnlrdb_dashboard_font_color', $p['font_color'] );
		update_option( 'pnlrdb_menu_width',           $p['menu_width'] );

		if ( isset( $p['dark_mode'] ) ) {
			update_user_meta(
				get_current_user_id(),
				Pnlrdb_Dark_Mode::USER_META_KEY,
				$p['dark_mode'] ? 'on' : 'off'
			);
		}

		wp_send_json_success( array(
			'preset' => $preset_key,
			'name'   => $p['name'],
		) );
	}
}
