<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
/*
=================================================================================================================
fasto_load_styles() - enqueue CSS files
=================================================================================================================
*/
function fasto_load_styles(){
	wp_enqueue_style( 'style' , FASTO_URI.'/style.css', array(), '1.0', 'all' );
	
	fasto_enqueue_fonts();
}
add_action( 'wp_enqueue_scripts' , 'fasto_load_styles', 1 );

/*
=================================================================================================================
fasto_add_fonts_css() - Add theme default fonts
=================================================================================================================
*/
function fasto_add_fonts_css(){
	
	$body_font = !empty(  get_theme_mod( 'body-font' ) ) ? ucwords( get_theme_mod( 'body-font' ) ) : 'Roboto';	
	$body_font_w = !empty(  get_theme_mod( 'body-font-w' ) ) ?  get_theme_mod( 'body-font-w' ) : '400';	
	$body_font = str_replace( '-',' ', $body_font );

	$heading_font = !empty(  get_theme_mod( 'heading-font' ) ) ? ucwords( get_theme_mod( 'heading-font' ) ) : 'Roboto';
	$heading_font_w = !empty(  get_theme_mod( 'heading-font-w' ) ) ?  get_theme_mod( 'heading-font-w' ) : '700';
	$heading_font = str_replace( '-',' ', $heading_font );
	
	$css = '
			body,.primary-font{font-family:'.$body_font.', Arial} 
			h1,h2,h3,h4,h5,h6,.secondary-font,.woocommerce-Price-amount,.woocommerce form .form-row label{font-family:'. $heading_font .', Arial}
		';
	wp_add_inline_style( 'fasto-custom-css', $css );
}
add_action( 'wp_enqueue_scripts','fasto_add_fonts_css' );

/*
=================================================================================================================
fasto_enqueue_fonts() - Enqueue Google Fonts
=================================================================================================================
*/
function fasto_enqueue_fonts(){

	$body_font = !empty(  get_theme_mod( 'body-font' ) ) ? ucwords( get_theme_mod( 'body-font' ) ) : 'Roboto';	
	$body_font_w = !empty(  get_theme_mod( 'body-font-w' ) ) ?  get_theme_mod( 'body-font-w' ) : '400';	
	$body_font = str_replace( '-',' ', $body_font );

	$heading_font = !empty(  get_theme_mod( 'heading-font' ) ) ? ucwords( get_theme_mod( 'heading-font' ) ) : 'Roboto';
	$heading_font_w = !empty(  get_theme_mod( 'heading-font-w' ) ) ?  get_theme_mod( 'heading-font-w' ) : '700';
	$heading_font = str_replace( '-',' ', $heading_font );

	wp_enqueue_style( 'fasto-custom-css', 'https://fonts.googleapis.com/css?family='. $heading_font .':'. $heading_font_w .'|'. $body_font .':'. $body_font_w .'&display=swap' );
}

/*
=================================================================================================================
fasto_load_scripts() - enqueue js files
=================================================================================================================
*/
function fasto_load_scripts(){
	wp_enqueue_script('jquery');
	
	if ( get_theme_mod( 'lazy-load' )  == '1' ){
		wp_enqueue_script( 'lazy', FASTO_URI."/js/lazy.js", '', '', true );
	}
	
	wp_enqueue_script( 'scripts', FASTO_URI."/js/scripts.js", '', '', true );
	
	wp_localize_script( 'scripts', 'ajax_var', array(
		'url' => admin_url( 'admin-ajax.php' ),
		'nonce_shares' => wp_create_nonce( 'fasto_count_shares' ),
	 ));	
	 
	 $data = array(
		'url' => FASTO_URI,
	);
	
	wp_localize_script( 'lazy', 'object_name', $data );
	 
}
add_action( 'wp_enqueue_scripts' , 'fasto_load_scripts' , 1 );

/*
=================================================================================================================
fasto_gutenberg_gallery_enqueue() - enqueue CSS and JS gallery only if needed
=================================================================================================================
*/
function fasto_gutenberg_gallery_enqueue(){
		
	if ( is_singular('post') ){
			
		$post = get_post();	
		
		//backward compatibility - this function was introduced in Wordpress 5.0.0
		if ( function_exists( 'has_blocks' ) ){
			if ( has_blocks( $post->post_content ) ) {
				$blocks = parse_blocks( $post->post_content );
				foreach ( $blocks as  $block ){
					if( $block['blockName'] === 'core/gallery' ){
						wp_enqueue_style( 'simple-lightbox' , FASTO_URI.'/css/simple-lightbox.min.css', array(), '1.0', 'all' );	
						wp_enqueue_script( 'simple-lightbox', FASTO_URI."/js/simple-lightbox.min.js", '', '', true );
					}
				}
			}
		}
		else{
			wp_enqueue_style( 'simple-lightbox' , FASTO_URI.'/css/simple-lightbox.min.css', array(), '1.0', 'all' );	
			wp_enqueue_script( 'simple-lightbox', FASTO_URI."/js/simple-lightbox.min.js", '', '', true );			
		}
	
	}
}

add_action( 'get_footer','fasto_gutenberg_gallery_enqueue',1 );
	

/*
=================================================================================================================
fasto_widget_gallery_enqueue() - enqueue CSS and JS if widget has gallery
=================================================================================================================
*/
function fasto_widget_gallery_enqueue(){
	$widgets = wp_get_sidebars_widgets();
	unset( $widgets['wp_inactive_widgets'] );
	foreach ( $widgets as $widget ){
		foreach ( $widget as $w ){
			if ( stripos ( $w, 'media_gallery'  ) !== false ){
				wp_enqueue_style( 'simple-lightbox' , FASTO_URI.'/css/simple-lightbox.min.css', array(), '1.0', 'all' );	
				wp_enqueue_script( 'simple-lightbox', FASTO_URI."/js/simple-lightbox.min.js", '', '', true );					
			}
		}
	}
}
add_action( 'get_footer','fasto_widget_gallery_enqueue',1 );
/*
=================================================================================================================
fasto_load_admin_styles() - enqueue the CSS files in admin
=================================================================================================================
*/
function fasto_load_admin_styles() {
	wp_enqueue_style( 'admin-fasto' , FASTO_URI.'/css/admin.css', array(), '1.0', 'all' );	
}
add_action( 'admin_enqueue_scripts', 'fasto_load_admin_styles' );

/*
=================================================================================================================
fasto_load_admin_scripts() - enqueue the JS files in admin for future use - commented action for now
=================================================================================================================
*/
function fasto_load_admin_scripts() {
	
}
//add_action( 'admin_enqueue_scripts', 'fasto_load_admin_scripts' );