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

    $appointment_management_fonts_json_data = file_get_contents( $appointment_management_webfonts_json );
    if ( false === $appointment_management_fonts_json_data ) {
        return array();
    }

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

    $appointment_management_google_fonts = array();
    foreach ( $appointment_management_all_fonts as $appointment_management_font ) {
        $appointment_management_google_fonts[ $appointment_management_font['family'] ] = array(
            'family'   => $appointment_management_font['family'],
            'variants' => $appointment_management_font['variants'],
        );
    }
    return $appointment_management_google_fonts;
}


function appointment_management_get_all_google_font_families() {
    $appointment_management_google_fonts  = appointment_management_get_all_google_fonts();
    $appointment_management_font_families = array();
    foreach ( $appointment_management_google_fonts as $appointment_management_font ) {
        $appointment_management_font_families[ $appointment_management_font['family'] ] = $appointment_management_font['family'];
    }
    return $appointment_management_font_families;
}

function appointment_management_get_fonts_url() {
    $appointment_management_fonts_url = '';
    $appointment_management_fonts     = array();

    $appointment_management_all_fonts = appointment_management_get_all_google_fonts();

    if ( ! empty( get_theme_mod( 'appointment_management_site_title_font', 'Open Sans' ) ) ) {
        $appointment_management_fonts[] = get_theme_mod( 'appointment_management_site_title_font', 'Open Sans' );
    }

    if ( ! empty( get_theme_mod( 'appointment_management_site_description_font', 'Open Sans' ) ) ) {
        $appointment_management_fonts[] = get_theme_mod( 'appointment_management_site_description_font', 'Open Sans' );
    }

    if ( ! empty( get_theme_mod( 'appointment_management_header_font', 'Open Sans' ) ) ) {
        $appointment_management_fonts[] = get_theme_mod( 'appointment_management_header_font', 'Open Sans' );
    }

    if ( ! empty( get_theme_mod( 'appointment_management_content_font', 'Open Sans' ) ) ) {
        $appointment_management_fonts[] = get_theme_mod( 'appointment_management_content_font', 'Open Sans' );
    }

    $appointment_management_fonts = array_unique( $appointment_management_fonts );

    foreach ( $appointment_management_fonts as $appointment_management_font ) {
        $appointment_management_variants      = $appointment_management_all_fonts[ $appointment_management_font ]['variants'];
        $appointment_management_font_family[] = $appointment_management_font . ':' . implode( ',', $appointment_management_variants );
    }

    $appointment_management_query_args = array(
        'family' => urlencode( implode( '|', $appointment_management_font_family ) ),
    );

    if ( ! empty( $appointment_management_font_family ) ) {
        $appointment_management_fonts_url = add_query_arg( $appointment_management_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $appointment_management_fonts_url;
}