<?php
/**
 * click5 enqueue scripts
 *
 * @package click5
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! function_exists( 'click5_scripts' ) ) {
	
	function click5_scripts() {

		$the_theme = wp_get_theme();
		$theme_version = $the_theme->get( 'Version' );
		
		wp_enqueue_style( 'fonts', get_stylesheet_directory_uri() . '/css/fonts.css', array(), $theme_version );
		wp_enqueue_style( 'bootstrap-styles', get_stylesheet_directory_uri() . '/css/bootstrap.min.css', array(), $theme_version );

		if( !is_page(2) ) {
			wp_enqueue_style( 'lightbox', get_stylesheet_directory_uri() . '/css/lightbox.css', array(), $theme_version );
		}
		
		wp_enqueue_style( 'slick', get_stylesheet_directory_uri() . '/css/slick.css', array(), $theme_version );
		wp_enqueue_style( 'click5-theme', get_stylesheet_directory_uri() . '/css/theme.css', array(), $theme_version );
		wp_enqueue_style( 'click5-style', get_stylesheet_uri(), array(), $theme_version );
		wp_enqueue_style( 'click5-media', get_stylesheet_directory_uri() . '/css/media.css', array(), $theme_version, 'screen and (max-width: 1799px)' );

		wp_enqueue_script('jquery');
		
		wp_enqueue_script( 'fontawesome-scripts', 'https://kit.fontawesome.com/840348a411.js', array(), $theme_version, true );
		wp_enqueue_script( 'bootstrap-scripts', get_template_directory_uri() . '/js/bootstrap.min.js', array(), $theme_version, [ 'strategy' => 'defer', 'in_footer' => 'true' ]);

		if( !is_page(2) ) {
			wp_register_script( 'recaptcha-scripts', get_template_directory_uri() . '/js/recaptcha.js', array(), $theme_version, [ 'strategy' => 'defer', 'in_footer' => 'true' ]);
			wp_enqueue_script( 'recaptcha-scripts' );	
			wp_localize_script( 'recaptcha-scripts',
				'click5_recaptcha',
				array(
					'sitekey' => get_field("recaptcha_sitekey", "option")
				)
			);

			wp_enqueue_script( 'modal-video-scripts', get_template_directory_uri() . '/js/jquery.modal-video.min.js', array('jquery'), $theme_version, [ 'strategy' => 'defer', 'in_footer' => 'true' ]);
			wp_enqueue_script( 'mask-scripts', get_template_directory_uri() . '/js/jquery.masked-input.min.js', array('jquery'), $theme_version, [ 'strategy' => 'defer', 'in_footer' => 'true' ]);
			wp_enqueue_script( 'lightbox-scripts', get_template_directory_uri() . '/js/lightbox.js', array('jquery'), $theme_version, [ 'strategy' => 'defer', 'in_footer' => 'true' ]);
		}
		
		wp_enqueue_script( 'slick-scripts', get_template_directory_uri() . '/js/slick.min.js', array('jquery'), $theme_version, [ 'strategy' => 'defer', 'in_footer' => 'true' ]);
		wp_enqueue_script( 'click5-theme-scripts', get_template_directory_uri() . '/js/theme.js', array('jquery'), $theme_version, [ 'strategy' => 'defer', 'in_footer' => 'true' ]);

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

		if( is_page(2) ) {
			wp_dequeue_style( 'contact-form-7' );
			wp_dequeue_script( 'contact-form-7' );
		}

		wp_dequeue_style( 'wp-block-library' );
		wp_dequeue_style( 'classic-theme-styles' );
	}
}

add_action( 'wp_enqueue_scripts', 'click5_scripts', 99 );

function click5_preload_styles($html, $handle, $href, $media) {
    if ( $handle === 'bootstrap-styles'
			|| $handle === 'fonts'
			|| $handle === 'click5-style'
			|| $handle === 'click5-theme' ) {

        return "<link rel='preload' href='{$href}' as='style' onload=\"this.onload=null;this.rel='stylesheet'\">\n" .
               "<noscript><link rel='stylesheet' href='{$href}'></noscript>\n";
    }

    return $html;
}
add_filter( 'style_loader_tag', 'click5_preload_styles', 10, 4 );