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

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

    $home_gardening_fonts_json_data = file_get_contents( $home_gardening_webfonts_json );
    if ( false === $home_gardening_fonts_json_data ) {
        return array();
    }

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

    $home_gardening_google_fonts = array();
    foreach ( $home_gardening_all_fonts as $home_gardening_font ) {
        $home_gardening_google_fonts[ $home_gardening_font['family'] ] = array(
            'family'   => $home_gardening_font['family'],
            'variants' => $home_gardening_font['variants'],
        );
    }
    return $home_gardening_google_fonts;
}

function home_gardening_get_all_google_font_families() {
    $home_gardening_google_fonts  = home_gardening_get_all_google_fonts();
    $home_gardening_font_families = array(
        '' => esc_html__( 'Default (Theme Font Family)', 'home-gardening' ),
    );

    foreach ( $home_gardening_google_fonts as $home_gardening_font ) {
        $home_gardening_font_families[ $home_gardening_font['family'] ] = $home_gardening_font['family'];
    }

    return $home_gardening_font_families;
}

function home_gardening_get_fonts_url() {
    $home_gardening_fonts_url = '';
    $home_gardening_all_fonts = home_gardening_get_all_google_fonts();
    $home_gardening_selected_fonts = array();

    $font_body    = get_theme_mod( 'home_gardening_global_font_setting', '' );
    $font_heading = get_theme_mod( 'home_gardening_global_font_settingone', '' );

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

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

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

    return $home_gardening_fonts_url;
}

function home_gardening_sanitize_google_fonts( $home_gardening_input, $home_gardening_setting ) {
    $home_gardening_choices = $home_gardening_setting->manager->get_control( $home_gardening_setting->id )->choices;
    return ( array_key_exists( $home_gardening_input, $home_gardening_choices ) ? $home_gardening_input : $home_gardening_setting->default );
}

function home_gardening_dynamic_css() {
    $home_gardening_color         = get_theme_mod( 'home_gardening_global_color', '#a5c32d' );
    $home_gardening_colortwo      = get_theme_mod( 'home_gardening_global_colortwo', '#2a7d2e' );
    $home_gardening_font_body     = get_theme_mod( 'home_gardening_global_font_setting', '' );
    $home_gardening_font_heading  = get_theme_mod( 'home_gardening_global_font_settingone', '' );

    // Enqueue Google Fonts if needed
    $home_gardening_fonts_url = home_gardening_get_fonts_url();
    if ( ! empty( $home_gardening_fonts_url ) ) {
        wp_enqueue_style( 'home-gardening-google-fonts', esc_url( $home_gardening_fonts_url ), array(), null );
    }

    // Build CSS variables
    $home_gardening_custom_css  = ':root {';
    $home_gardening_custom_css .= '--global-color: ' . esc_attr( $home_gardening_color ) . ';';
    $home_gardening_custom_css .= '--global-colortwo: ' . esc_attr( $home_gardening_colortwo ) . ';';

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

    if ( ! empty( $home_gardening_font_heading ) ) {
        $home_gardening_custom_css .= '--global-font-familyone: "' . esc_attr( $home_gardening_font_heading ) . '", sans-serif !important;';
    }

    $home_gardening_custom_css .= '}';

    wp_add_inline_style( 'home-gardening-style', $home_gardening_custom_css );
}
add_action( 'wp_enqueue_scripts', 'home_gardening_dynamic_css', 99 );