<?php
namespace TeknoTok\AiTranslator\Frontend;

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

/**
 * Controller to handle frontend language switcher rendering and assets.
 */
class SwitcherController {
	/**
	 * Constructor. Registers shortcode and output hooks.
	 */
	public function __construct() {
		add_shortcode( 'teknotok_language_switcher', array( $this, 'render_switcher_shortcode' ) );
		add_action( 'wp_footer', array( $this, 'render_floating_switcher' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	/**
	 * Enqueue switcher styles and scripts.
	 *
	 * @return void
	 */
	public function enqueue_assets() {
		wp_enqueue_style( 'teknotok-ait-switcher', TEKNOTOK_AIT_URL . 'assets/css/switcher.css', array(), TEKNOTOK_AIT_VERSION );
		wp_enqueue_script( 'teknotok-ait-switcher', TEKNOTOK_AIT_URL . 'assets/js/switcher.js', array( 'jquery' ), TEKNOTOK_AIT_VERSION, true );

		$settings = get_option( 'teknotok_ait_settings', array() );
		$source = $settings['source_language'] ?? 'en';
		$targets = $settings['target_languages'] ?? array();
		$all_langs = array_merge( array( $source ), $targets );

		$lang_urls = array();
		foreach ( $all_langs as $lang ) {
			$lang_urls[ $lang ] = \TeknoTok\AiTranslator\Frontend\UrlManager::get_clean_url_for_language( $lang );
		}

		wp_localize_script(
			'teknotok-ait-switcher',
			'teknotokAitSwitcher',
			array(
				'urls' => $lang_urls,
			)
		);
	}

	/**
	 * Output markup for the language switcher list.
	 *
	 * @return string HTML output.
	 */
	public function render_switcher_html() {
		$settings = get_option( 'teknotok_ait_settings', array() );
		$source = $settings['source_language'] ?? 'en';
		$targets = $settings['target_languages'] ?? array();

		if ( empty( $targets ) ) {
			return '';
		}

		$languages = array(
			'en' => 'English',
			'es' => 'Español',
			'tr' => 'Türkçe',
			'zh' => '中文',
			'hi' => 'हिन्दी',
			'ar' => 'العربية',
			'pt' => 'Português',
			'bn' => 'বাংলা',
			'ru' => 'Русский',
			'ja' => '日本語',
			'fr' => 'Français',
		);

		$active_lang = \TeknoTok\AiTranslator\Plugin::get_active_language();

		ob_start();
		?>
		<div class="teknotok-ait-switcher-container notranslate" translate="no">
			<div class="teknotok-ait-selected-lang">
				<img src="<?php echo esc_url( self::get_flag_url( $active_lang ) ); ?>" class="teknotok-ait-flag" alt="<?php echo esc_attr( $active_lang ); ?>" />
				<span class="teknotok-ait-label"><?php echo esc_html( $languages[ $active_lang ] ?? $active_lang ); ?></span>
				<span class="teknotok-ait-arrow"></span>
			</div>
			<ul class="teknotok-ait-languages-dropdown">
				<!-- Source Language -->
				<li class="<?php echo ( $active_lang === $source ) ? 'active' : ''; ?>" data-lang="<?php echo esc_attr( $source ); ?>">
					<img src="<?php echo esc_url( self::get_flag_url( $source ) ); ?>" class="teknotok-ait-flag" alt="<?php echo esc_attr( $source ); ?>" />
					<?php echo esc_html( $languages[ $source ] ?? $source ); ?>
				</li>
				<!-- Target Languages -->
				<?php foreach ( $targets as $lang ) : ?>
					<li class="<?php echo ( $active_lang === $lang ) ? 'active' : ''; ?>" data-lang="<?php echo esc_attr( $lang ); ?>">
						<img src="<?php echo esc_url( self::get_flag_url( $lang ) ); ?>" class="teknotok-ait-flag" alt="<?php echo esc_attr( $lang ); ?>" />
						<?php echo esc_html( $languages[ $lang ] ?? $lang ); ?>
					</li>
				<?php endforeach; ?>
				<!-- Referral Link -->
				<li class="teknotok-ait-referral-item">
					<a href="https://soft.teknotok.com/teknotok-translator-pro/" target="_blank" rel="noopener">
						<span class="teknotok-ait-referral-icon">⚡</span>
						<?php esc_html_e( 'AI Translator Pro', 'teknotok-ai-translator-pro' ); ?>
					</a>
				</li>
			</ul>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Shortcode callback.
	 *
	 * @return string
	 */
	public function render_switcher_shortcode() {
		return $this->render_switcher_html();
	}

	/**
	 * Render floating switcher overlay in footer.
	 *
	 * @return void
	 */
	public function render_floating_switcher() {
		$settings = get_option( 'teknotok_ait_settings', array() );
		$position = $settings['switcher_position'] ?? 'floating-bottom-right';

		if ( 'shortcode' === $position ) {
			return;
		}

		$class = 'teknotok-ait-floating-' . $position;
		$html = $this->render_switcher_html();

		if ( ! empty( $html ) ) {
			echo '<div class="teknotok-ait-floating-wrapper ' . esc_attr( $class ) . ' notranslate" translate="no">' . $html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}
	}

	/**
	 * Get the flag image URL from FlagCDN.
	 *
	 * @param string $lang Language code.
	 * @return string Flag image URL.
	 */
	public static function get_flag_url( $lang ) {
		$mapping = array(
			'en' => 'gb', // UK flag for English
			'es' => 'es', // Spain
			'tr' => 'tr', // Turkey
			'zh' => 'cn', // China
			'hi' => 'in', // India
			'ar' => 'sa', // Saudi Arabia
			'pt' => 'pt', // Portugal
			'bn' => 'bd', // Bangladesh
			'ru' => 'ru', // Russia
			'ja' => 'jp', // Japan
			'fr' => 'fr', // France
		);
		$code = $mapping[ $lang ] ?? $lang;
		return 'https://flagcdn.com/w40/' . $code . '.png';
	}
}
