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

    $electrical_services_fonts_json_data = file_get_contents( $electrical_services_webfonts_json );
    if ( false === $electrical_services_fonts_json_data ) {
        return array();
    }

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

    $electrical_services_google_fonts = array();
    foreach ( $electrical_services_all_fonts as $electrical_services_font ) {
        $electrical_services_google_fonts[ $electrical_services_font['family'] ] = array(
            'family'   => $electrical_services_font['family'],
            'variants' => $electrical_services_font['variants'],
        );
    }
    return $electrical_services_google_fonts;
}

function electrical_services_get_all_google_font_families() {
    $electrical_services_google_fonts  = electrical_services_get_all_google_fonts();
    $electrical_services_font_families = array();
    foreach ( $electrical_services_google_fonts as $electrical_services_font ) {
        $electrical_services_font_families[ $electrical_services_font['family'] ] = $electrical_services_font['family'];
    }
    return $electrical_services_font_families;
}

function electrical_services_get_fonts_url() {
    $electrical_services_fonts_url = '';
    $electrical_services_fonts     = array();

    $electrical_services_all_fonts = electrical_services_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'electrical_services_site_title_font', 'Familjen Grotesk' ) ) ) {
        $electrical_services_fonts[] = get_theme_mod( 'electrical_services_site_title_font', 'Familjen Grotesk' );
    }

    if ( ! empty( get_theme_mod( 'electrical_services_site_description_font', 'Roboto Flex' ) ) ) {
        $electrical_services_fonts[] = get_theme_mod( 'electrical_services_site_description_font', 'Roboto Flex' );
    }

    if ( ! empty( get_theme_mod( 'electrical_services_header_font', 'Familjen Grotesk' ) ) ) {
        $electrical_services_fonts[] = get_theme_mod( 'electrical_services_header_font', 'Familjen Grotesk' );
    }

    if ( ! empty( get_theme_mod( 'electrical_services_content_font', 'Roboto Flex' ) ) ) {
        $electrical_services_fonts[] = get_theme_mod( 'electrical_services_content_font', 'Roboto Flex' );
    }

    $electrical_services_fonts = array_unique( $electrical_services_fonts );

    foreach ( $electrical_services_fonts as $electrical_services_font ) {
        $electrical_services_variants      = $electrical_services_all_fonts[ $electrical_services_font ]['variants'];
        $electrical_services_font_family[] = $electrical_services_font . ':' . implode( ',', $electrical_services_variants );
    }

    $electrical_services_query_args = array(
        'family' => urlencode( implode( '|', $electrical_services_font_family ) ),
    );

    if ( ! empty( $electrical_services_font_family ) ) {
        $electrical_services_fonts_url = add_query_arg( $electrical_services_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $electrical_services_fonts_url;
}