<?php
/**
 * Particular Typography function for this theme
 *
 * Some of this functionality may eventually be superseded by essential features.
 *
 * @package Home Gardening
 */

 function pixel_photography_get_all_google_fonts() {
    $pixel_photography_webfonts_json = get_template_directory() . '/inc/typography-settings/google-webfonts.json';
    if ( ! file_exists( $pixel_photography_webfonts_json ) ) {
        return array();
    }

    $pixel_photography_fonts_json_data = file_get_contents( $pixel_photography_webfonts_json );
    if ( false === $pixel_photography_fonts_json_data ) {
        return array();
    }

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

    $pixel_photography_google_fonts = array();
    foreach ( $pixel_photography_all_fonts as $pixel_photography_font ) {
        $pixel_photography_google_fonts[ $pixel_photography_font['family'] ] = array(
            'family'   => $pixel_photography_font['family'],
            'variants' => $pixel_photography_font['variants'],
        );
    }
    return $pixel_photography_google_fonts;
}

function pixel_photography_get_all_google_font_families() {
    $pixel_photography_google_fonts  = pixel_photography_get_all_google_fonts();
    $pixel_photography_font_families = array(
        '' => esc_html__( 'Default (Theme Font Family)', 'pixel-photography' ),
    );

    foreach ( $pixel_photography_google_fonts as $pixel_photography_font ) {
        $pixel_photography_font_families[ $pixel_photography_font['family'] ] = $pixel_photography_font['family'];
    }

    return $pixel_photography_font_families;
}

function pixel_photography_get_fonts_url() {
    $pixel_photography_fonts_url = '';
    $pixel_photography_all_fonts = pixel_photography_get_all_google_fonts();
    $pixel_photography_selected_fonts = array();

    $font_body    = get_theme_mod( 'pixel_photography_global_font_setting', '' );
    $font_heading = get_theme_mod( 'pixel_photography_global_font_settingone', '' );

    if ( ! empty( $font_body ) && isset( $pixel_photography_all_fonts[ $font_body ] ) ) {
        $variants = $pixel_photography_all_fonts[ $font_body ]['variants'];
        $pixel_photography_selected_fonts[] = $font_body . ':' . implode( ',', $variants );
    }

    if ( ! empty( $font_heading ) && isset( $pixel_photography_all_fonts[ $font_heading ] ) && $font_heading !== $font_body ) {
        $variants = $pixel_photography_all_fonts[ $font_heading ]['variants'];
        $pixel_photography_selected_fonts[] = $font_heading . ':' . implode( ',', $variants );
    }

    if ( ! empty( $pixel_photography_selected_fonts ) ) {
        $query_args = array(
            'family' => urlencode( implode( '|', $pixel_photography_selected_fonts ) ),
        );
        $pixel_photography_fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
    }

    return $pixel_photography_fonts_url;
}

function pixel_photography_sanitize_google_fonts( $pixel_photography_input, $pixel_photography_setting ) {
    $pixel_photography_choices = $pixel_photography_setting->manager->get_control( $pixel_photography_setting->id )->choices;
    return ( array_key_exists( $pixel_photography_input, $pixel_photography_choices ) ? $pixel_photography_input : $pixel_photography_setting->default );
}

function pixel_photography_dynamic_css() {
    $pixel_photography_color         = get_theme_mod( 'pixel_photography_global_color', '#bc27f0' );
    $pixel_photography_font_body     = get_theme_mod( 'pixel_photography_global_font_setting', '' );
    $pixel_photography_font_heading  = get_theme_mod( 'pixel_photography_global_font_settingone', '' );

    // Enqueue Google Fonts if needed
    $pixel_photography_fonts_url = pixel_photography_get_fonts_url();
    if ( ! empty( $pixel_photography_fonts_url ) ) {
        wp_enqueue_style( 'pixel-photography-google-fonts', esc_url( $pixel_photography_fonts_url ), array(), null );
    }

    // Build CSS variables
    $pixel_photography_custom_css  = ':root {';
    $pixel_photography_custom_css .= '--global-color: ' . esc_attr( $pixel_photography_color ) . ';';

    if ( ! empty( $pixel_photography_font_body ) ) {
        $pixel_photography_custom_css .= '--global-font-family: "' . esc_attr( $pixel_photography_font_body ) . '", sans-serif !important;';
    }

    if ( ! empty( $pixel_photography_font_heading ) ) {
        $pixel_photography_custom_css .= '--global-font-familyone: "' . esc_attr( $pixel_photography_font_heading ) . '", sans-serif !important;';
    }

    $pixel_photography_custom_css .= '}';

    wp_add_inline_style( 'pixel-photography-style', $pixel_photography_custom_css );
}
add_action( 'wp_enqueue_scripts', 'pixel_photography_dynamic_css', 99 );