<?php
/**
 * Color controls service for exposing available color control types.
 *
 * @package RewardsWP
 */

namespace Awesomemotive\Rewardswp\Services;

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

use function Awesomemotive\Rewardswp\Helpers\can_access_rewardswp_feature;


/**
 * Color Controls Service.
 *
 * @since 1.0.0
 */
class ColorControlsService {

	/**
	 * Get available color control types based on user's plan.
	 *
	 * @since 1.0.0
	 *
	 * @return array Array of available control types.
	 */
	public function get_available_controls(): array {
		$controls = [];

		// Basic color controls - available to all plans.
		if ( can_access_rewardswp_feature( 'basic_color_controls' ) ) {
			$controls[] = 'basic_color_picker';
			$controls[] = 'preset_colors';
		}

		// Gradient controls - available to plus and pro plans.
		if ( can_access_rewardswp_feature( 'gradient_controls' ) ) {
			$controls[] = 'gradient_picker';
			$controls[] = 'gradient_presets';
		}

		// Advanced color tools - available to pro plan only.
		if ( can_access_rewardswp_feature( 'advanced_color_tools' ) ) {
			$controls[] = 'color_palette_generator';
			$controls[] = 'accessibility_checker';
			$controls[] = 'brand_color_sync';
		}

		return $controls;
	}

	/**
	 * Render color controls based on user's plan.
	 *
	 * @since 1.0.0
	 *
	 * @param array $current_settings Current color settings.
	 * @return string HTML output for color controls.
	 */
	public function render_controls( array $current_settings = [] ): string {
		$available_controls = $this->get_available_controls();
		$output             = '';

		// Always show basic controls if available.
		if ( in_array( 'basic_color_picker', $available_controls, true ) ) {
			$output .= $this->render_basic_color_picker( $current_settings );
		}

		if ( in_array( 'preset_colors', $available_controls, true ) ) {
			$output .= $this->render_preset_colors( $current_settings );
		}

		// Plus plan features.
		if ( in_array( 'gradient_picker', $available_controls, true ) ) {
			$output .= $this->render_gradient_picker( $current_settings );
		} else {
			$output .= $this->render_upgrade_prompt( 'plus', 'gradient controls' );
		}

		// Pro plan features.
		if ( in_array( 'color_palette_generator', $available_controls, true ) ) {
			$output .= $this->render_advanced_tools( $current_settings );
		} else {
			$output .= $this->render_upgrade_prompt( 'pro', 'advanced color tools' );
		}

		return $output;
	}

	/**
	 * Render basic color picker control.
	 *
	 * @since 1.0.0
	 *
	 * @param array $settings Current settings.
	 * @return string HTML output.
	 */
	private function render_basic_color_picker( array $settings ): string {
		$current_color = $settings['primary_color'] ?? '#0073aa';

		return sprintf(
			'<div class="color-control basic-color-picker">
				<label for="primary_color">Primary Color</label>
				<input type="color" id="primary_color" name="primary_color" value="%s" />
			</div>',
			esc_attr( $current_color )
		);
	}

	/**
	 * Render preset colors section.
	 *
	 * @since 1.0.0
	 *
	 * @param array $settings Current settings.
	 * @return string HTML output.
	 */
	private function render_preset_colors( array $settings ): string {
		$presets = [
			'#0073aa' => 'WordPress Blue',
			'#00a32a' => 'Success Green',
			'#d63638' => 'Error Red',
			'#f56e28' => 'Orange',
		];

		$output  = '<div class="color-control preset-colors">';
		$output .= '<h4>Color Presets</h4>';

		foreach ( $presets as $color => $name ) {
			$output .= sprintf(
				'<button type="button" class="preset-color" data-color="%s" style="background-color: %s;" title="%s"></button>',
				esc_attr( $color ),
				esc_attr( $color ),
				esc_attr( $name )
			);
		}

		$output .= '</div>';

		return $output;
	}

	/**
	 * Render gradient picker control (Plus plan feature).
	 *
	 * @since 1.0.0
	 *
	 * @param array $settings Current settings.
	 * @return string HTML output.
	 */
	private function render_gradient_picker( array $settings ): string {
		$gradient = $settings['gradient'] ?? 'linear-gradient(45deg, #0073aa, #005177)';

		return sprintf(
			'<div class="color-control gradient-picker">
				<label for="gradient">Background Gradient</label>
				<div class="gradient-preview" style="background: %s;"></div>
				<input type="text" id="gradient" name="gradient" value="%s" placeholder="Enter CSS gradient" />
				<p class="description">Available in Plus and Pro plans</p>
			</div>',
			esc_attr( $gradient ),
			esc_attr( $gradient )
		);
	}

	/**
	 * Render advanced color tools (Pro plan feature).
	 *
	 * @since 1.0.0
	 *
	 * @param array $settings Current settings.
	 * @return string HTML output.
	 */
	private function render_advanced_tools( array $settings ): string {
		return '
		<div class="color-control advanced-tools">
			<h4>Advanced Color Tools</h4>
			<div class="tool-grid">
				<div class="tool">
					<h5>Palette Generator</h5>
					<button type="button" class="button" id="generate-palette">Generate Palette</button>
					<p class="description">Auto-generate complementary colors</p>
				</div>
				<div class="tool">
					<h5>Accessibility Checker</h5>
					<button type="button" class="button" id="check-contrast">Check Contrast</button>
					<p class="description">WCAG compliance checking</p>
				</div>
				<div class="tool">
					<h5>Brand Color Sync</h5>
					<button type="button" class="button" id="sync-brand">Sync Brand Colors</button>
					<p class="description">Import from brand guidelines</p>
				</div>
			</div>
			<p class="description"><strong>Pro feature:</strong> Advanced color management tools</p>
		</div>';
	}

	/**
	 * Render upgrade prompt for locked features.
	 *
	 * @since 1.0.0
	 *
	 * @param string $required_plan The plan required to access the feature.
	 * @param string $feature_name The name of the feature.
	 * @return string HTML output.
	 */
	private function render_upgrade_prompt( string $required_plan, string $feature_name ): string {
		$plan_name = ucfirst( $required_plan );

		return sprintf(
			'<div class="color-control upgrade-prompt">
				<div class="upgrade-content">
					<h4>%s</h4>
					<p>Upgrade to %s plan to unlock %s and more advanced features.</p>
					<a href="%s" class="button button-primary">Upgrade to %s</a>
				</div>
			</div>',
			esc_html( ucfirst( $feature_name ) ),
			esc_html( $plan_name ),
			esc_html( $feature_name ),
			esc_url( admin_url( 'admin.php?page=rewardswp-settings&tab=license' ) ),
			esc_html( $plan_name )
		);
	}

	/**
	 * Validate and save color settings based on user's plan.
	 *
	 * @since 1.0.0
	 *
	 * @param array $submitted_settings The settings submitted by the user.
	 * @return array Validated settings with restricted features removed if necessary.
	 */
	public function validate_and_save_settings( array $submitted_settings ): array {
		$validated_settings = [];
		$available_controls = $this->get_available_controls();

		// Always allow basic color settings.
		if ( in_array( 'basic_color_picker', $available_controls, true ) ) {
			if ( isset( $submitted_settings['primary_color'] ) ) {
				$validated_settings['primary_color'] = sanitize_hex_color( $submitted_settings['primary_color'] );
			}
		}

		// Only save gradient if user has access.
		if ( in_array( 'gradient_picker', $available_controls, true ) ) {
			if ( isset( $submitted_settings['gradient'] ) ) {
				$validated_settings['gradient'] = sanitize_text_field( $submitted_settings['gradient'] );
			}
		}

		// Only save advanced settings if user has pro access.
		if ( in_array( 'color_palette_generator', $available_controls, true ) ) {
			if ( isset( $submitted_settings['brand_colors'] ) ) {
				$validated_settings['brand_colors'] = array_map( 'sanitize_hex_color', $submitted_settings['brand_colors'] );
			}
		}

		return $validated_settings;
	}
}
