<?php
namespace OxygenApp\Views\Admin\Tax;
if ( ! defined( 'ABSPATH' ) ) exit;


use OxygenApp\Views\BaseView;
use OxygenApp\Models\AdminPlaylist as AdminPlaylistModel;
use OxygenApp\Utils\LanguagesHelper;
use OxygenApp\Config\Flags;


class AdminPlaylist extends BaseView {

    static function renderOnCreateNew($taxonomy) {
        $feIdSvg = uniqid();

        $langCodes = LanguagesHelper::getInstance()->getExistingLangCodes();
        $baseFlagsUrl = Flags::getBaseURL();
        $mainMetaKey = AdminPlaylistModel::META_KEY;
        ?>

            <?php foreach ($langCodes as $key => $langCode): ?>
                <?php 
                    $langData = Flags::findLangByCode($langCode);
                    $flagIcon = $langData['flag_4x3'] ?? '';
                    $labelValue = $labelsValue[$langCode]['playlist_name'] ?? '';
                    self::renderTextInput(langCode: $langCode, baseFlagsUrl: $baseFlagsUrl, 
                    flagIcon: $flagIcon, mainMetaKey: $mainMetaKey, metaKey: 'playlist_name', value: $labelValue, label: esc_html__('Playlist name', 'oxyleaf-app-working-and-relaxing-app'));
                ?>
            <?php endforeach; ?>
        <?php
    }

    static function renderOnEdit($term, $taxonomy) {
        $feIdSvg = uniqid();
        $mainMetaKey = AdminPlaylistModel::META_KEY;

        $langCodes = LanguagesHelper::getInstance()->getExistingLangCodes();
        $baseFlagsUrl = Flags::getBaseURL();

        $labelsValue = get_term_meta($term->term_id, "labels_$mainMetaKey", true);

        ?>

        <?php foreach ($langCodes as $key => $langCode): ?>
            <?php 
            $feId = uniqid();
            $langData = Flags::findLangByCode($langCode);
            $flagIcon = $langData['flag_4x3'] ?? '';
            $labelValue = $labelsValue[$langCode]['playlist_name'] ?? '';
            $label = esc_html__('Playlist name', 'oxyleaf-app-working-and-relaxing-app')
            ?>
            <tr class="form-field">
                <th>
                    <label for="<?php esc_attr($feId) ?>">
                        <span class="skl-flag" style="width: 30px"><img style="max-height: 15px;" src="<?php echo esc_url($baseFlagsUrl . $flagIcon) ?>" alt=""></span>
                        <span>(<?php echo esc_html(strtoupper($langCode)) ?>) </span>
                        <?php echo esc_html($label) ?>
                    </label>
                </th>
                <td>
                    <div class="svg">
                        <div class="form-field mb-3">
                            <input type="text" 
                                value="<?php echo esc_attr($labelValue) ?>" 
                                name="<?php echo esc_attr("labels_{$mainMetaKey}[{$langCode}][playlist_name]") ?>" 
                                class="form-control" id="<?php echo esc_attr($feId) ?>">
                        </div>
                    </div>
                </td>
            </tr>
        <?php endforeach; ?>
        <?php

    }

    private static function renderTextInput(string $langCode, string $baseFlagsUrl, string $flagIcon, 
    string $mainMetaKey, string $metaKey, string $value = '', string $label = '', string $info = '') {
        $feId = uniqid();
        ?>
            <div class="mb-3">
                <label for="<?php esc_attr($feId) ?>" class="form-label w-100">
                    <span class="skl-flag" style="width: 30px"><img style="max-height: 15px;" src="<?php echo esc_url($baseFlagsUrl . $flagIcon) ?>" alt=""></span>
                    <span>(<?php echo esc_html(strtoupper($langCode)) ?>) </span>
                    <?php echo esc_html($label) ?>
                </label>
                <input type="text" 
                    value="<?php echo esc_attr($value) ?>" 
                    name="<?php echo esc_attr("labels_{$mainMetaKey}[{$langCode}][{$metaKey}]") ?>" 
                    class="form-control" id="<?php echo esc_attr($feId) ?>">
                <?php if ($info): ?>
                    <div class="fst-italic">
                        <?php echo esc_html($info) ?>
                    </div>
                <?php endif; ?>
            </div>
        <?php
    }
}