<?php
namespace TeknoTok\AiTranslator\Frontend;

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

/**
 * Controller to handle browser language detection prompts.
 */
class RedirectController {
	/**
	 * Constructor. Hooks into footer.
	 */
	public function __construct() {
		if ( is_admin() ) {
			return;
		}

		add_action( 'wp_footer', array( $this, 'render_redirection_prompt' ) );
	}

	/**
	 * Injects the redirection banner container and parameters.
	 *
	 * @return void
	 */
	public function render_redirection_prompt() {
		$settings = get_option( 'teknotok_ait_settings', array() );
		$detect_browser = $settings['detect_browser'] ?? '1';

		if ( '1' !== $detect_browser ) {
			return;
		}

		$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',
		);

		// Pass target languages configuration to JavaScript
		$targets_mapped = array();
		foreach ( $targets as $lang ) {
			$targets_mapped[ $lang ] = $languages[ $lang ] ?? $lang;
		}

		$prompts = array(
			'en' => array(
				'text'   => 'Our website already supports English. Do you want to use it in your language?',
				'switch' => 'Yes, Switch',
				'dismiss'=> 'No',
			),
			'es' => array(
				'text'   => 'Nuestro sitio web ya es compatible con español. ¿Desea utilizarlo en su idioma?',
				'switch' => 'Sí, cambiar',
				'dismiss'=> 'No',
			),
			'tr' => array(
				'text'   => 'Web sitemiz zaten Türkçe destekliyor. Kendi dilinizde kullanmak ister misiniz?',
				'switch' => 'Evet, değiştir',
				'dismiss'=> 'Hayır',
			),
			'zh' => array(
				'text'   => '我们的网站已支持中文。您想用您的语言浏览吗？',
				'switch' => '是的，切换',
				'dismiss'=> '不用了',
			),
			'hi' => array(
				'text'   => 'हमारी वेबसाइट पहले से ही हिन्दी का समर्थन करती है। क्या आप इसे अपनी भाषा में उपयोग करना चाहते हैं？',
				'switch' => 'हाँ, बदलें',
				'dismiss'=> 'नहीं',
			),
			'ar' => array(
				'text'   => 'موقعنا يدعم اللغة العربية بالفعل. هل ترغب في تصفحه باللغة العربية؟',
				'switch' => 'نعم، قم بالتحويل',
				'dismiss'=> 'لا',
			),
			'pt' => array(
				'text'   => 'Nosso site já é compatível com português. Deseja utilizá-lo em seu idioma?',
				'switch' => 'Sim, alterar',
				'dismiss'=> 'Não',
			),
			'bn' => array(
				'text'   => 'আমাদের ওয়েবসাইট ইতিমধ্যে বাংলা সমর্থন করে। আপনি কি এটি আপনার নিজের ভাষায় ব্যবহার করতে চান？',
				'switch' => 'হ্যাঁ, পরিবর্তন করুন',
				'dismiss'=> 'না',
			),
			'ru' => array(
				'text'   => 'Наш сайт уже поддерживает русский язык. Хотите ли вы использовать его на своем языке?',
				'switch' => 'Да, переключить',
				'dismiss'=> 'Нет',
			),
			'ja' => array(
				'text'   => '当サイトはすでに日本語に対応しています。日本語で表示しますか？',
				'switch' => 'はい、切り替えます',
				'dismiss'=> 'いいえ',
			),
			'fr' => array(
				'text'   => 'Notre site web prend déjà en charge le français. Souhaitez-vous l\'utiliser dans votre langue ?',
				'switch' => 'Oui, changer',
				'dismiss'=> 'Non',
			),
		);

		$active_lang = \TeknoTok\AiTranslator\Plugin::get_active_language();
		$source_lang = $settings['source_language'] ?? 'en';

		// If user is already on a translated locale, do not prompt
		if ( $active_lang !== $source_lang ) {
			return;
		}

		?>
		<div id="teknotok-ait-redirect-prompt" class="teknotok-ait-banner notranslate" translate="no" style="display:none;" data-targets="<?php echo esc_attr( json_encode( $targets_mapped ) ); ?>" data-prompts="<?php echo esc_attr( json_encode( $prompts ) ); ?>">
			<div class="teknotok-ait-banner-content">
				<span class="teknotok-ait-banner-text">
					<?php esc_html_e( 'It looks like your browser language is %lang_name%. Would you like to switch?', 'teknotok-ai-translator-pro' ); ?>
				</span>
				<div class="teknotok-ait-banner-actions">
					<button id="teknotok-ait-btn-switch" class="button button-primary"><?php esc_html_e( 'Yes, Switch', 'teknotok-ai-translator-pro' ); ?></button>
					<button id="teknotok-ait-btn-dismiss" class="button button-secondary"><?php esc_html_e( 'No', 'teknotok-ai-translator-pro' ); ?></button>
				</div>
			</div>
		</div>
		<?php
	}
}
