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

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

    $pixel_hair_spa_fonts_json_data = file_get_contents( $pixel_hair_spa_webfonts_json );
    if ( false === $pixel_hair_spa_fonts_json_data ) {
        return array();
    }

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

    $pixel_hair_spa_google_fonts = array();
    foreach ( $pixel_hair_spa_all_fonts as $pixel_hair_spa_font ) {
        $pixel_hair_spa_google_fonts[ $pixel_hair_spa_font['family'] ] = array(
            'family'   => $pixel_hair_spa_font['family'],
            'variants' => $pixel_hair_spa_font['variants'],
        );
    }
    return $pixel_hair_spa_google_fonts;
}


function pixel_hair_spa_get_all_google_font_families() {
    $pixel_hair_spa_google_fonts  = pixel_hair_spa_get_all_google_fonts();
    $pixel_hair_spa_font_families = array(
        '' => esc_html__( 'Default (Theme Font Family)', 'pixel-hair-spa' ),
    );

    foreach ( $pixel_hair_spa_google_fonts as $pixel_hair_spa_font ) {
        $pixel_hair_spa_font_families[ $pixel_hair_spa_font['family'] ] = $pixel_hair_spa_font['family'];
    }

    return $pixel_hair_spa_font_families;
}


function pixel_hair_spa_get_fonts_url() {
    $pixel_hair_spa_fonts_url = '';
    $pixel_hair_spa_fonts     = array();

    $pixel_hair_spa_all_fonts = pixel_hair_spa_get_all_google_fonts();
    $pixel_hair_spa_selected_font = get_theme_mod( 'pixel_hair_spa_global_font_setting', '' );

    if ( ! empty( $pixel_hair_spa_selected_font ) && isset( $pixel_hair_spa_all_fonts[ $pixel_hair_spa_selected_font ] ) ) {
        $pixel_hair_spa_variants = $pixel_hair_spa_all_fonts[ $pixel_hair_spa_selected_font ]['variants'];
        $pixel_hair_spa_font_family[] = $pixel_hair_spa_selected_font . ':' . implode( ',', $pixel_hair_spa_variants );

        $pixel_hair_spa_query_args = array(
            'family' => urlencode( implode( '|', $pixel_hair_spa_font_family ) ),
        );

        $pixel_hair_spa_fonts_url = add_query_arg( $pixel_hair_spa_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $pixel_hair_spa_fonts_url;
}

function pixel_hair_spa_sanitize_google_fonts( $pixel_hair_spa_input, $pixel_hair_spa_setting ) {
    $pixel_hair_spa_choices = $pixel_hair_spa_setting->manager->get_control( $pixel_hair_spa_setting->id )->choices;
    return ( array_key_exists( $pixel_hair_spa_input, $pixel_hair_spa_choices ) ? $pixel_hair_spa_input : $pixel_hair_spa_setting->default );
}

function pixel_hair_spa_dynamic_css() {
    $pixel_hair_spa_color       = get_theme_mod( 'pixel_hair_spa_global_color', '#6f5c9b' );
    $pixel_hair_spa_font_family = get_theme_mod( 'pixel_hair_spa_global_font_setting', '' );

    // Enqueue Google Fonts if font is selected
    $pixel_hair_spa_fonts_url = pixel_hair_spa_get_fonts_url();
    if ( ! empty( $pixel_hair_spa_fonts_url ) ) {
        wp_enqueue_style( 'pixel-hair-spa-google-fonts', esc_url( $pixel_hair_spa_fonts_url ), array(), null );
    }

    $pixel_hair_spa_custom_css = ':root { --global-color: ' . esc_attr( $pixel_hair_spa_color ) . '; }';

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

    wp_add_inline_style( 'pixel-hair-spa-style', $pixel_hair_spa_custom_css );
}
add_action( 'wp_enqueue_scripts', 'pixel_hair_spa_dynamic_css', 99 );