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

    $florist_bouquet_fonts_json_data = file_get_contents( $florist_bouquet_webfonts_json );
    if ( false === $florist_bouquet_fonts_json_data ) {
        return array();
    }

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

    $florist_bouquet_google_fonts = array();
    foreach ( $florist_bouquet_all_fonts as $florist_bouquet_font ) {
        $florist_bouquet_google_fonts[ $florist_bouquet_font['family'] ] = array(
            'family'   => $florist_bouquet_font['family'],
            'variants' => $florist_bouquet_font['variants'],
        );
    }
    return $florist_bouquet_google_fonts;
}


function florist_bouquet_get_all_google_font_families() {
    $florist_bouquet_google_fonts  = florist_bouquet_get_all_google_fonts();
    $florist_bouquet_font_families = array();
    foreach ( $florist_bouquet_google_fonts as $florist_bouquet_font ) {
        $florist_bouquet_font_families[ $florist_bouquet_font['family'] ] = $florist_bouquet_font['family'];
    }
    return $florist_bouquet_font_families;
}

function florist_bouquet_get_fonts_url() {
    $florist_bouquet_fonts_url = '';
    $florist_bouquet_fonts     = array();

    $florist_bouquet_all_fonts = florist_bouquet_get_all_google_fonts();

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

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

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

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

    $florist_bouquet_fonts = array_unique( $florist_bouquet_fonts );

    foreach ( $florist_bouquet_fonts as $florist_bouquet_font ) {
        $florist_bouquet_variants      = $florist_bouquet_all_fonts[ $florist_bouquet_font ]['variants'];
        $florist_bouquet_font_family[] = $florist_bouquet_font . ':' . implode( ',', $florist_bouquet_variants );
    }

    $florist_bouquet_query_args = array(
        'family' => urlencode( implode( '|', $florist_bouquet_font_family ) ),
    );

    if ( ! empty( $florist_bouquet_font_family ) ) {
        $florist_bouquet_fonts_url = add_query_arg( $florist_bouquet_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $florist_bouquet_fonts_url;
}

