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

    $fast_food_joint_fonts_json_data = file_get_contents( $fast_food_joint_webfonts_json );
    if ( false === $fast_food_joint_fonts_json_data ) {
        return array();
    }

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

    $fast_food_joint_google_fonts = array();
    foreach ( $fast_food_joint_all_fonts as $fast_food_joint_font ) {
        $fast_food_joint_google_fonts[ $fast_food_joint_font['family'] ] = array(
            'family'   => $fast_food_joint_font['family'],
            'variants' => $fast_food_joint_font['variants'],
        );
    }
    return $fast_food_joint_google_fonts;
}


function fast_food_joint_get_all_google_font_families() {
    $fast_food_joint_google_fonts  = fast_food_joint_get_all_google_fonts();
    $fast_food_joint_font_families = array();
    foreach ( $fast_food_joint_google_fonts as $fast_food_joint_font ) {
        $fast_food_joint_font_families[ $fast_food_joint_font['family'] ] = $fast_food_joint_font['family'];
    }
    return $fast_food_joint_font_families;
}

function fast_food_joint_get_fonts_url() {
    $fast_food_joint_fonts_url = '';
    $fast_food_joint_fonts     = array();

    $fast_food_joint_all_fonts = fast_food_joint_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'fast_food_joint_site_title_font', 'Montserrat' ) ) ) {
        $fast_food_joint_fonts[] = get_theme_mod( 'fast_food_joint_site_title_font', 'Montserrat' );
    }

    if ( ! empty( get_theme_mod( 'fast_food_joint_site_description_font', 'Montserrat' ) ) ) {
        $fast_food_joint_fonts[] = get_theme_mod( 'fast_food_joint_site_description_font', 'Montserrat' );
    }

    if ( ! empty( get_theme_mod( 'fast_food_joint_header_font', 'Lemon' ) ) ) {
        $fast_food_joint_fonts[] = get_theme_mod( 'fast_food_joint_header_font', 'Lemon' );
    }

    if ( ! empty( get_theme_mod( 'fast_food_joint_content_font', 'Montserrat' ) ) ) {
        $fast_food_joint_fonts[] = get_theme_mod( 'fast_food_joint_content_font', 'Montserrat' );
    }

    $fast_food_joint_fonts = array_unique( $fast_food_joint_fonts );

    foreach ( $fast_food_joint_fonts as $fast_food_joint_font ) {
        $fast_food_joint_variants      = $fast_food_joint_all_fonts[ $fast_food_joint_font ]['variants'];
        $fast_food_joint_font_family[] = $fast_food_joint_font . ':' . implode( ',', $fast_food_joint_variants );
    }

    $fast_food_joint_query_args = array(
        'family' => urlencode( implode( '|', $fast_food_joint_font_family ) ),
    );

    if ( ! empty( $fast_food_joint_font_family ) ) {
        $fast_food_joint_fonts_url = add_query_arg( $fast_food_joint_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $fast_food_joint_fonts_url;
}

