<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
define('WIDGET_DESC', esc_html__('Aggiungi widget qui.', 'nexo'));
require_once get_template_directory() . '/inc/customizer.php';

// Enqueue styles and scripts
function nexo_theme_enqueue_assets() {
    wp_enqueue_style( 'nexo-theme-fonts', get_template_directory_uri() . '/assets/css/stylesheet.min.css', array(), wp_get_theme()->get( 'Version' ) );
    
    wp_enqueue_style( 'nexo-theme-style', get_template_directory_uri() . '/style.min.css', array(), wp_get_theme()->get( 'Version' ) );
    
    wp_enqueue_script( 'nexo-theme-script', get_template_directory_uri() . '/assets/js/nexo.min.js', array(), wp_get_theme()->get( 'Version' ), true );

    wp_enqueue_script('nexo-theme-customizer', get_template_directory_uri() . '/assets/js/customizer.min.js', ['customize-preview', 'jquery'], null, true);

    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
}
add_action( 'wp_enqueue_scripts', 'nexo_theme_enqueue_assets' );

// Theme setup
function nexo_theme_setup() {
    // Make theme available for translation
    load_theme_textdomain( 'nexo', get_template_directory() . '/languages' );
    // Add default posts and comments RSS feed links to head
    add_theme_support( 'automatic-feed-links' );

    // Let WordPress manage the document title
    add_theme_support( 'title-tag' );

    // Enable support for Post Thumbnails on posts and pages
    add_theme_support( 'post-thumbnails' );

    // Enable support for  align-wide
    add_theme_support( "align-wide" );

    // Enable support for responsive embeds
    add_theme_support( 'responsive-embeds' );

    //enable support for style block gutember
    add_theme_support( 'wp-block-styles' );

    //enable support editor classic
    add_theme_support( 'editor-style' );

    // Enable support for custom spacing
    add_theme_support( 'custom-spacing' );

    // Enable support for custom units
    add_theme_support( 'custom-units' , 'px', 'em', 'rem', 'vh', 'vw' );


    // Set default thumbnail size
    set_post_thumbnail_size( 1200, 9999 );

    // Switch default core markup to output valid HTML5
    add_theme_support( 'html5', array(
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
    ) );

    // Add support for core custom logo
    add_theme_support( 'custom-logo', array(
        'height'      => 70,
        'width'       => 150,
        'flex-width'  => true,
        'flex-height' => true,
    ) );

    // Register nav menus
    register_nav_menus( array(
        'primary' => esc_html__( 'Primary Menu', 'nexo' ),
    ) );

    // block style
    register_block_style(
        'core/quote',
        array(
            'name'         => 'blue-quote',
            'label'        => __( 'Blue Quote', 'nexo' ),
            'is_default'   => true,
            'inline_style' => '.wp-block-quote.is-style-blue-quote { color: blue; }',
        )
    );

}
add_action( 'after_setup_theme', 'nexo_theme_setup' );

// Additional image sizes
function nexo_theme_add_image_sizes() {
    add_image_size( 'nexo-theme-custom-thumb', 0, 210, true );
}
add_action( 'after_setup_theme', 'nexo_theme_add_image_sizes' );


/*  Register editor stylesheet for the theme.
/* ------------------------------------ */
function nexo_theme_add_editor_styles() {
	add_editor_style( 'editor-style.css' );
}
add_action( 'admin_init', 'nexo_theme_add_editor_styles' );

// Register widget single
function nexo_theme_register_widget_single() {
    register_sidebar(array(
        'name'          => esc_html__('Sidebar Article', 'nexo'),
        'id'            => 'sidebar-1',
        'description'   => WIDGET_DESC,
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ));
}
add_action('widgets_init', 'nexo_theme_register_widget_single');

// Register widget footer
function nexo_theme_register_widget_footer() {
    register_sidebar(array(
        'name'          => esc_html__('Sidebar Footer', 'nexo'),
        'id'            => 'sidebar-2',
        'description'   => WIDGET_DESC,
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ));
}
add_action('widgets_init', 'nexo_theme_register_widget_footer');


// Register widget page
function nexo_theme_register_widget_page() {
    register_sidebar(array(
        'name'          => esc_html__('Sidebar page', 'nexo'),
        'id'            => 'sidebar-3',
        'description'   => WIDGET_DESC,
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ));
}
add_action('widgets_init', 'nexo_theme_register_widget_page');


// Register custom background
function nexo_theme_custom_background_setup() {
    $args = array(
        'default-color'          => 'ffffff', // Colore di sfondo predefinito
        'default-image'          => '',      // Immagine di sfondo predefinita (opzionale)
        'wp-head-callback'       => '_custom_background_cb', // Callback per generare il CSS
        'default-repeat'         => 'no-repeat',
        'default-position-x'     => 'center',
        'default-attachment'     => 'fixed',
    );
    add_theme_support( 'custom-background', $args );
}
add_action( 'after_setup_theme', 'nexo_theme_custom_background_setup' );

function nexo_customizer_css() {
    $header_bg_color = get_theme_mod('nexo_header_bg_color', '#101524'); // Colore di sfondo
    $header_text_color = get_theme_mod('nexo_header_text_color', '#ebebeb'); // Colore del testo
    $header_text_hover_color = get_theme_mod('nexo_header_text_hover_color', '#BFFF00'); // Colore hover del testo
    ?>
    <style>
        :root {
            --backgroundNavbar: <?php echo esc_attr($header_bg_color); ?>;
            --textColorNavbar: <?php echo esc_attr($header_text_color); ?>;
            --textHoverNavbar: <?php echo esc_attr($header_text_hover_color); ?>;
        }
    </style>
    <?php
}
add_action('wp_head', 'nexo_customizer_css');


function wp_trim_letters($text, $num_letters = 55, $trimmarker = '...') {
    $text = wp_strip_all_tags($text);
    return mb_strimwidth($text, 0, $num_letters, $trimmarker, 'UTF-8');
}

/**
 * Nexo_Walker_Nav_Menu
 * Versione ottimizzata per PHP 8.x e WP 6.x
 */
class Nexo_Walker_Nav_Menu extends Walker_Nav_Menu {
    public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
        // --- FIX PHP 8: Cast $args to object if it's an array ---
        $args = (object) $args;

        $classes = empty( $item->classes ) ? [] : (array) $item->classes;
        $has_children = in_array( 'menu-item-has-children', $classes );

        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
        $output .= $indent . '<li id="menu-item-' . $item->ID . '" class="' . esc_attr( implode( ' ', $classes ) ) . '">';

        $atts = [
            'title'  => ! empty( $item->attr_title ) ? $item->attr_title : '',
            'target' => ! empty( $item->target )     ? $item->target     : '',
            'rel'    => ! empty( $item->xfn )        ? $item->xfn        : '',
            'href'   => ! empty( $item->url )        ? $item->url        : '',
        ];

        $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
        $attributes = '';
        foreach ( $atts as $attr => $value ) {
            if ( ! empty( $value ) ) {
                $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
                $attributes .= ' ' . $attr . '="' . $value . '"';
            }
        }

        // Ora $args->before non genererà più il Warning
        $item_output = isset($args->before) ? $args->before : '';
        $item_output .= '<a' . $attributes . '>';
        $item_output .= (isset($args->link_before) ? $args->link_before : '') . apply_filters( 'the_title', $item->title, $item->ID ) . (isset($args->link_after) ? $args->link_after : '');
        $item_output .= '</a>';

        // Iniezione del Toggle Button se ci sono figli
        if ( $has_children ) {
            $item_output .= '<button class="sub-menu-toggle" aria-expanded="false" aria-label="' . esc_attr__( 'Espandi sottomenu', 'nexo' ) . '">';
            $item_output .= '<span class="screen-reader-text">' . esc_html__( 'Espandi', 'nexo' ) . '</span>';
            // Rimuovo l'icona i per usare il CSS ::after che abbiamo scritto prima, è più pulito
            $item_output .= '</button>';
        }

        $item_output .= isset($args->after) ? $args->after : '';

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}