<?php
function wheel_alignment_service_get_all_google_fonts() {
    $wheel_alignment_service_webfonts_json = get_template_directory() . '/theme-library/google-webfonts.json';
    if ( ! file_exists( $wheel_alignment_service_webfonts_json ) ) {
        return array();
    }

    $wheel_alignment_service_fonts_json_data = file_get_contents( $wheel_alignment_service_webfonts_json );
    if ( false === $wheel_alignment_service_fonts_json_data ) {
        return array();
    }

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

    $wheel_alignment_service_google_fonts = array();
    foreach ( $wheel_alignment_service_all_fonts as $wheel_alignment_service_font ) {
        $wheel_alignment_service_google_fonts[ $wheel_alignment_service_font['family'] ] = array(
            'family'   => $wheel_alignment_service_font['family'],
            'variants' => $wheel_alignment_service_font['variants'],
        );
    }
    return $wheel_alignment_service_google_fonts;
}


function wheel_alignment_service_get_all_google_font_families() {
    $wheel_alignment_service_google_fonts  = wheel_alignment_service_get_all_google_fonts();
    $wheel_alignment_service_font_families = array();
    foreach ( $wheel_alignment_service_google_fonts as $wheel_alignment_service_font ) {
        $wheel_alignment_service_font_families[ $wheel_alignment_service_font['family'] ] = $wheel_alignment_service_font['family'];
    }
    return $wheel_alignment_service_font_families;
}

function wheel_alignment_service_get_fonts_url() {
    $wheel_alignment_service_fonts_url = '';
    $wheel_alignment_service_fonts     = array();

    $wheel_alignment_service_all_fonts = wheel_alignment_service_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'wheel_alignment_service_site_title_font', 'Oxanium' ) ) ) {
        $wheel_alignment_service_fonts[] = get_theme_mod( 'wheel_alignment_service_site_title_font', 'Oxanium' );
    }

    if ( ! empty( get_theme_mod( 'wheel_alignment_service_site_description_font', 'Raleway' ) ) ) {
        $wheel_alignment_service_fonts[] = get_theme_mod( 'wheel_alignment_service_site_description_font', 'Raleway' );
    }

    if ( ! empty( get_theme_mod( 'wheel_alignment_service_header_font', 'Epilogue' ) ) ) {
        $wheel_alignment_service_fonts[] = get_theme_mod( 'wheel_alignment_service_header_font', 'Epilogue' );
    }

    if ( ! empty( get_theme_mod( 'wheel_alignment_service_content_font', 'Raleway' ) ) ) {
        $wheel_alignment_service_fonts[] = get_theme_mod( 'wheel_alignment_service_content_font', 'Raleway' );
    }

    $wheel_alignment_service_fonts = array_unique( $wheel_alignment_service_fonts );

    foreach ( $wheel_alignment_service_fonts as $wheel_alignment_service_font ) {
        $wheel_alignment_service_variants      = $wheel_alignment_service_all_fonts[ $wheel_alignment_service_font ]['variants'];
        $wheel_alignment_service_font_family[] = $wheel_alignment_service_font . ':' . implode( ',', $wheel_alignment_service_variants );
    }

    $wheel_alignment_service_query_args = array(
        'family' => urlencode( implode( '|', $wheel_alignment_service_font_family ) ),
    );

    if ( ! empty( $wheel_alignment_service_font_family ) ) {
        $wheel_alignment_service_fonts_url = add_query_arg( $wheel_alignment_service_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $wheel_alignment_service_fonts_url;
}