<?php
/**
 * Admin Page for Maple Local Fonts.
 *
 * @package Maple_Local_Fonts
 */

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

/**
 * Class MLF_Admin_Page
 *
 * Handles the admin settings page rendering.
 */
class MLF_Admin_Page {

    /**
     * Available font weights.
     *
     * @var array
     */
    private $weights = [
        100 => 'Thin',
        200 => 'Extra Light',
        300 => 'Light',
        400 => 'Regular',
        500 => 'Medium',
        600 => 'Semi Bold',
        700 => 'Bold',
        800 => 'Extra Bold',
        900 => 'Black',
    ];

    /**
     * Constructor.
     */
    public function __construct() {
        // Empty constructor - class is instantiated for rendering
    }

    /**
     * Render the admin page.
     */
    public function render() {
        $capability = function_exists('mlf_get_capability') ? mlf_get_capability() : 'edit_theme_options';
        if (!current_user_can($capability)) {
            wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'maple-local-fonts'));
        }

        $registry = new MLF_Font_Registry();
        $installed_fonts = $registry->get_imported_fonts();
        ?>
        <div class="wrap mlf-wrap">
            <h1><?php esc_html_e('Maple Local Fonts', 'maple-local-fonts'); ?></h1>

            <div class="mlf-container">
                <!-- Import Section -->
                <div class="mlf-section mlf-import-section">
                    <h2><?php esc_html_e('Import from Google Fonts', 'maple-local-fonts'); ?></h2>

                    <form id="mlf-import-form" class="mlf-form">
                        <div class="mlf-form-row">
                            <label for="mlf-font-name"><?php esc_html_e('Font Name', 'maple-local-fonts'); ?></label>
                            <input type="text" id="mlf-font-name" name="font_name" placeholder="<?php esc_attr_e('e.g., Open Sans', 'maple-local-fonts'); ?>" required />
                            <p class="description"><?php esc_html_e('Enter the exact font name as it appears on Google Fonts.', 'maple-local-fonts'); ?></p>
                        </div>

                        <div class="mlf-form-row">
                            <label><?php esc_html_e('Weights', 'maple-local-fonts'); ?></label>
                            <div class="mlf-checkbox-grid">
                                <?php foreach ($this->weights as $weight => $label) : ?>
                                    <label class="mlf-checkbox-label">
                                        <input type="checkbox" name="weights[]" value="<?php echo esc_attr($weight); ?>" <?php checked(in_array($weight, [400, 700], true)); ?> />
                                        <span><?php echo esc_html($weight); ?> (<?php echo esc_html($label); ?>)</span>
                                    </label>
                                <?php endforeach; ?>
                            </div>
                        </div>

                        <div class="mlf-form-row">
                            <label><?php esc_html_e('Styles', 'maple-local-fonts'); ?></label>
                            <div class="mlf-checkbox-grid mlf-checkbox-grid-small">
                                <label class="mlf-checkbox-label">
                                    <input type="checkbox" name="styles[]" value="normal" checked />
                                    <span><?php esc_html_e('Normal', 'maple-local-fonts'); ?></span>
                                </label>
                                <label class="mlf-checkbox-label">
                                    <input type="checkbox" name="styles[]" value="italic" />
                                    <span><?php esc_html_e('Italic', 'maple-local-fonts'); ?></span>
                                </label>
                            </div>
                        </div>

                        <div class="mlf-form-row mlf-form-row-info">
                            <span class="mlf-file-count">
                                <?php esc_html_e('Files to download:', 'maple-local-fonts'); ?>
                                <strong id="mlf-file-count">2</strong>
                            </span>
                        </div>

                        <!-- Font Preview Section -->
                        <div class="mlf-form-row mlf-preview-section" id="mlf-preview-section" style="display: none;">
                            <label><?php esc_html_e('Preview', 'maple-local-fonts'); ?></label>
                            <div class="mlf-preview-box" id="mlf-preview-box">
                                <div class="mlf-preview-text" id="mlf-preview-text">
                                    <span class="mlf-preview-sample mlf-preview-heading"><?php esc_html_e('The quick brown fox jumps over the lazy dog', 'maple-local-fonts'); ?></span>
                                    <span class="mlf-preview-sample mlf-preview-paragraph"><?php esc_html_e('ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789', 'maple-local-fonts'); ?></span>
                                </div>
                                <div class="mlf-preview-loading" id="mlf-preview-loading" style="display: none;">
                                    <span class="spinner is-active"></span>
                                    <span><?php esc_html_e('Loading preview...', 'maple-local-fonts'); ?></span>
                                </div>
                                <div class="mlf-preview-error" id="mlf-preview-error" style="display: none;">
                                    <?php esc_html_e('Could not load font preview. The font may not exist on Google Fonts.', 'maple-local-fonts'); ?>
                                </div>
                            </div>
                            <p class="description"><?php esc_html_e('Preview is loaded directly from Google Fonts. After installation, the font will be served locally.', 'maple-local-fonts'); ?></p>
                        </div>

                        <div class="mlf-form-row mlf-form-row-submit">
                            <button type="submit" class="button button-primary" id="mlf-download-btn">
                                <?php esc_html_e('Download & Install', 'maple-local-fonts'); ?>
                            </button>
                            <span class="spinner" id="mlf-spinner"></span>
                        </div>

                        <div id="mlf-message" class="mlf-message" style="display: none;"></div>
                    </form>
                </div>

                <!-- Installed Fonts Section -->
                <div class="mlf-section mlf-installed-section">
                    <h2><?php esc_html_e('Installed Fonts', 'maple-local-fonts'); ?></h2>

                    <?php if (empty($installed_fonts)) : ?>
                        <p class="mlf-no-fonts"><?php esc_html_e('No fonts installed yet.', 'maple-local-fonts'); ?></p>
                    <?php else : ?>
                        <div id="mlf-font-list" class="mlf-font-list">
                            <?php foreach ($installed_fonts as $font) : ?>
                                <div class="mlf-font-item" data-font-id="<?php echo esc_attr($font['id']); ?>">
                                    <div class="mlf-font-info">
                                        <h3 class="mlf-font-name"><?php echo esc_html($font['name']); ?></h3>
                                        <p class="mlf-font-variants">
                                            <?php
                                            $variant_strings = [];
                                            foreach ($font['variants'] as $variant) {
                                                $variant_strings[] = sprintf('%s %s', $variant['weight'], $variant['style']);
                                            }
                                            echo esc_html(implode(', ', $variant_strings));
                                            ?>
                                        </p>
                                    </div>
                                    <div class="mlf-font-actions">
                                        <button type="button" class="button mlf-delete-btn" data-font-id="<?php echo esc_attr($font['id']); ?>">
                                            <?php esc_html_e('Delete', 'maple-local-fonts'); ?>
                                        </button>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>

                <!-- Info Section -->
                <div class="mlf-section mlf-info-section">
                    <?php if (wp_is_block_theme()) : ?>
                        <div class="mlf-info-box">
                            <span class="dashicons dashicons-info"></span>
                            <p>
                                <?php
                                printf(
                                    /* translators: %s: link to WordPress Editor */
                                    esc_html__('Use %s to apply fonts to your site.', 'maple-local-fonts'),
                                    '<a href="' . esc_url(admin_url('site-editor.php?path=%2Fwp_global_styles')) . '">' . esc_html__('Appearance → Editor → Styles → Typography', 'maple-local-fonts') . '</a>'
                                );
                                ?>
                            </p>
                        </div>
                    <?php else : ?>
                        <div class="mlf-info-box mlf-info-box-classic">
                            <span class="dashicons dashicons-info"></span>
                            <div class="mlf-classic-theme-info">
                                <p><strong><?php esc_html_e('Classic Theme Detected', 'maple-local-fonts'); ?></strong></p>
                                <p><?php esc_html_e('Your theme does not support the Full Site Editor. To use imported fonts, add custom CSS to your theme:', 'maple-local-fonts'); ?></p>
                                <pre class="mlf-code-example">body {
    font-family: "Open Sans", sans-serif;
}
h1, h2, h3, h4, h5, h6 {
    font-family: "Open Sans", sans-serif;
}</pre>
                                <p class="description">
                                    <?php
                                    printf(
                                        /* translators: %s: link to Customizer */
                                        esc_html__('Add this CSS in %s or your theme\'s style.css file.', 'maple-local-fonts'),
                                        '<a href="' . esc_url(admin_url('customize.php')) . '">' . esc_html__('Appearance → Customize → Additional CSS', 'maple-local-fonts') . '</a>'
                                    );
                                    ?>
                                </p>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php
    }
}
