<?php
/**
 * Helper Class.
 *
 * @package SmartWait_Animated_Preloader
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class SWAP_Helpers.
 */
class SWAP_Helpers {

	/**
	 * Free template slugs available without a Pro license.
	 *
	 * @var array
	 */
	const FREE_TEMPLATES = array( 'tpl-1', 'tpl-2', 'tpl-3', 'tpl-8', 'tpl-9', 'tpl-20' );

	/**
	 * Free text animation keys available without a Pro license.
	 *
	 * @var array
	 */
	const FREE_TEXT_ANIMATIONS = array( 'none', 'fade', 'bounce' );

	/**
	 * Check if the current site has an active Pro license.
	 *
	 * Returns false by default. After Freemius SDK integration,
	 * update this method to check the actual plan status.
	 *
	 * @return bool
	 */
	public static function is_pro() {
		return function_exists( 'swap_fs' ) && swap_fs()->is_paying();
	}

	/**
	 * Get default settings.
	 *
	 * @return array
	 */
	public static function get_default_settings() {
		return array(
			// 1. Display Control
			'active_preloader_type' => 'none', // none, style, logo, text, progress_timing.
			'display_rule'          => 'whole_site', // whole_site, homepage, specific_pages.
			'excluded_pages'        => '', // Comma-separated IDs.
			'user_exclusion'        => 'no',
			'device_targeting'      => array( 'all' ),
			'show_delay'            => 0, // ms.
			'hide_delay'            => 500, // ms.
			'excluded_url_patterns' => '',
			'excluded_post_types'   => array(),

			// 2. Animation Types
			'spinner_type'      => 'tpl-1',
			'loader_color'      => '#ffffff',
			'style_size'        => 50, // px.
			'style_wait_anim'   => 'no', // Wait for loop.
			'spinner_anim_speed' => 1000, // ms.
			'template_category'  => 'minimal', // minimal, creative, particle, glitch, logo_reveal.
			'premium_template'  => '',

			// 3. Background
			'bg_choice'         => 'custom', // simple (black/white), custom.
			'bg_color_type'     => 'black', // simple mode: black, white.
			'bg_opacity'        => '100',
			// Custom mode:
			'bg_type'           => 'solid', // solid, gradient, image, video.
			'bg_custom_color'   => '#000000',
			'bg_gradient'       => 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
			'bg_image_url'      => '',
			'bg_image_size'     => 'cover',
			'bg_image_position'  => 'center center',
			'bg_video_url'      => '',
			'bg_blur'           => 0, // px.

			// 4. Branding
			'logo_url'              => '',
			'logo_animation'        => 'none', // fade, slide, zoom, spin, pulse.
			'logo_anim_speed'       => 1000, // ms.
			'logo_wait_anim'        => 'no', // Wait for loop.
			'logo_width'            => 150,
			'logo_height'           => 0, // Auto if 0.
			'logo_spacing'          => 20,

			// 5. Text & Typography
			'show_loading_text'    => 'no', // Kept for default merge, but UI removed.
			'loading_text'         => 'Loading...',
			'text_animation'       => 'none',
			'text_anim_speed'      => 2000, // ms.
			'text_wait_anim'       => 'no', // Wait for loop.
			'font_family'          => 'Arial, sans-serif',
			'custom_font_url'   => '',
			'font_size'         => 16, // px.
			'font_weight'       => 'normal',
			'text_color'        => '#000000',

			// Readymade Text Styles
			'use_normal_text'       => 'no',
			'use_readymade_text'    => 'no',
			'readymade_text_style'  => 'l1',

			// 6. Progress
			'show_percentage'    => 'no',
			'show_progress_bar'  => 'no',
			'progress_bar_type'    => 'linear', // linear, circular, top-bar.
			'progress_bar_color'   => '#ffffff',
			'progress_bar_height'  => 4, // px.
			'percentage_color'     => '#ffffff',
			'percentage_font_size' => 24, // px.
			'estimated_time'       => 2000, // ms (fake estimation base).

			// 7. Animation & Timing
			'fade_out_speed'    => 500, // ms.
			'min_display_time'  => 1000, // ms.
			'transition_effect' => 'fade', // fade, slide-up, split.

			// 8. Admin & Developer
			'enable_live_preview' => 'yes',
			'custom_css'          => '',
			'script_loading'      => 'footer', // header, footer.
		);
	}

	/**
	 * Get settings with defaults.
	 *
	 * @return array
	 */
	public static function get_settings() {
		$defaults = self::get_default_settings();
		$settings = get_option( 'swap_settings', $defaults );
		return wp_parse_args( $settings, $defaults );
	}

	/**
	 * Get HTML for a specific spinner type.
	 *
	 * @param string $type Spinner type.
	 * @return string HTML.
	 */
	public static function get_spinner_html( $type ) {
		// All new CSS-only templates use a single div with a unique class.
		$valid_templates = array();
		for ( $i = 1; $i <= 55; $i++ ) {
			$valid_templates[] = 'tpl-' . $i;
		}

		if ( in_array( $type, $valid_templates, true ) ) {
			// Templates that don't support custom colors due to hardcoded palettes.
			$no_color_templates = array( 
				'tpl-14', 'tpl-15', 'tpl-17', 'tpl-24', 'tpl-25', 'tpl-26', 'tpl-27', 'tpl-28', 
				'tpl-29', 'tpl-30', 'tpl-31', 'tpl-32', 'tpl-40', 'tpl-41', 'tpl-46', 'tpl-47', 
				'tpl-48', 'tpl-49', 'tpl-50', 'tpl-51', 'tpl-52', 'tpl-53', 'tpl-54', 'tpl-55' 
			);
			$wrapper_class = in_array( $type, $no_color_templates, true ) ? 'swap-tpl-wrap no-color' : 'swap-tpl-wrap';
			
			return '<div class="' . esc_attr( $wrapper_class ) . '"><div class="swap-' . esc_attr( $type ) . '"></div></div>';
		}

		// Fallback to first template.
		return '<div class="swap-tpl-wrap"><div class="swap-tpl-1"></div></div>';
	}
}
