<?php
define('NX_THEMES_FREE', 'nxt-event-planner');

require_once get_template_directory() . '/inc/nxt-customizer.php';
require_once get_template_directory() . '/inc/nxt-event-planner-admin-banner.php';

function nxt_event_planner_scripts() {
    $nxt_event_planner_theme = wp_get_theme();
    $nxt_event_planner_version = $nxt_event_planner_theme->get( 'Version' );

    // Enqueue main stylesheet with cache-busting
    wp_enqueue_style( 'nxt-event-planner-style', get_stylesheet_uri(), array(), filemtime( get_stylesheet_directory() . '/style.css' ) );
    wp_add_inline_style( 'nxt-event-planner-style', 'body{opacity:0;transition:opacity 0.3s ease-in;}' );

    wp_enqueue_script( 'nxt-event-planner-navigation', get_template_directory_uri() . '/js/nxt-event-planner-navigation.js', array(), $nxt_event_planner_version, true );
    wp_localize_script( 'nxt-event-planner-navigation', 'nxtEventPlannerNavigation', array(
        'expand'  => __( 'Expand submenu', 'nxt-event-planner' ),
        'collapse'=> __( 'Collapse submenu', 'nxt-event-planner' ),
        'toggle'  => __( 'Toggle submenu', 'nxt-event-planner' ),
    ) );
    wp_enqueue_script( 'nxt-event-planner-dynamic-bg', get_template_directory_uri() . '/js/nxt-event-planner-dynamic-bg.js', array(), $nxt_event_planner_version, true );

    if ( get_theme_mod( 'nxt_event_planner_loader_enable', true ) ) {
        wp_enqueue_script( 'nxt-event-planner-loader', get_template_directory_uri() . '/js/nxt-event-planner-loader.js', array(), $nxt_event_planner_version, true );
    }

    if ( is_home() || is_front_page() || is_archive() || is_search() ) {
        wp_enqueue_script( 'nxt-event-planner-card-animations', get_template_directory_uri() . '/js/nxt-event-planner-card-animations.js', array(), $nxt_event_planner_version, true );
    }

    if ( ( is_single() || is_page() ) && ( have_comments() || comments_open() ) ) {
        wp_enqueue_script( 'comment-reply' );
        wp_enqueue_script( 'nxt-event-planner-comment-animations', get_template_directory_uri() . '/js/nxt-event-planner-comment-animations.js', array(), $nxt_event_planner_version, true );
    }
}
add_action( 'wp_enqueue_scripts', 'nxt_event_planner_scripts' );

function nxt_event_planner_add_fouc_script() {
    wp_add_inline_script( 'nxt-event-planner-navigation', 'document.addEventListener("DOMContentLoaded",function(){document.body.style.opacity="1";});' );
}
add_action( 'wp_enqueue_scripts', 'nxt_event_planner_add_fouc_script' );

function nxt_event_planner_setup() {
    add_theme_support( 'title-tag' );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption' ) );
    register_nav_menus( array( 'primary' => __( 'Primary Menu', 'nxt-event-planner' ) ) );
    add_theme_support( 'wp-block-styles' );
    add_theme_support( 'responsive-embeds' );
    add_theme_support( 'custom-logo' );
    add_theme_support( 'align-wide' );
    add_theme_support( 'custom-background' );
    add_editor_style();

    // Add theme support for custom-header to enable the UI in the Customizer.
    // The actual output is handled by `nxt_event_planner_custom_header_style` hooked to wp_head.
    add_theme_support( 'custom-header', array(
        'default-background-color' => 'ffffff',
        'default-text-color'     => '152028',
        'wp_head_callback'       => '__return_empty_string', // We use our own callback hooked to wp_head
    ) );
}
add_action( 'after_setup_theme', 'nxt_event_planner_setup' );


function nxt_event_planner_widgets_init() {
    register_sidebar( array(
        'name'          => __( 'Homepage Sidebar', 'nxt-event-planner' ),
        'id'            => 'sidebar-1',
        'description'   => __( 'Add widgets here to appear in your sidebar.', 'nxt-event-planner' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );

    for ( $nxt_event_planner_fi = 1; $nxt_event_planner_fi <= 4; $nxt_event_planner_fi++ ) {
        register_sidebar( array(
            /* translators: %d: footer column number */
            'name'          => sprintf( __( 'Footer Widget Area %d', 'nxt-event-planner' ), $nxt_event_planner_fi ),
            'id'            => "footer{$nxt_event_planner_fi}-sidebar",
            /* translators: %d: footer column number */
            'description'   => sprintf( __( 'Add widgets here to appear in footer column %d.', 'nxt-event-planner' ), $nxt_event_planner_fi ),
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget'  => '</section>',
            'before_title'  => '<h3 class="widget-title title">',
            'after_title'   => '</h3>',
        ) );
    }
}
add_action( 'widgets_init', 'nxt_event_planner_widgets_init' );

function nxt_event_planner_force_widget_title_h3( $nxt_event_planner_params ) {
    if ( empty( $nxt_event_planner_params[0]['before_title'] ) || empty( $nxt_event_planner_params[0]['after_title'] ) ) {
        return $nxt_event_planner_params;
    }

    $nxt_event_planner_sidebar_id = isset( $nxt_event_planner_params[0]['id'] ) ? $nxt_event_planner_params[0]['id'] : '';
    if ( ! in_array( $nxt_event_planner_sidebar_id, array( 'sidebar-1', 'footer1-sidebar', 'footer2-sidebar', 'footer3-sidebar', 'footer4-sidebar' ), true ) ) {
        return $nxt_event_planner_params;
    }

    $nxt_event_planner_params[0]['before_title'] = preg_replace( '/<h[1-6](\\s[^>]*)?>/i', '<h3$1>', $nxt_event_planner_params[0]['before_title'] );
    $nxt_event_planner_params[0]['after_title']  = preg_replace( '/<\\/h[1-6]>/i', '</h3>', $nxt_event_planner_params[0]['after_title'] );

    return $nxt_event_planner_params;
}
add_filter( 'dynamic_sidebar_params', 'nxt_event_planner_force_widget_title_h3' );

function nxt_event_planner_force_widget_block_heading_h3( $nxt_event_planner_content, $nxt_event_planner_block ) {
    if ( empty( $nxt_event_planner_content ) ) {
        return $nxt_event_planner_content;
    }

    // Widget block content can be nested (e.g. core/group -> core/heading).
    // Convert heading blocks in widgets from h2 to h3 for sidebar hierarchy.
    $nxt_event_planner_content = preg_replace( '/<h2([^>]*)class=\"([^\"]*\\bwp-block-heading\\b[^\"]*)\"([^>]*)>/i', '<h3$1class="$2"$3>', $nxt_event_planner_content );
    $nxt_event_planner_content = preg_replace( '/<\\/h2>/i', '</h3>', $nxt_event_planner_content );

    return $nxt_event_planner_content;
}
add_filter( 'widget_block_content', 'nxt_event_planner_force_widget_block_heading_h3', 10, 2 );

function nxt_event_planner_excerpt_length( $nxt_event_planner_length ) {
    if ( is_admin() ) return $nxt_event_planner_length;
    return 10;
}
add_filter( 'excerpt_length', 'nxt_event_planner_excerpt_length', 999 );

class NXT_Event_Planner_Comment_Walker extends Walker_Comment {
    protected function html5_comment( $nxt_event_planner_comment, $nxt_event_planner_depth, $nxt_event_planner_args ) {
        $nxt_event_planner_tag = ( 'div' === $nxt_event_planner_args['style'] ) ? 'div' : 'li';
        ?>
        <<?php echo $nxt_event_planner_tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $nxt_event_planner_comment ); ?> >
            <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
                <div class="comment-header">
                    <div class="comment-author-avatar">
                        <?php if ( 0 != $nxt_event_planner_args['avatar_size'] ) echo get_avatar( $nxt_event_planner_comment, $nxt_event_planner_args['avatar_size'] ); ?>
                    </div>
                    <div class="comment-author-info">
                        <div class="comment-meta">
                            <b class="fn"><?php echo get_comment_author_link( $nxt_event_planner_comment ); ?></b>
                            <span class="says" data-i18n="says"><?php esc_html_e( 'says:', 'nxt-event-planner' ); ?></span>
                        </div>
                        <div class="comment-date">
                            <a href="<?php echo esc_url( get_comment_link( $nxt_event_planner_comment, $nxt_event_planner_args ) ); ?>">
                                <time datetime="<?php comment_time( 'c' ); ?>">
                                    <?php printf( __( '%1$s %2$s', 'nxt-event-planner' ), get_comment_date( '', $nxt_event_planner_comment ), get_comment_time() ); ?>
                                </time>
                            </a>
                            <?php edit_comment_link( '<span data-i18n="edit">' . __( 'Edit', 'nxt-event-planner' ) . '</span>', '<span class="edit-link">', '</span>' ); ?>
                        </div>
                    </div>
                </div>
                <?php if ( '0' == $nxt_event_planner_comment->comment_approved ) : ?>
                <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'nxt-event-planner' ); ?></p>
                <?php endif; ?>
                <div class="comment-content">
                    <?php comment_text(); ?>
                </div>
                <?php
                comment_reply_link( array_merge( $nxt_event_planner_args, array(
                    'add_below' => 'div-comment',
                    'depth'     => $nxt_event_planner_depth,
                    'max_depth' => $nxt_event_planner_args['max_depth'],
                    'before'    => '<div class="reply">',
                    'after'     => '</div>'
                ) ) ) ;
                ?>
            </article>
        </<?php echo $nxt_event_planner_tag; ?>>
        <?php
    }
}

function nxt_event_planner_get_random_gradient_class() {
    return 'dynamic-bg-placeholder';
}

function nxt_event_planner_register_block_styles() {
    if ( function_exists( 'register_block_style' ) ) {
        register_block_style('core/button', array('name'=>'outline','label'=>__( 'Outline', 'nxt-event-planner' )));
        register_block_style('core/quote', array('name'=>'border-left','label'=>__( 'Border Left', 'nxt-event-planner' )));
    }
}
add_action( 'init', 'nxt_event_planner_register_block_styles' );

function nxt_event_planner_register_block_patterns() {
    if ( function_exists( 'register_block_pattern' ) ) {
        register_block_pattern(
            'nxt-event-planner/hero-section',
            array(
                'title'       => __( 'Hero Section', 'nxt-event-planner' ),
                'categories'  => array( 'header' ),
                'content'     => '<!-- wp:group {"className":"hero-section"} --><div class="wp-block-group hero-section"><!-- wp:heading {"level":1} --><h1>Welcome to Our Site</h1><!-- /wp:heading --><!-- wp:paragraph --><p>This is a hero section with a call to action.</p><!-- /wp:paragraph --><!-- wp:buttons --><div class="wp-block-buttons"><!-- wp:button --><div class="wp-block-button"><a class="wp-block-button__link">Get Started</a></div><!-- /wp:button --></div><!-- /wp:buttons --></div><!-- /wp:group -->',
            )
        );
    }
}
add_action( 'init', 'nxt_event_planner_register_block_patterns' );
