<?php
/**
 * Gradient colors – presets, customizer, and CSS output.
 *
 * @package EasySite_Pro
 */

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

/**
 * Curated gradient presets.
 *
 * @return array
 */
function easysite_get_gradient_presets() {
	return apply_filters( 'easysite_gradient_presets', array(
		'brand' => array(
			'label'  => __( 'Brand colors', 'easysite-pro' ),
			'from'   => 'main',
			'to'     => 'accent',
			'angle'  => 135,
		),
		'ocean' => array(
			'label'  => __( 'Ocean Blue', 'easysite-pro' ),
			'from'   => '#0ea5e9',
			'to'     => '#2563eb',
			'angle'  => 135,
		),
		'sunset' => array(
			'label'  => __( 'Sunset', 'easysite-pro' ),
			'from'   => '#f97316',
			'to'     => '#ec4899',
			'angle'  => 135,
		),
		'forest' => array(
			'label'  => __( 'Forest', 'easysite-pro' ),
			'from'   => '#059669',
			'to'     => '#14b8a6',
			'angle'  => 135,
		),
		'aurora' => array(
			'label'  => __( 'Aurora', 'easysite-pro' ),
			'from'   => '#7c3aed',
			'to'     => '#06b6d4',
			'angle'  => 135,
		),
		'midnight' => array(
			'label'  => __( 'Midnight', 'easysite-pro' ),
			'from'   => '#1e3a5f',
			'to'     => '#0f172a',
			'angle'  => 160,
		),
		'rose' => array(
			'label'  => __( 'Rose', 'easysite-pro' ),
			'from'   => '#e11d48',
			'to'     => '#fb7185',
			'angle'  => 135,
		),
		'gold' => array(
			'label'  => __( 'Gold', 'easysite-pro' ),
			'from'   => '#b8860b',
			'to'     => '#fbbf24',
			'angle'  => 135,
		),
		'fire' => array(
			'label'  => __( 'Fire', 'easysite-pro' ),
			'from'   => '#dc2626',
			'to'     => '#f59e0b',
			'angle'  => 135,
		),
		'lavender' => array(
			'label'  => __( 'Lavender', 'easysite-pro' ),
			'from'   => '#8b5cf6',
			'to'     => '#ec4899',
			'angle'  => 120,
		),
		'custom' => array(
			'label'  => __( 'Custom gradient', 'easysite-pro' ),
			'from'   => 'custom1',
			'to'     => 'custom2',
			'angle'  => 135,
		),
	) );
}

/**
 * Gradient direction choices.
 *
 * @return array
 */
function easysite_get_gradient_directions() {
	return array(
		'135'    => __( 'Diagonal ↘', 'easysite-pro' ),
		'90'     => __( 'Horizontal →', 'easysite-pro' ),
		'180'    => __( 'Vertical ↓', 'easysite-pro' ),
		'radial' => __( 'Radial (center)', 'easysite-pro' ),
	);
}

/**
 * Resolve gradient colors for a preset key.
 *
 * @param string $key Preset key.
 * @return array { from, to, angle, css }
 */
function easysite_resolve_gradient( $key = '' ) {
	if ( ! $key ) {
		$key = easysite_get_mod( 'easysite_gradient_preset', 'brand' );
	}

	$presets = easysite_get_gradient_presets();
	if ( ! isset( $presets[ $key ] ) ) {
		$key = 'brand';
	}

	$preset = $presets[ $key ];
	$main   = easysite_get_mod( 'easysite_main_color', '#2563eb' );
	$accent = easysite_get_mod( 'easysite_accent_color', '#0ea5e9' );

	$from = $preset['from'];
	$to   = $preset['to'];

	if ( 'main' === $from ) {
		$from = $main;
	}
	if ( 'accent' === $to ) {
		$to = $accent;
	}
	if ( 'custom1' === $from ) {
		$from = easysite_get_mod( 'easysite_gradient_color_1', $main );
	}
	if ( 'custom2' === $to ) {
		$to = easysite_get_mod( 'easysite_gradient_color_2', $accent );
	}

	$angle = easysite_get_mod( 'easysite_gradient_direction', (string) $preset['angle'] );
	if ( 'custom' !== $key && isset( $preset['angle'] ) ) {
		$mod_angle = easysite_get_mod( 'easysite_gradient_direction', '' );
		$angle     = $mod_angle ? $mod_angle : (string) $preset['angle'];
	}

	$css = easysite_build_gradient_css( $from, $to, $angle );

	return array(
		'from'  => $from,
		'to'    => $to,
		'angle' => $angle,
		'css'   => $css,
		'key'   => $key,
	);
}

/**
 * Build gradient CSS value.
 *
 * @param string $from  Start color.
 * @param string $to    End color.
 * @param string $angle Direction key or degrees.
 * @return string
 */
function easysite_build_gradient_css( $from, $to, $angle = '135' ) {
	$from = easysite_sanitize_hex_color( $from ) ? easysite_sanitize_hex_color( $from ) : '#2563eb';
	$to   = easysite_sanitize_hex_color( $to ) ? easysite_sanitize_hex_color( $to ) : '#0ea5e9';

	if ( 'radial' === $angle ) {
		return sprintf( 'radial-gradient(circle at center, %s 0%%, %s 100%%)', $from, $to );
	}

	$deg = absint( $angle );
	return sprintf( 'linear-gradient(%1$sdeg, %2$s 0%%, %3$s 100%%)', $deg, $from, $to );
}

/**
 * Gradient options for page builder selects.
 *
 * @return array
 */
function easysite_gradient_select_options() {
	$options = array( 'none' => __( 'No gradient', 'easysite-pro' ) );
	foreach ( easysite_get_gradient_presets() as $key => $preset ) {
		if ( 'custom' === $key ) {
			continue;
		}
		$options[ $key ] = $preset['label'];
	}
	$options['custom'] = __( 'Custom (from Customizer)', 'easysite-pro' );
	return $options;
}

/**
 * Inline style attribute for a section gradient background.
 *
 * @param string $key Preset key.
 * @return string
 */
function easysite_gradient_bg_style( $key ) {
	if ( ! $key || 'none' === $key ) {
		return '';
	}
	$resolved = easysite_resolve_gradient( $key );
	return 'background:' . $resolved['css'] . ';';
}

/**
 * CSS class for gradient preset.
 *
 * @param string $key Preset key.
 * @return string
 */
function easysite_gradient_class( $key ) {
	if ( ! $key || 'none' === $key ) {
		return '';
	}
	return ' easysite-gradient-bg easysite-gradient-' . sanitize_html_class( $key );
}

/**
 * Inline CSS custom properties for a section-specific gradient.
 *
 * @param string $key Preset key.
 * @return string
 */
function easysite_gradient_vars_style( $key ) {
	if ( ! $key || 'none' === $key ) {
		return '';
	}
	$resolved = easysite_resolve_gradient( $key );
	return sprintf(
		'--easysite-section-gradient:%1$s;--easysite-section-gradient-from:%2$s;--easysite-section-gradient-to:%3$s;',
		$resolved['css'],
		esc_attr( $resolved['from'] ),
		esc_attr( $resolved['to'] )
	);
}

/**
 * Render gradient swatch previews for admin UI.
 *
 * @param string $selected Selected preset key.
 */
function easysite_render_gradient_swatches( $selected = '' ) {
	if ( ! $selected ) {
		$selected = easysite_get_mod( 'easysite_gradient_preset', 'brand' );
	}
	echo '<div class="easysite-gradient-swatches" role="list">';
	foreach ( easysite_get_gradient_presets() as $key => $preset ) {
		if ( 'custom' === $key ) {
			continue;
		}
		$resolved = easysite_resolve_gradient( $key );
		$active   = $selected === $key ? ' is-active' : '';
		printf(
			'<span class="easysite-gradient-swatch%1$s" role="listitem" title="%2$s" style="background:%3$s" data-gradient="%4$s"></span>',
			esc_attr( $active ),
			esc_attr( $preset['label'] ),
			esc_attr( $resolved['css'] ),
			esc_attr( $key )
		);
	}
	echo '</div>';
}

/**
 * Apply gradient colors when design preset is chosen.
 *
 * @param string $preset Design preset key.
 */
function easysite_apply_preset_gradients( $preset ) {
	$map = array(
		'modern'    => 'ocean',
		'minimal'   => 'midnight',
		'bold'      => 'fire',
		'elegant'   => 'midnight',
		'luxury'    => 'gold',
		'startup'   => 'aurora',
		'corporate' => 'brand',
	);
	if ( isset( $map[ $preset ] ) ) {
		set_theme_mod( 'easysite_gradient_preset', $map[ $preset ] );
		set_theme_mod( 'easysite_use_gradients', true );
	}
}

/**
 * Customizer: gradient swatch previews.
 */
function easysite_customizer_gradient_assets() {
	wp_enqueue_style(
		'easysite-pro-admin',
		EASYSITE_PRO_URI . '/assets/css/admin.css',
		array(),
		EASYSITE_PRO_VERSION
	);
}
add_action( 'customize_controls_enqueue_scripts', 'easysite_customizer_gradient_assets' );

/**
 * Inject gradient swatches into customizer control.
 */
function easysite_customizer_gradient_swatches_script() {
	ob_start();
	easysite_render_gradient_swatches();
	$swatches_html = ob_get_clean();
	?>
	<script>
	(function ($) {
		$(function () {
			var $control = $('#customize-control-easysite_gradient_preset');
			if (!$control.length) {
				return;
			}
			var $wrap = $('<div class="easysite-customizer-gradients"></div>').html(<?php echo wp_json_encode( $swatches_html ); ?>);
			$control.find('select').after($wrap);

			$wrap.on('click', '.easysite-gradient-swatch', function () {
				var val = $(this).data('gradient');
				wp.customize('easysite_gradient_preset').set(val);
			});

			wp.customize('easysite_gradient_preset', function (setting) {
				setting.bind(function (val) {
					$wrap.find('.easysite-gradient-swatch').removeClass('is-active');
					$wrap.find('[data-gradient="' + val + '"]').addClass('is-active');
				});
			});
		});
	})(jQuery);
	</script>
	<?php
}
add_action( 'customize_controls_print_footer_scripts', 'easysite_customizer_gradient_swatches_script', 20 );
