<?php
/**
 * Particular Typography function for this theme
 *
 * Some of this functionality may eventually be superseded by essential features.
 *
 * @package Interactive Education
 */

 function interactive_education_get_all_google_fonts() {
    $interactive_education_webfonts_json = get_template_directory() . '/inc/typography-settings/google-webfonts.json';
    if ( ! file_exists( $interactive_education_webfonts_json ) ) {
        return array();
    }

    $interactive_education_fonts_json_data = file_get_contents( $interactive_education_webfonts_json );
    if ( false === $interactive_education_fonts_json_data ) {
        return array();
    }

    $interactive_education_all_fonts = json_decode( $interactive_education_fonts_json_data, true );
    if ( json_last_error() !== JSON_ERROR_NONE ) {
        return array();
    }

    $interactive_education_google_fonts = array();
    foreach ( $interactive_education_all_fonts as $interactive_education_font ) {
        $interactive_education_google_fonts[ $interactive_education_font['family'] ] = array(
            'family'   => $interactive_education_font['family'],
            'variants' => $interactive_education_font['variants'],
        );
    }
    return $interactive_education_google_fonts;
}

function interactive_education_get_all_google_font_families() {
    $interactive_education_google_fonts  = interactive_education_get_all_google_fonts();
    $interactive_education_font_families = array(
        '' => esc_html__( 'Default (Theme Font Family)', 'interactive-education' ),
    );

    foreach ( $interactive_education_google_fonts as $interactive_education_font ) {
        $interactive_education_font_families[ $interactive_education_font['family'] ] = $interactive_education_font['family'];
    }

    return $interactive_education_font_families;
}

function interactive_education_get_fonts_url() {
    $interactive_education_fonts_url = '';
    $interactive_education_all_fonts = interactive_education_get_all_google_fonts();
    $interactive_education_selected_fonts = array();

    $font_body    = get_theme_mod( 'interactive_education_global_font_setting', '' );

    if ( ! empty( $font_body ) && isset( $interactive_education_all_fonts[ $font_body ] ) ) {
        $variants = $interactive_education_all_fonts[ $font_body ]['variants'];
        $interactive_education_selected_fonts[] = $font_body . ':' . implode( ',', $variants );
    }

    if ( ! empty( $interactive_education_selected_fonts ) ) {
        $query_args = array(
            'family' => urlencode( implode( '|', $interactive_education_selected_fonts ) ),
        );
        $interactive_education_fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
    }

    return $interactive_education_fonts_url;
}

function interactive_education_sanitize_google_fonts( $interactive_education_input, $interactive_education_setting ) {
    $interactive_education_choices = $interactive_education_setting->manager->get_control( $interactive_education_setting->id )->choices;
    return ( array_key_exists( $interactive_education_input, $interactive_education_choices ) ? $interactive_education_input : $interactive_education_setting->default );
}

function interactive_education_dynamic_css() {
    $interactive_education_color         = get_theme_mod( 'interactive_education_global_color', '#ffc107' );
    $interactive_education_colortwo      = get_theme_mod( 'interactive_education_global_colortwo', '#1865f2' );
    $interactive_education_colorthree      = get_theme_mod( 'interactive_education_global_colorthree', '#f4a261' );
    $interactive_education_font_body     = get_theme_mod( 'interactive_education_global_font_setting', '' );

    // Enqueue Google Fonts if needed
    $interactive_education_fonts_url = interactive_education_get_fonts_url();
    if ( ! empty( $interactive_education_fonts_url ) ) {
        wp_enqueue_style( 'interactive-education-google-fonts', esc_url( $interactive_education_fonts_url ), array(), null );
    }

    // Build CSS variables
    $interactive_education_custom_css  = ':root {';
    $interactive_education_custom_css .= '--global-color: ' . esc_attr( $interactive_education_color ) . ';';
    $interactive_education_custom_css .= '--global-colortwo: ' . esc_attr( $interactive_education_colortwo ) . ';';
    $interactive_education_custom_css .= '--global-colorthree: ' . esc_attr( $interactive_education_colorthree ) . ';';

    if ( ! empty( $interactive_education_font_body ) ) {
        $interactive_education_custom_css .= '--global-font-family: "' . esc_attr( $interactive_education_font_body ) . '", sans-serif !important;';
    }

    $interactive_education_custom_css .= '}';

    wp_add_inline_style( 'interactive-education-style', $interactive_education_custom_css );
}
add_action( 'wp_enqueue_scripts', 'interactive_education_dynamic_css', 99 );