<?php
/**
 * Shois Chat Button — Animation Engine (PHP)
 *
 * Registers animation metadata for the admin picker,
 * enqueues animation CSS files, and builds the animation config
 * for the frontend.
 *
 * @package ShoisChatButton
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class SHCB_Animation_Engine
 *
 * Server-side animation management.
 */
class SHCB_Animation_Engine {

	/**
	 * Get all available entrance animations.
	 *
	 * @return array List of animation definitions.
	 */
	public static function get_entrance_animations() {
		return array(
			array(
				'id'    => 'shcb-enter-fade',
				'label' => __( 'Fade In', 'shois-chat-button' ),
				'group' => 'entrance',
			),
			array(
				'id'    => 'shcb-enter-slideup',
				'label' => __( 'Slide Up', 'shois-chat-button' ),
				'group' => 'entrance',
			),
			array(
				'id'    => 'shcb-enter-slidedown',
				'label' => __( 'Slide Down', 'shois-chat-button' ),
				'group' => 'entrance',
			),
			array(
				'id'    => 'shcb-enter-slideright',
				'label' => __( 'Slide Right', 'shois-chat-button' ),
				'group' => 'entrance',
			),
			array(
				'id'    => 'shcb-enter-zoom',
				'label' => __( 'Zoom In', 'shois-chat-button' ),
				'group' => 'entrance',
			),
			array(
				'id'    => 'shcb-enter-bounce',
				'label' => __( 'Bounce In', 'shois-chat-button' ),
				'group' => 'entrance',
			),
			array(
				'id'    => 'shcb-enter-flip',
				'label' => __( 'Flip In', 'shois-chat-button' ),
				'group' => 'entrance',
			),
			array(
				'id'    => 'shcb-enter-roll',
				'label' => __( 'Roll In', 'shois-chat-button' ),
				'group' => 'entrance',
			),
			array(
				'id'    => 'shcb-enter-pop',
				'label' => __( 'Pop', 'shois-chat-button' ),
				'group' => 'entrance',
			),
			array(
				'id'    => 'shcb-enter-rubber',
				'label' => __( 'Rubber Band', 'shois-chat-button' ),
				'group' => 'entrance',
			),
		);
	}

	/**
	 * Get all available idle/attention animations.
	 *
	 * @return array List of animation definitions.
	 */
	public static function get_idle_animations() {
		return array(
			array(
				'id'    => 'shcb-idle-pulse',
				'label' => __( 'Pulse Ring', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-bounce',
				'label' => __( 'Bounce', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-shake',
				'label' => __( 'Shake', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-heartbeat',
				'label' => __( 'Heartbeat', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-jello',
				'label' => __( 'Jello', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-swing',
				'label' => __( 'Swing', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-tada',
				'label' => __( 'Tada', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-neon',
				'label' => __( 'Neon Glow', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-float',
				'label' => __( 'Float', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-wave',
				'label' => __( 'Wave', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-ripple',
				'label' => __( 'Ripple', 'shois-chat-button' ),
				'group' => 'idle',
			),
			array(
				'id'    => 'shcb-idle-flash',
				'label' => __( 'Flash', 'shois-chat-button' ),
				'group' => 'idle',
			),
		);
	}

	/**
	 * Get all available hover animations.
	 *
	 * @return array List of animation definitions.
	 */
	public static function get_hover_animations() {
		return array(
			array(
				'id'    => 'shcb-hover-grow',
				'label' => __( 'Grow', 'shois-chat-button' ),
				'group' => 'hover',
			),
			array(
				'id'    => 'shcb-hover-shrink',
				'label' => __( 'Shrink', 'shois-chat-button' ),
				'group' => 'hover',
			),
			array(
				'id'    => 'shcb-hover-glow',
				'label' => __( 'Glow', 'shois-chat-button' ),
				'group' => 'hover',
			),
			array(
				'id'    => 'shcb-hover-rotatecw',
				'label' => __( 'Rotate CW', 'shois-chat-button' ),
				'group' => 'hover',
			),
			array(
				'id'    => 'shcb-hover-rotateccw',
				'label' => __( 'Rotate CCW', 'shois-chat-button' ),
				'group' => 'hover',
			),
			array(
				'id'    => 'shcb-hover-wobble',
				'label' => __( 'Wobble', 'shois-chat-button' ),
				'group' => 'hover',
			),
			array(
				'id'    => 'shcb-hover-lift',
				'label' => __( 'Lift', 'shois-chat-button' ),
				'group' => 'hover',
			),
			array(
				'id'    => 'shcb-hover-color',
				'label' => __( 'Color Shift', 'shois-chat-button' ),
				'group' => 'hover',
			),
			array(
				'id'    => 'shcb-hover-ripple',
				'label' => __( 'Ripple', 'shois-chat-button' ),
				'group' => 'hover',
			),
			array(
				'id'    => 'shcb-hover-iconspin',
				'label' => __( 'Icon Spin', 'shois-chat-button' ),
				'group' => 'hover',
			),
		);
	}

	/**
	 * Get all available popup transition animations.
	 *
	 * @return array List of animation definitions.
	 */
	public static function get_popup_animations() {
		return array(
			array(
				'id'    => 'shcb-open-fadescale',
				'label' => __( 'Fade Scale', 'shois-chat-button' ),
				'group' => 'popup',
			),
			array(
				'id'    => 'shcb-open-slideuppanel',
				'label' => __( 'Slide Up', 'shois-chat-button' ),
				'group' => 'popup',
			),
			array(
				'id'    => 'shcb-open-spring',
				'label' => __( 'Spring Pop', 'shois-chat-button' ),
				'group' => 'popup',
			),
			array(
				'id'    => 'shcb-open-flip',
				'label' => __( 'Flip', 'shois-chat-button' ),
				'group' => 'popup',
			),
			array(
				'id'    => 'shcb-open-unfold',
				'label' => __( 'Unfold', 'shois-chat-button' ),
				'group' => 'popup',
			),
			array(
				'id'    => 'shcb-open-door',
				'label' => __( 'Door Open', 'shois-chat-button' ),
				'group' => 'popup',
			),
			array(
				'id'    => 'shcb-open-balloon',
				'label' => __( 'Balloon', 'shois-chat-button' ),
				'group' => 'popup',
			),
			array(
				'id'    => 'shcb-open-blurin',
				'label' => __( 'Blur In', 'shois-chat-button' ),
				'group' => 'popup',
			),
		);
	}

	/**
	 * Get all available platform icon expansion animations.
	 *
	 * @return array List of animation definitions.
	 */
	public static function get_platform_icon_animations() {
		return array(
			array(
				'id'    => 'shcb-platform-fan',
				'label' => __( 'Fan Out', 'shois-chat-button' ),
				'group' => 'platform_icon',
			),
			array(
				'id'    => 'shcb-platform-cascade',
				'label' => __( 'Cascade', 'shois-chat-button' ),
				'group' => 'platform_icon',
			),
			array(
				'id'    => 'shcb-platform-scatter',
				'label' => __( 'Scatter', 'shois-chat-button' ),
				'group' => 'platform_icon',
			),
			array(
				'id'    => 'shcb-platform-instant',
				'label' => __( 'Instant', 'shois-chat-button' ),
				'group' => 'platform_icon',
			),
		);
	}

	/**
	 * Get all available badge animations.
	 *
	 * @return array List of animation definitions.
	 */
	public static function get_badge_animations() {
		return array(
			array(
				'id'    => 'shcb-badge-countup',
				'label' => __( 'Count Up', 'shois-chat-button' ),
				'group' => 'badge',
			),
			array(
				'id'    => 'shcb-badge-bounce',
				'label' => __( 'Bounce', 'shois-chat-button' ),
				'group' => 'badge',
			),
			array(
				'id'    => 'shcb-badge-pulse',
				'label' => __( 'Pulse', 'shois-chat-button' ),
				'group' => 'badge',
			),
		);
	}

	/**
	 * Get all animation groups combined for the REST API.
	 *
	 * @return array All animation definitions grouped.
	 */
	public static function get_all_animations_for_api() {
		return array(
			'entrance'      => self::get_entrance_animations(),
			'idle'          => self::get_idle_animations(),
			'hover'         => self::get_hover_animations(),
			'popup'         => self::get_popup_animations(),
			'platform_icon' => self::get_platform_icon_animations(),
			'badge'         => self::get_badge_animations(),
		);
	}

	/**
	 * Build animation config for the frontend shcb_config object.
	 *
	 * @param array $template_config Template config with animation settings.
	 * @return array Animation config for JS.
	 */
	public static function build_animation_config( $template_config ) {
		// Support both flat keys (new format) and nested 'animations' array (legacy).
		$legacy_anims = isset( $template_config['animations'] ) && is_array( $template_config['animations'] ) ? $template_config['animations'] : array();

		// Helper to gently fallback between flat key, legacy key, and default.
		$get_anim = function ( $key, $default ) use ( $template_config, $legacy_anims ) {
			if ( array_key_exists( $key, $template_config ) ) {
				return $template_config[ $key ];
			}
			if ( array_key_exists( $key, $legacy_anims ) ) {
				return $legacy_anims[ $key ];
			}
			return $default;
		};

		$enable_pulse = true;
		if ( array_key_exists( 'enable_pulse_ring', $template_config ) ) {
			$enable_pulse = (bool) $template_config['enable_pulse_ring'];
		} elseif ( array_key_exists( 'enable_pulse_ring', $legacy_anims ) ) {
			$enable_pulse = (bool) $legacy_anims['enable_pulse_ring'];
		}

		return array(
			'entrance'        => sanitize_html_class( $get_anim( 'entrance', 'shcb-enter-bounce' ) ),
			'entrance_delay'  => absint( $get_anim( 'entrance_delay', 0 ) ),
			'idle'            => sanitize_html_class( $get_anim( 'idle', 'shcb-idle-pulse' ) ),
			'idle_interval'   => max( 1, absint( $get_anim( 'idle_interval', 5 ) ) ),
			'hover'           => sanitize_html_class( $get_anim( 'hover', 'shcb-hover-grow' ) ),
			'popup'           => sanitize_html_class( $get_anim( 'popup', 'shcb-open-spring' ) ),
			'platform_icons'  => sanitize_html_class( $get_anim( 'platform_icons', 'shcb-platform-fan' ) ),
			'badge'           => sanitize_html_class( $get_anim( 'badge', 'shcb-badge-countup' ) ),
			'enable_pulse_ring'=> $enable_pulse,
		);
	}

	/**
	 * Enqueue animation CSS files.
	 * Called by the widget renderer when animations are active.
	 *
	 * @return void
	 */
	public static function enqueue_animation_css() {
		$files = array( 'entrance', 'attention', 'hover', 'transition' );

		foreach ( $files as $file ) {
			$css_path = SHCB_PLUGIN_DIR . 'widget/animations/' . $file . '.css';
			$css_url  = SHCB_PLUGIN_URL . 'widget/animations/' . $file . '.css';

			if ( file_exists( $css_path ) ) {
				wp_enqueue_style(
					'shcb-anim-' . $file,
					$css_url,
					array( 'shcb-template-css' ),
					SHCB_VERSION
				);
			}
		}
	}

	/**
	 * Validate an animation ID against known animation IDs.
	 *
	 * @param string $anim_id Animation CSS class name.
	 * @return bool True if valid.
	 */
	public static function is_valid_animation( $anim_id ) {
		if ( empty( $anim_id ) ) {
			return true; // Empty means "none selected".
		}

		$all = array_merge(
			self::get_entrance_animations(),
			self::get_idle_animations(),
			self::get_hover_animations(),
			self::get_popup_animations(),
			self::get_platform_icon_animations(),
			self::get_badge_animations()
		);

		foreach ( $all as $anim ) {
			if ( $anim['id'] === $anim_id ) {
				return true;
			}
		}

		return false;
	}
}
