<?php
if (!defined('WPINC')) {
    die();
}

use EstatebudConnect\Configs\Icons;
use EstatebudConnect\Utilities\Helper;
use EstatebudConnect\Models\Localizations;
use EstatebudConnect\Core\Locale;

$enabled_languages = Locale::instance()->get_enabled_languages();
$current_language = 'en';

foreach ($enabled_languages as $language) {
    if ($language['active']) {
        $current_language = $language['code'];
        break;
    }
}

$dynamic_terms = Helper::get_translatable_dynamic_terms();
$locale_terms = Locale::instance()->get_terms($current_language);

foreach ($locale_terms as $key => $term) {
	if (isset($term['value']) && is_array($term['value'])) {
		if (isset($term['value']['one'])) {
			$locale_terms[$key]['value']['singular'] = $term['value']['one'];
		}

		if (isset($term['value']['other'])) {
			$locale_terms[$key]['value']['plural'] = $term['value']['other'];
		}

		unset($locale_terms[$key]['value']['one'], $locale_terms[$key]['value']['other']);
	}
}

$translatable_terms = array_merge($dynamic_terms, $locale_terms);

$localizations_model = Localizations::instance();
$existing_localizations = $localizations_model->get_localizations($current_language);
$existing_localizations_keys = array_column($existing_localizations, 'key');

$has_multiline_text = static function ($value) {
    return is_string($value) && preg_match('/\r|\n/', $value);
};

if (empty($enabled_languages)) {
    $enabled_languages = [
        [
            'code' => 'en',
            'name' => 'English',
            'flag' => '',
            'url' => '',
            'active' => true,
        ],
    ];
}
?>
<div class="estatebud-sidebar-content-body-scrollable">
	<div class="estatebud-sidebar-content-body-table-wrapper estatebud-localization-table-wrapper">
		<div class="estatebud-action-group">
			<div class="estatebud-action-group-1">
				<div class="estatebud-localization-dropdowns">
					<div class="estatebud-dropdown-wrapper">
						<select class="estatebud-choices-select" id="language-selector" name="language" data-placeholder="Language">
							<?php foreach ($enabled_languages as $language): ?>
								<option value="<?php echo esc_attr($language['code']); ?>"<?php echo $language['active'] ? ' selected' : ''; ?>>
									<?php echo esc_html(Helper::get_locale_display_name($language['code'], $language['name'])); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</div>
					<div class="estatebud-dropdown-wrapper">
						<select class="estatebud-choices-select" id="filter-selector" name="filter" data-placeholder="Filter">
							<option value="all" selected>All</option>
							<option value="field-label">Field label</option>
							<option value="field-placeholder">Field placeholder</option>
							<option value="field-option">Field option</option>
							<option value="button-text">Button text</option>
						</select>
					</div>
				</div>
			</div>
			<div class="estatebud-action-group-2">
				<input type="text" class="estatebud-search-input" placeholder="Search" id="localization-search">
			</div>
		</div>

		<div class="estatebud-localization-table">
			<table class="estatebud-localization-table">
				<thead>
					<tr>
						<th>Value</th>
						<th>Translation</th>
					</tr>
				</thead>
				<tbody id="localization-table-body">
					<?php if (!empty($translatable_terms)): ?>
						<?php foreach ($translatable_terms as $term): ?>
							<?php
							$existing_translation_index = array_search($term['key'], $existing_localizations_keys);
							$existing_translation = $existing_translation_index !== false ? $existing_localizations[$existing_translation_index] : null;
							?>
							<?php if (is_array($term['value'])): ?>
								<?php
								$singular_term = $term['value']['singular'] ?? '';
								$plural_term = $term['value']['plural'] ?? '';
								
								$existing_singular_term = $existing_translation['text']['singular'] ?? '';
								$existing_plural_term = $existing_translation['text']['plural'] ?? '';

								$use_singular_textarea = $has_multiline_text($singular_term);
								$use_plural_textarea = $has_multiline_text($plural_term);
								?>
								<tr data-key="<?php echo esc_attr($term['key']); ?>" class="estatebud-singular-plural-row">
									<td class="estatebud-localization-value-cell">
										<div class="estatebud-singular-plural-container">
											<div class="estatebud-singular-row">
												<div class="estatebud-localization-value-content">
													<span><?php echo nl2br(esc_html($singular_term)); ?></span>
													<div class="estatebud-localization-tags">
														<span class="estatebud-singular-chip">Singular</span>
														<span class="estatebud-localization-tag"><?php echo esc_html(Helper::get_term_type_label($term['key'])); ?></span>
													</div>
												</div>
											</div>
											<div class="estatebud-plural-row">
												<div class="estatebud-localization-value-content">
													<span><?php echo nl2br(esc_html($plural_term)); ?></span>
													<div class="estatebud-localization-tags">
														<span class="estatebud-plural-chip">Plural</span>
														<span class="estatebud-localization-tag"><?php echo esc_html(Helper::get_term_type_label($term['key'])); ?></span>
													</div>
												</div>
											</div>
										</div>
									</td>
									<td class="estatebud-localization-translation-cell">
										<div class="estatebud-singular-plural-inputs">
											<div class="estatebud-input-group">
												<?php if ($use_singular_textarea): ?>
													<textarea class="estatebud-text-field estatebud-singular-input"
														placeholder="<?php echo esc_attr($singular_term); ?>"><?php echo esc_textarea($existing_singular_term); ?></textarea>
												<?php else: ?>
													<input type="text" class="estatebud-text-field estatebud-singular-input"
														value="<?php echo esc_attr($existing_singular_term); ?>"
														placeholder="<?php echo esc_attr($singular_term); ?>">
												<?php endif; ?>
											</div>
											<div class="estatebud-input-group">
												<?php if ($use_plural_textarea): ?>
													<textarea class="estatebud-text-field estatebud-plural-input"
														placeholder="<?php echo esc_attr($plural_term); ?>"><?php echo esc_textarea($existing_plural_term); ?></textarea>
												<?php else: ?>
													<input type="text" class="estatebud-text-field estatebud-plural-input"
														value="<?php echo esc_attr($existing_plural_term); ?>"
														placeholder="<?php echo esc_attr($plural_term); ?>">
												<?php endif; ?>
											</div>
										</div>
									</td>
								</tr>
							<?php else: ?>
								<?php
								$term_value = $term['value'] ?? '';
								$existing_term = $existing_translation['text'] ?? '';
								$use_textarea = $has_multiline_text($term_value);
								?>
								<tr data-key="<?php echo esc_attr($term['key']); ?>">
									<td class="estatebud-localization-value-cell">
										<div class="estatebud-localization-value-content">
											<span><?php echo nl2br(esc_html($term_value)); ?></span>
											<div class="estatebud-localization-tags">
												<span class="estatebud-localization-tag"><?php echo esc_html(Helper::get_term_type_label($term['key'])); ?></span>
											</div>
										</div>
									</td>
									<td class="estatebud-localization-translation-cell">
										<?php if ($use_textarea): ?>
											<textarea class="estatebud-text-field"
												placeholder="<?php echo esc_attr($term_value); ?>"><?php echo esc_textarea($existing_term); ?></textarea>
										<?php else: ?>
											<input type="text" class="estatebud-text-field"
												value="<?php echo esc_attr($existing_term); ?>"
												placeholder="<?php echo esc_attr($term_value); ?>">
										<?php endif; ?>
									</td>
								</tr>
							<?php endif; ?>
						<?php endforeach; ?>
					<?php else: ?>
						<tr>
							<td colspan="2" class="estatebud-localization-empty-state">
								<div class="estatebud-localization-empty-content">
									<span>No translatable terms found. Create some forms first.</span>
								</div>
							</td>
						</tr>
					<?php endif; ?>
				</tbody>
			</table>
		</div>
	</div>
</div>
