<?php
/**
 * Gutenberg Typography Functionality
 * 
 * Handles block editor font loading and styling based on customizer settings.
 * 
 * @package bootstrap-coach
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Block editor assets - fonts and styles
 * 
 * Loads Google Fonts and applies custom typography styles in the block editor
 * based on theme customizer font family settings.
 */
function bootstrap_coach_block_editor_assets() {
    // Only run in admin context
    if ( ! is_admin() ) {
        return;
    }

    $font_family         = get_theme_mod( 'body_font_family', 'Nunito' );
    $heading_font_family = get_theme_mod( 'heading_font_family', 'Merriweather' );
    $font_size           = esc_attr(get_theme_mod('font_size', '16px'));
    $font_weight         = absint(get_theme_mod('body_font_weight', 600));
    $line_height         = floatval(get_theme_mod('body_line_height', 1.5));
    $heading_font_weight = esc_attr(get_theme_mod('bootstrap_coach_heading_font_weight', 600));
    $text_color          = get_theme_mod('text_color', '#757575');
    $dark_color          = get_theme_mod('dark_color', '#000000');
    $primary_color       = get_theme_mod('primary_color', '#FF6C2E');

    // Get heading sizes
    $default_size = array(
        '1' =>  38,
        '2' =>  34,
        '3' =>  30,
        '4' =>  26,
        '5' =>  24,
        '6' =>  18,
    );

    $heading = array();
    for ($i = 1; $i <= 6; $i++) {
        $heading[$i] = absint(get_theme_mod('bootstrap_coach_heading_' . $i . '_size', absint($default_size[$i])));
    }

    // Enqueue Google Fonts for block editor
    wp_enqueue_style(
        'bootstrap-coach-editor-googlefonts',
        'https://fonts.googleapis.com/css?family=' . 
          esc_attr( $font_family ) . ':100,200,300,400,500,600,700,800,900|' .
          esc_attr( $heading_font_family ) . ':100,200,300,400,500,600,700,800,900',
        array(),
        null
    );

    // Enqueue editor styles CSS file
    wp_enqueue_style(
        'bootstrap-coach-editor-styles',
        get_template_directory_uri() . '/inc/gutenberg/typography/editor-styles.css',
        array( 'wp-edit-blocks' ),
        filemtime( get_template_directory() . '/inc/gutenberg/typography/editor-styles.css' )
    );

    // Add dynamic font family values as CSS custom properties
    $custom_css = "
    :root {
        --font-family: '{$font_family}';
        --heading-font-family: '{$heading_font_family}';
        --font-size: {$font_size};
        --font-weight: {$font_weight};
        --line-height: {$line_height};
        --heading-font-weight: {$heading_font_weight};
        --text-color: {$text_color};
        --heading-title-color: {$dark_color};
        --h1-font-size: {$heading[1]}px;
        --h2-font-size: {$heading[2]}px;
        --h3-font-size: {$heading[3]}px;
        --h4-font-size: {$heading[4]}px;
        --h5-font-size: {$heading[5]}px;
        --h6-font-size: {$heading[6]}px;
        --primary-color: " . esc_attr( $primary_color ) . ";
    }";
    wp_add_inline_style( 'bootstrap-coach-editor-styles', $custom_css );
}
add_action( 'enqueue_block_assets', 'bootstrap_coach_block_editor_assets' );
