<?php
/**
 * Wrock Metro enqueue scripts
 *
 * @package Wrock Metro
 */

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

if ( ! function_exists( 'wrockmetro_scripts' ) ) {
	/**
	 * Load theme's JavaScript and CSS sources.
	 */
	function wrockmetro_scripts() {
		// Get the theme data.
		$the_theme         = wp_get_theme();
		$theme_version     = $the_theme->get( 'Version' );	
		$suffix            = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

		// Grab asset urls.
		$theme_styles  = "/css/theme.css";
		$theme_styles_bootstraputiliti  = "/css/bootstrap-utilities.min.css";
		//$theme_styles  = "/css/theme{$suffix}.css";
		$theme_scripts = "/js/theme{$suffix}.js";

		$css_version = $theme_version . '.' . filemtime( get_template_directory() . $theme_styles ); // @phpstan-ignore-line -- file exists
		wp_enqueue_style( 'wrockmetro-styles', get_template_directory_uri() . $theme_styles, array(), $css_version );
		wp_enqueue_style( 'wrockmetro-styles-bootstrap-utilities', get_template_directory_uri() . $theme_styles_bootstraputiliti, array(), $css_version );
		wrockmetro_offcanvas_admin_bar_inline_styles();
		wrockmetro_settings_css();
		if(get_theme_mod( 'wrockmetro_container_type' ) == 'container-box'){wrockmetro_boxcss();}
		if(get_theme_mod( 'wrockmetro_content_background' ) !== 'contentdefault'){wrockmetro_contentblock();}
		if (get_theme_mod('wrockmetro_heroarea') =='enable')	{wrockmetro_header_styletwo();}
		if (get_theme_mod('wrockmetro_thumbhover') =='enable')	{wrockmetro_thumbhover();}
		if (get_theme_mod('wrockmetro_woostyle') !=='disable')	{wrockmetro_woostyle();}
		
		wp_enqueue_script( 'jquery' );

		wp_enqueue_script('ajax-search', get_template_directory_uri() . '/js/ajax-search.js', array('jquery'), '1.0', true);
		wp_localize_script('ajax-search', 'ajaxsearch', array('ajaxurl' => admin_url('admin-ajax.php')));
	

		$js_version = $theme_version . '.' . filemtime( get_template_directory() . $theme_scripts ); // @phpstan-ignore-line -- file exists
		wp_enqueue_script( 'wrockmetro-scripts', get_template_directory_uri() . $theme_scripts, array(), $js_version, true );
		wp_enqueue_script('wrockmetro-ticker-js', get_template_directory_uri().'/js/tickerme.js', array(), '1.0', false );
		
		wrockmetro_scrolltop();
		if (get_theme_mod('wrockmetro_loading_animation') !== 'disable' ){ repulicpro_js_codes(); }
		if (get_theme_mod('wrockmetro_stickymenu') == 'sticky-top' ){ repulicpro_ifstickytop(); }
		repulicpro_jsticker_codes();
		
		//wp_register_script( 'wrockmetro-tickertwo-js', get_template_directory_uri().'/js/jquery.ticker.js' );
		wp_enqueue_script('wrockmetro-tickertwo-js', get_template_directory_uri().'/js/jquery.ticker.js', array(), '1.0', false );	
		// wp_localize_script('wrockmetro-tickertwo-js', 'custom_ticker_settings', array(
	//		'titleText' => get_theme_mod('wrockmetro_tickerlabel_text', 'News Ticker'),
	//	));
	//	wp_enqueue_script( 'wrockmetro-tickertwo-js' );	

	
		if (get_theme_mod('wrockmetro_sticky_sidebarblog') == 'enable' ){ repulicpro_sticky_sidebar(); }
		
		if (get_theme_mod('wrockmetro_enablefonts') == 'enable' ){
		wp_enqueue_style('wrockmetro-body-font', '//fonts.googleapis.com/css?family='.str_replace(" ", "+", get_theme_mod('wrockmetro_body_font', 'Open Sans') ).':100,300,400,500,600,700' );
		}		

		
		if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
			wp_enqueue_script( 'comment-reply' );
		}
	}
} // End of if function_exists( 'wrockmetro_scripts' ).

add_action( 'wp_enqueue_scripts', 'wrockmetro_scripts' );

/**
* Enqueue script for custom customize control.
*/
function wrockmetro_custom_customize_enqueue() {
	wp_enqueue_style( 'customizer-css', get_stylesheet_directory_uri() . '/css/customizer-css.css' );
}
add_action( 'customize_controls_enqueue_scripts', 'wrockmetro_custom_customize_enqueue' );


if ( ! function_exists( 'wrockmetro_offcanvas_admin_bar_inline_styles' ) ) {
	/**
	 * Add inline styles for the offcanvas component if the admin bar is visibile.
	 *
	 * Fixes that the offcanvas close icon is hidden behind the admin bar.
	 *
	 * @since 1.2.0
	 */
	function wrockmetro_offcanvas_admin_bar_inline_styles() {
		$navbar_type = get_theme_mod( 'wrockmetro_navbar_type', 'collapse' );
		if ( 'offcanvas' !== $navbar_type ) {
			return;
		}
		$css = '
		body.admin-bar .offcanvas.show  {
			margin-top: 32px;
		}
		@media screen and ( max-width: 782px ) {
			body.admin-bar .offcanvas.show {
				margin-top: 46px;
			}
		}';
		wp_add_inline_style( 'wrockmetro-styles', $css );
	}
}
$loadcbar = ''; // Initialize $loadcbar with an empty string
if ( ! function_exists( 'wrockmetro_settings_css' ) ) {
	
	if (get_theme_mod('wrockmetro_loading_animation') !== 'disable' ){
		$loadcbar = '@keyframes page-load{from{width:0}to{width:100%}}.page-loading::before{content:" ";display:block;position:fixed;z-index:10;height:3px;width:100%;top:0;left:0;background-color:#ff3030;animation:page-load ease-out 2s}.page-loading #wpadminbar{margin-top: 3px;}';}

		
	function wrockmetro_settings_css(){	
		
		 global $loadcbar, $heroarea;
		 $custom_csscode = get_theme_mod('custom_css', '');
		 $morefonts  = 'system-ui,
  -apple-system, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", "Liberation Sans", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"';
		$custom_css = 'body{font-family: '.esc_html(get_theme_mod('wrockmetro_body_font')).','.$morefonts.';}	

		.styleone-header .wrockmetrotopmenu ul#main-menu li a:hover{color: #ffffff !important;}
		#wrapper-footer-full, #wrapper-static-hero{background-color: '.esc_html(get_theme_mod('wrockmetro_footerbg','#e9ecef')).';}
		div#blockbig h3.entry-title a,#blocksmall h3.entry-title a{color: '.esc_html(get_theme_mod('wrockmetro_blogtileslink','#ffffff')).' !important;}	
		.styletwo-header a.navbar-brand.custom-logo-link{max-width: '.esc_html(get_theme_mod('wrockmetro_styletwo_logosize','10')).'% !important;}
		div#wrapper-footer{background: '.esc_html(get_theme_mod('wrockmetro_footer_copy','#494949')).';}
		.tickertitle>.bn-title{background: '.esc_html(get_theme_mod('wrockmetro_tickerbg','#f44a56')).' !important;}
		.tickertitle>.bn-title>span{border-color: transparent transparent transparent '.esc_html(get_theme_mod('wrockmetro_tickerbg','#f44a56')).' !important;}
		.tickertitle>.bn-title>p{color: '.esc_html(get_theme_mod('wrockmetro_tickerlabelfont','#ffffff')).' !important;}
		div#ticker_container{background: '.esc_html(get_theme_mod('wrockmetro_tickerpostbg','#ffffff')).' !important;}
		div#ticker_container a{color: '.esc_html(get_theme_mod('wrockmetro_tickerpostlink','#5533ff')).' !important;}
		.styletwo-header a.nav-link{color: '.esc_html(get_theme_mod('wrockmetro_heroarea_links','#343434')).' !important;}
		.styletwo-header ul#main-menu li a:hover{color: '.esc_html(get_theme_mod('wrockmetro_heroarea_links_hover','#FF9800')).' !important;}'. $loadcbar . '
		h2.entry-title a{font-size: '.esc_html(get_theme_mod('wrockmetro_posttitlefont','20')).'px !important; font-style: '.esc_html(get_theme_mod('wrockmetro_posttitlefont_style','normal')).' !important;text-transform: '.esc_html(get_theme_mod('wrockmetro_posttitlefont_transform','none')).' !important;font-weight: '.esc_html(get_theme_mod('wrockmetro_posttitlefont_weight','600')).' !important;}
		.post-excerpt p{font-size: '.esc_html(get_theme_mod('wrockmetro_postexcert_size','17')).'px !important; font-style: '.esc_html(get_theme_mod('wrockmetro_postexcert_style','normal')).' !important;text-transform: '.esc_html(get_theme_mod('wrockmetro_postexcert_transform','none')).' !important;font-weight: '.esc_html(get_theme_mod('wrockmetro_postexcert_weight','100')).' !important;}h1.entry-title{font-size: '.esc_html(get_theme_mod('wrockmetro_postsingle_size','32')).'px !important; font-style: '.esc_html(get_theme_mod('wrockmetro_postsingle_style','normal')).' !important;text-transform: '.esc_html(get_theme_mod('wrockmetro_postsingle_transform','none')).' !important;font-weight: '.esc_html(get_theme_mod('wrockmetro_postsingle_weight','100')).' !important;}
		.page-loading::before {background-color: '.esc_html(get_theme_mod('wrockmetro_loading_bar_color','#ff3030')).' !important;} '.$custom_csscode.'	
			
		';		
		wp_add_inline_style( 'wrockmetro-styles', $custom_css );
	}
}

if ( ! function_exists( 'repulicpro_js_codes' ) ) {
	
	function repulicpro_js_codes(){
		
		$jscode = '(function( $ ) {
		  $(document).ready(function() {
				// Attach event listeners to specific links or elements where you want to trigger the loading animation
				var linksToAnimate = document.querySelectorAll(".load-animation-link");
				linksToAnimate.forEach(function(link) {
					link.addEventListener("click", function(e) {
						e.preventDefault(); // Prevent the default link behavior
						document.body.classList.add("page-loading");
						// Remove the loading class when the content has loaded
						window.addEventListener("DOMContentLoaded", function() {
							document.body.classList.remove("page-loading");
						});
					});
				});
				// Use this if you still want the loading animation on beforeunload
				window.addEventListener("beforeunload", function() {
					document.body.classList.add("page-loading");
				});
			
			});
		})(jQuery);';
		wp_add_inline_script( 'wrockmetro-scripts', $jscode );		
	}
}
if ( ! function_exists( 'repulicpro_jsticker_codes' ) ) {	
	function repulicpro_jsticker_codes(){
	
		$jscodeticker = '(function( $ ) {
		  $(document).ready(function() {
				$("#ticker").tickerme();
				 $("#js-news").ticker();				
			});
		})(jQuery);';

	wp_add_inline_script( 'wrockmetro-scripts', $jscodeticker );		
	}
}
function wrockmetro_admin_enqueue_styles() {
    wp_enqueue_style('wrockmetro-admin-css', get_template_directory_uri() . '/css/admin-style.css');
}
add_action('admin_enqueue_scripts', 'wrockmetro_admin_enqueue_styles');



 function wrockmetro_boxcss(){
    $mycss='body {max-width: 1279px;margin: 0 auto;background: #ececec;	}
	div#page {background: #ffffff;box-shadow: 10px 0 10px -10px #bbb, -10px 0 10px -10px #bbb;}
	';
   wp_add_inline_style( 'wrockmetro-styles', $mycss );
} 
 function wrockmetro_thumbhover(){
    $thumbhover='article .col-auto.d-lg-block:hover img {
    -webkit-transform: scale(1.1, 1.1);-moz-transform: scale(1.1, 1.1);-o-transform: scale(1.1, 1.1);-ms-transform:scale(1.1, 1.1);transform: scale(1.1, 1.1);-webkit-transition: all 0.6s linear;-moz-transition: all 0.6s linear;-o-transition: all 0.6s linear;-ms-transition: all 0.6s linear;transition: all 0.6s linear;}';
   wp_add_inline_style( 'wrockmetro-styles', $thumbhover );
} 
 function wrockmetro_contentblock(){
    $wrockmetro_contentblock='
	div#page {background: transparent !important;box-shadow: none !important;}
	.woocommerce ul.products li.product .woocommerce-loop-product__title,.woocommerce ul.products li.product .price,.card .add-to-cart-container {padding: 10px;}
	.woocommerce ul.products li.product a img{border-top-left-radius: 0.375rem;border-top-right-radius: 0.375rem;}
	.woocommerce ul.products li.product, .woocommerce-page ul.products li.product,
	article .row.g-0.border.rounded.overflow-hidden.flex-md-row.mb-4.shadow-sm.position-relative {background: #ffffff;}
	.wrockmetro-indexwid .col-md-7 ul li,
	.wrockmetro-indexwid .col-md-5,
	body.single-post main#main,
	body.page main#main,
	body.single-product main#main{background: #fff;padding: 2%;}
	.wrockmetro-popular, .wrockmetro-latest,div#left-sidebar aside, div#right-sidebar aside{padding: 10px;background: #ffffff;margin: 8px 0px;}
	div#left-sidebar aside:first-child,	div#right-sidebar aside:first-child{margin-top:0px;}';
   wp_add_inline_style( 'wrockmetro-styles', $wrockmetro_contentblock );
} 

function wrockmetro_header_styletwo(){
	$heroarea = '.styletwo-header nav#main-nav{background: #00000014 !important;}header#wrapper-navbar.nonfixed {height: 0;z-index: 100;position: relative;}';
	wp_add_inline_style( 'wrockmetro-styles', $heroarea );
}

function wrockmetro_woostyle(){
	$woostyle = '';
	wp_add_inline_style( 'wrockmetro-styles', $woostyle );
}

function wrockmetro_scrolltop(){
	if(get_theme_mod('wrockmetro_scroll_top_position')=='left'){
		$scrollcss = '.scrollup{left: 0;right:  auto ;}';
	}
	else if(get_theme_mod('wrockmetro_scroll_top_position')=='right'){
		$scrollcss = '.scrollup{left: auto;right:  0;}';
	}
	else{
		$scrollcss = '.scrollup{left: auto;right:  0;}';
	}

	wp_add_inline_style( 'wrockmetro-styles', $scrollcss );
}

function repulicpro_ifstickytop(){
	$ifstickytop = '#wpadminbar + .styletwo-header>.container{margin-top: 120px;}header#wrapper-navbar.sticky-top{height:0;} #wpadminbar + .styletwo-header header#wrapper-navbar{margin-top: 32px;}#customize-preview .styletwo-header>.container{margin-top: 120px;}';
	wp_add_inline_style( 'wrockmetro-styles', $ifstickytop );
}


if ( ! function_exists( 'repulicpro_sticky_sidebar' ) ) {
	
	function repulicpro_sticky_sidebar(){
		
		$jscode = '(function( $ ) {
			$(document).ready(function() {
				$(window).scroll(function() {
					// Check if left sidebar exists
					var leftSidebar = $("#left-sidebar");
					var leftSidebarOffset = leftSidebar.length > 0 ? leftSidebar.offset().top : null;
		
					// Check if right sidebar exists
					var rightSidebar = $("#right-sidebar");
					var rightSidebarOffset = rightSidebar.length > 0 ? rightSidebar.offset().top : null;
		
					// Modify left sidebar properties
					if (leftSidebarOffset !== null) {
						if ($(document).scrollTop() > (leftSidebarOffset - 20)) {                        
							leftSidebar.addClass("sticky-top");
						} else {
							leftSidebar.removeClass("sticky-top");
						}
					}
		
					// Modify right sidebar properties
					if (rightSidebarOffset !== null) {
						if ($(document).scrollTop() > (rightSidebarOffset - 20)) {
							rightSidebar.addClass("sticky-top");
						} else {
							rightSidebar.removeClass("sticky-top");
						}
					}
				});
			});
		})(jQuery);';
		wp_add_inline_script( 'wrockmetro-scripts', $jscode );		
		
		$csscode = 'div#right-sidebar, div#left-sidebar{height: fit-content;}';
		wp_add_inline_style( 'wrockmetro-styles', $csscode );		
	}
}
