<?php

if ( ! function_exists( 'dark_fitness_get_all_google_fonts' ) ) :
	/**
	 * Returns list of Google fonts.
	 */
	function dark_fitness_get_all_google_fonts() {
		$dark_fitness_webfonts_json   = get_template_directory() . '/inc/google-webfonts.json';
		$dark_fitness_fonts_json_data = file_get_contents( $dark_fitness_webfonts_json );

		$dark_fitness_all_fonts = json_decode( $dark_fitness_fonts_json_data, true );

		$dark_fitness_google_fonts = array();
		foreach ( $dark_fitness_all_fonts as $dark_fitness_font ) {
			$dark_fitness_google_fonts[ $dark_fitness_font['family'] ] = array(
				'family'   => $dark_fitness_font['family'],
				'variants' => $dark_fitness_font['variants'],
			);
		}
		return $dark_fitness_google_fonts;
	}
endif;

if ( ! function_exists( 'dark_fitness_get_all_google_font_families' ) ) :
	/**
	 * Returns list of Google font families.
	 */
	function dark_fitness_get_all_google_font_families() {
		$dark_fitness_google_fonts  = dark_fitness_get_all_google_fonts();
		$dark_fitness_font_families = array();
		foreach ( $dark_fitness_google_fonts as $dark_fitness_font ) {
			$dark_fitness_font_families[ $dark_fitness_font['family'] ] = $dark_fitness_font['family'];
		}
		return $dark_fitness_font_families;
	}
endif;

if ( ! function_exists( 'dark_fitness_get_fonts_url' ) ) :
	/**
	 * Return Google fonts URL.
	 */
	function dark_fitness_get_fonts_url() {
		$dark_fitness_fonts_url = '';
		$dark_fitness_fonts     = array();

		$dark_fitness_all_fonts = dark_fitness_get_all_google_fonts();

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

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

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

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

		$dark_fitness_fonts = array_unique( $dark_fitness_fonts );

		foreach ( $dark_fitness_fonts as $dark_fitness_font ) {
			$dark_fitness_variants      = $dark_fitness_all_fonts[ $dark_fitness_font ]['variants'];
			$dark_fitness_font_family[] = $dark_fitness_font . ':' . implode( ',', $dark_fitness_variants );
		}

		$dark_fitness_query_args = array(
			'family' => urlencode( implode( '|', $dark_fitness_font_family ) ),
		);

		if ( ! empty( $dark_fitness_font_family ) ) {
			$dark_fitness_fonts_url = add_query_arg( $dark_fitness_query_args, 'https://fonts.googleapis.com/css' );
		}

		return $dark_fitness_fonts_url;
	}
endif;