<?php

namespace GriffinForms\Admin\Html\FormControls\FormTheme;

/**
 * FontRegistry
 *
 * Central registry for all available fonts in the form theme system.
 * Serves as the single source of truth for font definitions.
 *
 * This class provides methods to retrieve system fonts, external fonts,
 * and all available fonts in a structured format suitable for UI rendering
 * and font loading.
 */
class FontRegistry
{
    /**
     * Array of font definitions organized by category
     */
    protected $fonts = array();

    /**
     * Constructor - initializes font registry with predefined fonts
     */
    public function __construct()
    {
        $this->fonts = array(
            'system' => array(
                'inherit' => __('Default (Inherit)', 'griffinforms-form-builder'),
                'sans-serif' => 'System Sans-serif',
                'Arial' => 'Arial',
                'Verdana' => 'Verdana',
                'Georgia' => 'Georgia',
            ),
            'external' => array(
                'Open Sans' => 'Open Sans',
                'Roboto' => 'Roboto',
                'Lato' => 'Lato',
                'Playfair Display' => 'Playfair Display',
                'Raleway' => 'Raleway',
                'Montserrat' => 'Montserrat',
                'Poppins' => 'Poppins',
                'Manrope' => 'Manrope',
                'Inter' => 'Inter',
                'Nunito' => 'Nunito',
                'Quicksand' => 'Quicksand',
                'DM Sans' => 'DM Sans',
                'Outfit' => 'Outfit',
                'Source Sans Pro' => 'Source Sans Pro',
                'Libre Franklin' => 'Libre Franklin',
                'Merriweather' => 'Merriweather',
                'IBM Plex Sans' => 'IBM Plex Sans',
                'Work Sans' => 'Work Sans',
                'Sora' => 'Sora',
                'Urbanist' => 'Urbanist',
                'Roboto Mono' => 'Roboto Mono',
                'JetBrains Mono' => 'JetBrains Mono',
                'Delius' => 'Delius',
                'Mea Culpa' => 'Mea Culpa',
            )
        );
    }

    /**
     * Get all available fonts organized by category
     *
     * @return array Associative array with keys 'system' and 'external'
     */
    public function getAll()
    {
        return $this->fonts;
    }

    /**
     * Get system fonts only (fonts that don't require Google Fonts)
     *
     * @return array Associative array of system font family => label
     */
    public function getSystemFonts()
    {
        return $this->fonts['system'] ?? array();
    }

    /**
     * Get external fonts only (Google Fonts)
     *
     * @return array Associative array of external font family => label
     */
    public function getExternalFonts()
    {
        return $this->fonts['external'] ?? array();
    }

    /**
     * Get external font family names only (keys without labels)
     *
     * Useful for checking if a font is external or for enqueueing.
     *
     * @return array Array of external font family names
     */
    public function getExternalFontNames()
    {
        return array_keys($this->getExternalFonts());
    }

    /**
     * Get all fonts combined (system + external)
     *
     * @return array Associative array of all font family => label
     */
    public function getCombined()
    {
        return array_merge($this->getSystemFonts(), $this->getExternalFonts());
    }

    /**
     * Check if a font family is an external font
     *
     * @param string $font_family Font family name to check
     * @return bool True if font is external, false otherwise
     */
    public function isExternal($font_family)
    {
        return in_array($font_family, $this->getExternalFontNames(), true);
    }

    /**
     * Check if a font family is a system font
     *
     * @param string $font_family Font family name to check
     * @return bool True if font is system, false otherwise
     */
    public function isSystem($font_family)
    {
        return isset($this->fonts['system'][$font_family]);
    }
}
