<?php

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

/**
 * Remove author link from the comment profile.
 * This replaces the author's link with a plain text name wrapped in a span element for better security and control.
 */
if (!function_exists('neatnote_remove_comment_author_link')) :
    function neatnote_remove_comment_author_link($return, $author, $comment_ID)
    {
        $author_name = get_comment_author($comment_ID);
        return '<span class="fn">' . esc_html($author_name) . '</span>';
    }
    add_filter('get_comment_author_link', 'neatnote_remove_comment_author_link', 10, 3);
endif;

/**
 * Enqueue the color picker from 'wp-color-picker' for the admin settings page.
 */
if (!function_exists('neatnote_theme_enqueue_color_picker')) :
    add_action('admin_enqueue_scripts', 'neatnote_theme_enqueue_color_picker');
    function neatnote_theme_enqueue_color_picker($hook_suffix)
    {
        // Only load the color picker on the Neatnote theme settings page.
        if ($hook_suffix === 'toplevel_page_neatnote-theme') {
            wp_enqueue_style('wp-color-picker');
            wp_enqueue_script('wp-color-picker');
            wp_enqueue_media();
        }
    }
endif;

/**
 * Modified pagination function for the Neatnote Theme.
 * Adds SVG icons for next and previous buttons and customizes the appearance of the pagination.
 */
if (!function_exists('neatnote_the_posts_navigation')) :
    function neatnote_the_posts_navigation()
    {
        $allowed_html = array(
            'i' => array('class' => array()) // Allow <i> tag with class attribute
        );

        $neatnote_prev_icon = wp_kses('<i class="fa-solid fa-chevron-left"></i>', $allowed_html);
        $neatnote_next_icon = wp_kses('<i class="fa-solid fa-chevron-right"></i>', $allowed_html);

        the_posts_pagination(
            array(
                'mid_size' => 2,
                'prev_text' => sprintf('%s <span class="neatnote-nav-prev-text">%s</span>', $neatnote_prev_icon, esc_html_x('Previous', 'Pagination', 'neatnote')),
                'next_text' => sprintf('<span class="neatnote-nav-next-text">%s</span> %s', esc_html_x('Next', 'Pagination', 'neatnote'), $neatnote_next_icon),
                'screen_reader_text' => esc_html_x('Posts navigation', 'Screen reader text', 'neatnote'),
            )
        );
    }
endif;


/**
 * Register sidebars for the Neatnote Theme.
 * Adds multiple sidebars for different areas of the website, each with custom widget wrappers.
 */
if (! function_exists('neatnote_register_sidebars')) :
    function neatnote_register_sidebars()
    {
        // Registering Sidebars
        register_sidebar(array(
            'name'          => esc_html__('Neatnote Main Sidebar', 'neatnote'),
            'id'            => 'neatnote-main-sidebar',
            'description'   => esc_html__('Widgets in this area will be shown on all posts and pages.', 'neatnote'),
            'before_widget' => '<div id="neatnote-widget-%1$s" class="neatnote-widget %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h3 class="neatnote-widget-title">',
            'after_title'   => '</h3>',
        ));

        register_sidebar(array(
            'name'          => esc_html__('Sidebar for Pages', 'neatnote'),
            'id'            => 'neatnote-sidebar-for-pages',
            'description'   => esc_html__('Sidebar for Pages.', 'neatnote'),
            'before_widget' => '<div id="neatnote-widget-%1$s" class="neatnote-widget %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h2 class="neatnote-widget-title">',
            'after_title'   => '</h2>',
        ));

        register_sidebar(array(
            'name'          => esc_html__('Sidebar for Single Page', 'neatnote'),
            'id'            => 'neatnote-sidebar-single-page',
            'description'   => esc_html__('Sidebar for Single Page.', 'neatnote'),
            'before_widget' => '<div id="neatnote-widget-%1$s" class="neatnote-widget %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h2 class="neatnote-widget-title">',
            'after_title'   => '</h2>',
        ));

        register_sidebar(array(
            'name'          => esc_html__('Sidebar for Blog Listing Page', 'neatnote'),
            'id'            => 'neatnote-sidebar-blog-page',
            'description'   => esc_html__('Sidebar for Blog Listing Page.', 'neatnote'),
            'before_widget' => '<div id="neatnote-widget-%1$s" class="neatnote-widget %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h2 class="neatnote-widget-title">',
            'after_title'   => '</h2>',
        ));
    }
    add_action('widgets_init', 'neatnote_register_sidebars');
endif;


/**
 * Calculate estimated read time based on average reading speed.
 *
 * @param int $post_id Post ID.
 * @return string Estimated read time in minutes.
 */
if (! function_exists('neatnote_theme_calculate_read_time')) :
    function neatnote_theme_calculate_read_time($post_id)
    {
        $content = get_post_field('post_content', $post_id);
        $word_count = str_word_count(strip_tags($content));
        $words_per_minute = 200;
        $read_time = ceil($word_count / $words_per_minute);
        return $read_time;
    }
endif;


/**
 * Function to display the estimated read time for a post.
 * 
 * This function fetches the current post ID, calculates the read time based on word count, 
 * and outputs the read time in minutes (rounded up).
 */
if (! function_exists('neatnote_theme_display_read_time')) :
    function neatnote_theme_display_read_time()
    {
        // Get the current post ID
        $post_id = get_the_ID();

        // Calculate the estimated read time for the post
        $read_time = neatnote_theme_calculate_read_time($post_id);

        // Output the estimated read time in a readable format (e.g., "5 min read")
        echo sprintf(esc_html__(' %s min read', 'neatnote'), esc_html($read_time));
    }
endif;

/**
 *  Setup the social menu with icons and create it if it doesn't exist.
 * This function adds a custom "Social Menu" with Font Awesome SVG icons 
 * for the specified social media links and assigns it to a menu location.
 * Create a "Social Menu" with Font Awesome icons for social media links.
 * This function will create a social menu and populate it with predefined links (Facebook, Instagram, X, LinkedIn)
 * and their respective SVG icons, then assign this menu to a theme location.
 */


if (! class_exists('Neatnote_Social_Menu_Walker')) :
    class Neatnote_Social_Menu_Walker extends Walker_Nav_Menu
    {
        public function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
        {
            $output .= '<li class="neatnote-menu-item">';
            $output .= '<a href="' . esc_url($item->url) . '" target="_blank">';
            $output .= wp_kses_post($item->title);
            $output .= '</a></li>';
        }
    }
endif;

/**
 * modified wp_nav menu for Neatnote theme
 *
 * @param array $classes The CSS classes applied to the menu item's `<li>`.
 * @param WP_Post $item The current menu item.
 * @param stdClass $args An object of wp_nav_menu() arguments.
 * @return array Updated array of CSS classes.
 * 
 */
if (! class_exists('Neatnote_main_menu_Walker_Nav_Menu')) :
    class Neatnote_main_menu_Walker_Nav_Menu extends Walker_Nav_Menu
    {
        function start_lvl(&$output, $depth = 0, $args = null)
        {
            $output .= '<ul class="neatnote-sub-menu">';
        }
        function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
        {
            $classes = empty($item->classes) ? array() : (array) $item->classes;
            $classes[] = 'neatnote-menu-item-' . $item->ID;
            // Add 'active' class for current menu item or parent/ancestor items
            if (in_array('neatnote-current-menu-item', $classes) || in_array('neatnote-current-menu-parent', $classes) || in_array('current-menu-ancestor', $classes)) {
                $classes[] = 'neatnote-active';
            }
            $has_children = in_array('menu-item-has-children', $classes);
            $output .= '<li id="neatnote-menu-item-' . $item->ID . '" class="' . implode(' ', $classes) . '">';
            $output .= '<div class="neatnote-menudrawer"><a href="' . esc_url($item->url) . '">' . $item->title . '</a>';
            if ($has_children) {
                $icon = $depth === 0 ? 'fa-angle-down' : 'fa-angle-right';
                $output .= '<button type="button" aria-expanded="false" class="neatnote-ToggleButton"><span><i class="fa-solid ' . $icon . '"></i></span></button></div>';
            }
        }

        // End level (Submenu)
        function end_lvl(&$output, $depth = 0, $args = null)
        {
            $output .= '</ul>';
        }
        // End element (Menu Item)
        function end_el(&$output, $item, $depth = 0, $args = null, $id = 0)
        {
            $output .= '</li>';
        }
    }
endif;

// font sanitization.
if (!function_exists('neatnote_sanitize_google_fonts')) :
    function neatnote_sanitize_google_fonts($input, $setting)
    {
        // Get list of choices from the control associated with the setting.
        $choices = $setting->manager->get_control($setting->id)->choices;
        // If the input is a valid key, return it; otherwise, return the default.
        return (array_key_exists($input, $choices) ? $input : $setting->default);
    }
endif;

