<?php

declare(strict_types=1);

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

use Signocore\Multilanguage\Application\Constants;
use Signocore\Multilanguage\Support\FlagHelper;

/**
 * @var array<string, array{code: string, locale: string, name: string, native_name: string, rtl: bool}> $languages
 * @var array<string, object> $activeLanguages
 * @var string $defaultCode
 */

$activeCodes = array_keys($activeLanguages);
$selectedCount = count($activeCodes);
?>

<h2 class="sml-wizard-title"><?php esc_html_e('Choose your languages', 'signocore-multilanguage'); ?></h2>
<p class="sml-wizard-subtitle"><?php esc_html_e('Select the languages you want your site to be available in. You can always add or remove languages later.', 'signocore-multilanguage'); ?></p>

<div class="sml-search-wrap">
	<span class="dashicons dashicons-search"></span>
	<input
		type="text"
		id="sml-language-search"
		class="sml-search-input"
		placeholder="<?php esc_attr_e('Search languages...', 'signocore-multilanguage'); ?>"
		autocomplete="off"
	>
</div>

<p class="sml-selected-count" id="sml-selected-count">
	<strong><?php echo esc_html((string) $selectedCount); ?></strong>
	<?php echo esc_html(_n('language selected', 'languages selected', $selectedCount, 'signocore-multilanguage')); ?>
</p>

<p class="sml-min-warning <?php echo $selectedCount > 0 && $selectedCount < 2 ? 'visible' : ''; ?>" id="sml-min-warning">
	<?php esc_html_e('Please select at least 2 languages to continue.', 'signocore-multilanguage'); ?>
</p>

<?php if (!\Signocore\Multilanguage\Application\LicenseManager::load()->isPro()) : ?>
	<p class="sml-limit-warning <?php echo $selectedCount >= \Signocore\Multilanguage\Application\Entitlements::FREE_LANGUAGE_LIMIT ? 'visible' : ''; ?>" id="sml-limit-warning">
		<?php echo esc_html(sprintf(
			/* translators: %d: maximum number of languages */
			__('Free plan is limited to %d languages. Upgrade to Pro for unlimited.', 'signocore-multilanguage'),
			\Signocore\Multilanguage\Application\Entitlements::FREE_LANGUAGE_LIMIT
		)); ?>
	</p>
<?php endif; ?>

<div class="sml-language-grid">
	<?php foreach ($languages as $code => $lang): ?>
		<?php
		$isActive = in_array($code, $activeCodes, true);
		$isDefault = $code === $defaultCode;
		?>
		<div
			class="sml-language-item <?php echo $isActive ? 'selected' : ''; ?>"
			data-code="<?php echo esc_attr($code); ?>"
			data-name="<?php echo esc_attr($lang['name']); ?>"
			data-native="<?php echo esc_attr($lang['native_name']); ?>"
		>
			<div class="sml-language-checkbox">
				<input
					type="checkbox"
					name="languages[]"
					value="<?php echo esc_attr($code); ?>"
					<?php checked($isActive); ?>
					id="sml-lang-<?php echo esc_attr($code); ?>"
				>
			</div>

			<?php echo FlagHelper::getFlagImg($code, 24, 18, $lang['name']); ?>

			<div class="sml-language-info">
				<span class="sml-language-name"><?php echo esc_html($lang['name']); ?></span>
				<?php if ($lang['native_name'] !== $lang['name']): ?>
					<span class="sml-language-native"><?php echo esc_html($lang['native_name']); ?></span>
				<?php endif; ?>
			</div>

			<div class="sml-language-default" style="display: <?php echo $isActive ? 'block' : 'none'; ?>;">
				<label>
					<input
						type="radio"
						name="default_language"
						value="<?php echo esc_attr($code); ?>"
						<?php checked($isDefault); ?>
					>
					<span><?php esc_html_e('Default', 'signocore-multilanguage'); ?></span>
				</label>
			</div>
		</div>
	<?php endforeach; ?>

	<div class="sml-no-results" id="sml-no-results">
		<?php esc_html_e('No languages found matching your search.', 'signocore-multilanguage'); ?>
	</div>
</div>

<div class="sml-wizard-actions">
	<button type="button" class="sml-skip-link"><?php esc_html_e('Skip Setup', 'signocore-multilanguage'); ?></button>
	<div class="sml-wizard-actions-right">
		<button type="button" id="sml-next-btn" class="sml-btn sml-btn-primary" <?php echo $selectedCount < 2 ? 'disabled' : ''; ?>>
			<?php esc_html_e('Next', 'signocore-multilanguage'); ?>
			<span class="sml-spinner"></span>
		</button>
	</div>
</div>
