<?php
/**
 * Wrock Metro functions and definitions
 *
 * @package Wrock Metro
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
define( 'WROCKMETRO_VERSION', wp_get_theme()->get( 'Version' ) );
// Wrock Metro's includes directory.
$wrockmetro_inc_dir = 'inc';
define( 'WROCKMETRO_THEME_DIR', get_template_directory() );
define( 'WROCKMETRO_THEME_URI', get_template_directory_uri() );

// Array of files to include.
$wrockmetro_includes = array(
	'/theme-settings.php',                  // Initialize theme default settings.
	'/setup.php',                           // Theme setup and custom theme supports.
	'/widgets.php',                         // Register widget area.
	'/enqueue.php',                         // Enqueue scripts and styles.
	'/template-tags.php',                   // Custom template tags for this theme.
	'/pagination.php',                      // Custom pagination for this theme.
	'/hooks.php',                           // Custom hooks.
	'/extras.php',                          // Custom functions that act independently of the theme templates.
	'/customizer.php',                      // Customizer additions.
	'/custom-comments.php',                 // Custom Comments file.
	'/class-wp-bootstrap-navwalker.php',    // Load custom WordPress nav walker.
	'/editor.php',                          // Load Editor functions.
	'/block-editor.php',                    // Load Block Editor functions.
	'/deprecated.php',                      // Load deprecated functions.
	'/widget-popular-posts.php',            // Widget Popular Posts.
	'/widget-latest-posts.php',             // Widget Latest Posts.
	'/widget-block-posts.php',            	// Widget Block Posts.
	'/index-posts.php',             		// Index Posts.
	//'/incfile.php',             			// Functions.
	'/listpostsblock.php',             		// list posts block.
	'/blocks-tiles.php',             		// tiles posts block.
	'/pagemetalayout.php',             		// tiles posts block.
	'/block-style.php',             		// block-style.
	'/schema.php',             				// schema
	
);

function wrockmetro_styles_admin($hook) {
	if ( 'appearance_page_wrockmetro-info' != $hook ) {
        return;
    }
	wp_enqueue_style('wrockmetro-admin-styles',get_theme_file_uri( 'inc/core/bootstrap/css/admin-style.css' ),[], WROCKMETRO_VERSION );
	wp_enqueue_style('wrockmetro-bootstrap-responsive',get_theme_file_uri( 'inc/core/bootstrap/css/bootstrap-responsive.css' ),[], WROCKMETRO_VERSION );
}
add_action( 'admin_enqueue_scripts', 'wrockmetro_styles_admin' );

function wrockmetro_add_menu_page() {
	add_theme_page( 
		esc_html__( 'Wrock Metro', 'wrock-metro' ), 
		esc_html__( 'Wrock Metro', 'wrock-metro' ), 
		'edit_theme_options', 'wrockmetro-info', 'wrockmetro_theme_page_display','2' );
}
add_action( 'admin_menu', 'wrockmetro_add_menu_page' );
function wrockmetro_theme_page_display() {
	$theme = wp_get_theme();
	include_once 'inc/theme-info.php';
}
final class WROCKMETRO_Theme_Class {
	public static function wrockmetro_constants() {
		
		$version = self::theme_version();

		// Theme version.
		define( 'WROCKMETRO_THEME_VERSION', $version );
		
		define( 'WROCKMETRO_JS_DIR_URI', WROCKMETRO_THEME_URI . '/js/' );
		define( 'WROCKMETRO_CSS_DIR_URI', WROCKMETRO_THEME_URI . '/css/' );

		// Include Paths.
		define( 'WROCKMETRO_INC_DIR', WROCKMETRO_THEME_DIR . '/inc/' );
		define( 'WROCKMETRO_INC_DIR_URI', WROCKMETRO_THEME_URI . '/inc/' );
		
	}
}
new WROCKMETRO_Theme_Class();
// Load WooCommerce functions if WooCommerce is activated.
if ( class_exists( 'WooCommerce' ) ) {
	$wrockmetro_includes[] = '/woocommerce.php';
}

// Load Jetpack compatibility file if Jetpack is activiated.
if ( class_exists( 'Jetpack' ) ) {
	$wrockmetro_includes[] = '/jetpack.php';
}
// Include files.
foreach ( $wrockmetro_includes as $file ) {
	require_once get_theme_file_path( $wrockmetro_inc_dir . $file );
}

// add the ajax fetch js
add_action( 'wp_footer', 'wrockmetro_fetch_ajax' );
function wrockmetro_fetch_ajax() {
?>
    <script type="text/javascript">
    jQuery(document).ready(function ($) {
        $('#keyword').on('keyup', function () {
            var keyword = $(this).val();
            
            if (keyword.length > 2) { // Prevent unnecessary AJAX calls
                $.ajax({
                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    type: 'POST',
                    data: {
                        action: 'data_fetch',
                        keyword: keyword
                    },
                    success: function (data) {
                        $('#datafetch').html(data);
                    }
                });
            }
        });
    });
    </script>
<?php
}
// the ajax function
add_action('wp_ajax_data_fetch' , 'wrockmetro_data_fetch');
add_action('wp_ajax_nopriv_data_fetch','wrockmetro_data_fetch');
function wrockmetro_data_fetch(){

    $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => array('post') ) );
    if( $the_query->have_posts() ) :?>
	<div class="wrockmetro-instantsearch">
      <ul>
      <?php while( $the_query->have_posts() ): $the_query->the_post(); ?>
            <li><a href="<?php echo esc_url( get_permalink() ); ?>">
			<?php if (has_post_thumbnail()) : ?>
                        <div class="post-thumbnail">
                            <?php the_post_thumbnail(array(70, 70)); ?>
                        </div>
                    <?php endif; ?><?php the_title();?></a></li>
        <?php endwhile; ?>
     </ul>
	</div>
       <?php wp_reset_postdata();  
    endif;

    die();
}
// Block Theme Support
add_action( 'after_setup_theme', 'wrockmetro_block_template_part_support' );
function wrockmetro_block_template_part_support() {
	add_theme_support( 'block-template-parts' );
	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'border' );
}
if ( is_user_logged_in() ) {   
	if ( ! function_exists( 'wrockmetro_primary_menu_fallback' ) ) {
		function wrockmetro_primary_menu_fallback() {		 
		  ?>	  
		<ul id="main-menu" class="navbar-nav flex-grow-1 pe-3">
			<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-904 nav-item"><a title="Home" href="<?php echo esc_url(home_url()); ?>" class="nav-link"><?php echo esc_html('Home','wrock-metro'); ?></a></li>
			<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-905 nav-item"><a title="Contact us" href="<?php echo esc_url( admin_url( 'nav-menus.php' ) ); ?>" class="nav-link"><?php echo esc_html('Set primary menu','wrock-metro'); ?></a></li>
		</ul>
		  <?php
		}
	}
}else {
		if ( ! function_exists( 'wrockmetro_primary_menu_fallback' ) ) {
		function wrockmetro_primary_menu_fallback() {		 
		  ?>	  
		<ul id="main-menu" class="navbar-nav flex-grow-1 pe-3">
			<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-904 nav-item"><a title="Home" href="<?php echo esc_url(home_url()); ?>" class="nav-link"><?php echo esc_html('Home','wrock-metro'); ?></a></li>
		</ul>
		  <?php
		}
	}	
}

function wrockmetro_home_link_to_menu($items, $args) {
    // Get the customizer setting value
    $show_home_link = get_theme_mod('wrockmetro_homeicon', true);

    // Only proceed if the option is enabled
    if ($show_home_link && $args->theme_location == 'primary') {
        // Determine if the current page is the homepage
        $current_class = is_front_page() ? ' current-menu-item' : '';
        
        // Create the Home link with the desired classes and attributes
        $homeMenuItem = '<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" class="menu-item menu-item-type-post_type menu-item-object-page nav-item' . $current_class . '"><a href="' . esc_url(home_url('/')) . '" class="nav-link"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="19" fill="currentColor" class="bi bi-house-door-fill" viewBox="0 0 16 18"><path d="M6.5 14.5v-3.505c0-.245.25-.495.5-.495h2c.25 0 .5.25.5.5v3.5a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5v-7a.5.5 0 0 0-.146-.354L13 5.793V2.5a.5.5 0 0 0-.5-.5h-1a.5.5 0 0 0-.5.5v1.293L8.354 1.146a.5.5 0 0 0-.708 0l-6 6A.5.5 0 0 0 1.5 7.5v7a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5"></path></svg></a></li>';
        
        // Add the Home link to the beginning of the menu
        $items = $homeMenuItem . $items;
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'wrockmetro_home_link_to_menu', 10, 2);