<?php

// Exit if accessed directly
if (!defined('ABSPATH'))
    exit;

/**
 * Enqueue scripts and styles for the front end.
 */

function neatnote_enqueue_fonts()
{
    // Get the selected font from the Customizer.
    $selected_font = get_theme_mod('neatnote_body_fonts', '');

    if (! empty($selected_font)) {
        // Enqueue Google Fonts if the selected font is available on Google Fonts.
        // Google Fonts are licensed under the SIL Open Font License (OFL)
        // License: https://scripts.sil.org/OFL
        wp_enqueue_style('google-font', 'https://fonts.googleapis.com/css2?family=' . esc_attr($selected_font) . '&display=swap');
    }
}
add_action('wp_enqueue_scripts', 'neatnote_enqueue_fonts');

function neatnote_public_scripts()
{

    // Enqueue Font Awesome CSS for icons
    wp_enqueue_style(
        'font-awesome',
        get_template_directory_uri() . '/assets/fonts/font-awesome/css/all.min.css',
        array(),
        NEATNOTE_VERSION,
        false
    );

    // Enqueue custom plugin CSS
    wp_enqueue_style(
        'aos-plugin-css',
        get_template_directory_uri() . '/assets/css/neatnote-aos.css',
        array(),
        NEATNOTE_VERSION,
        false
    );

    // Load the main theme stylesheet
    wp_enqueue_style(
        'neatnote-style',
        get_stylesheet_uri(),
        array(),
        NEATNOTE_VERSION,
        false
    );

    
    // Enqueue public script for neatnote plugin functionality
    // wp_enqueue_script('jquery');
    // If on a single post and comments are open, enqueue the comment reply script
    if (is_singular() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }

    wp_enqueue_script(
        'aos-plugin',
        get_template_directory_uri() . '/assets/js/neatnote-plugin-js/neatnote-aos-plugin.js',
        array( 'jquery' ),
        NEATNOTE_VERSION,
        true
    );

    // Enqueue custom public script for other front-end functionality
    wp_enqueue_script(
        'neatnote-public-script',
        get_template_directory_uri() . '/assets/js/neatnote-script.js',
        array( 'jquery' ),
        NEATNOTE_VERSION,
        true
    );
    // Enqueue ustomizer-preview script
    if (! function_exists('neatnote_customize_preview_js')) :
        function neatnote_customize_preview_js()
        {
            wp_enqueue_script(
                'neatnote-customizer-preview',
                get_template_directory_uri() . '/assets/js/customizer-preview.js',
                array('customize-preview'),
                NEATNOTE_VERSION,
                true
            );
        }
        add_action('customize_preview_init', 'neatnote_customize_preview_js');

    endif;

    // Enqueue navigation script for other front-end functionality
    wp_enqueue_script(
        'neatnote-navigation-script',
        get_template_directory_uri() . '/assets/js/neatnote-navigation.js',
        array( 'jquery' ),
        NEATNOTE_VERSION,
        true
    );
}

// Add action to load scripts and styles for the front end
add_action('wp_enqueue_scripts', 'neatnote_public_scripts');

if (!function_exists('neatnote_add_dynamic_root_css_variables')) :
    function neatnote_add_dynamic_root_css_variables()
    {
        $body_fonts = esc_attr(str_replace('+', ' ', get_theme_mod('neatnote_body_fonts', '')));
        $css = ':root {';
        $css .= "--neatnote-body-fonts: {$body_fonts}, serif;";
        $css .= '--primary: ' . esc_attr(get_theme_mod('neatnote_primary', '#072c50')) . ';';
        $css .= '--secondary: ' . esc_attr(get_theme_mod('neatnote_secondary', '#d2d6dc')) . ';';
        $css .= '--body-text-color: ' . esc_attr(get_theme_mod('neatnote_body_text_color', '#2d5c81')) . ';';
        $css .= '--accent-color: ' . esc_attr(get_theme_mod('neatnote_accent_color', '#174474')) . ';';
        $css .= '--accent-hover-color: ' . esc_attr(get_theme_mod('neatnote_accent_hover_color', '#2d5c81')) . ';';
        $css .= '--header-bg-color: ' . esc_attr(get_theme_mod('neatnote_header_background_color', '#f1f7fc')) . ';';
        $css .= '--sticky-header-bg-color: ' . esc_attr(get_theme_mod('neatnote_sticky_header_background_color', '#F1F7FC')) . ';';
        $css .= '--header-menu-color: ' . esc_attr(get_theme_mod('neatnote_header_menu_color', '#072c50')) . ';';
        $css .= '--header-menu-hover-color: ' . esc_attr(get_theme_mod('neatnote_header_menu_hover_color', '#2d5c81')) . ';';
        $css .= '--footer-bg-color: ' . esc_attr(get_theme_mod('neatnote_footer_background_color', '#F1F7FC')) . ';';
        $css .= '--footer-description-font-color: ' . esc_attr(get_theme_mod('neatnote_footer_description_font_color', '#072c50')) . ';';
        $css .= '--footer-menu-color: ' . esc_attr(get_theme_mod('neatnote_footer_menu_color', '#072c50')) . ';';
        $css .= '--footer-menu-hover-color: ' . esc_attr(get_theme_mod('neatnote_footer_menu_hover_color', '#2d5c81')) . ';';
        $css .= '--footer-social-media-color: ' . esc_attr(get_theme_mod('neatnote_footer_social_media_color', '#072c50')) . ';';
        $css .= '--footer-social-media-hover-color: ' . esc_attr(get_theme_mod('neatnote_footer_social_media_hover_color', '#2d5c81')) . ';';
        $css .= '--theme-white-color: ' . esc_attr(get_theme_mod('neatnote_theme_white_color', '#ffffff')) . ';';
        $css .= '--menu-dropdown-bg-color: ' . esc_attr(get_theme_mod('neatnote_header_sub_menu_dropdown_bg_color', '#F1F7FC')) . ';';
        // Single blog
        $css .= '--neatnote-single-blog-font-color: ' . esc_attr(get_theme_mod('neatnote_font_color', '#2d5c81')) . ';';
        $css .= '--neatnote-single-blog-social-media-color: ' . esc_attr(get_theme_mod('neatnote_social_media_color', '#0e5f9ff0')) . ';';
        $css .= '--neatnote-single-blog-social-media-hover-color: ' . esc_attr(get_theme_mod('neatnote_social_media_hover_color', '#2d5c81')) . ';';
        $css .= '--neatnote-single-blog-list-color: ' . esc_attr(get_theme_mod('neatnote_post_content_list_color', '#072c50')) . ';';
        $css .= '}';
        wp_add_inline_style('neatnote-style', $css);
    }
    add_action('wp_enqueue_scripts', 'neatnote_add_dynamic_root_css_variables');
endif;
