<?php
/**
 * Css js include
 *
 * @package wpstream-theme
 */

if ( ! function_exists( 'wpstream_js_variable' ) ) {
	add_action( 'wp_head', 'wpstream_js_variable', 1 );
	/**
	 * Add js variable
	 *
	 * @return void
	 */
	function wpstream_js_variable() {
		$variables = array(
			'dev_mode' => defined( 'WPSTREAM_THEME_JS_LOG' ) && WPSTREAM_THEME_JS_LOG,
		);

		$variables = apply_filters( 'wpstream_js_variable', $variables );

		foreach ( $variables as $key => $value ) {
			if ( is_string( $value ) ) {
				$variables[ $key ] = html_entity_decode( $value, ENT_QUOTES, 'UTF-8' );
			}
		}

		?>
		<script id="wpstream-js-variable">
			window.wpstreamVariable = <?php echo wp_json_encode( $variables ); ?>;
		</script>
		<?php
	}
}

add_action( 'wp_enqueue_scripts', 'wpstream_theme_scripts' );
/**
 * Theme scripts
 */
function wpstream_theme_scripts() {
	$suffix  = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
	$bridge_path    = get_template_directory() . '/js/wpstream-playback-ui-bridge.js';
	$bridge_url     = get_template_directory_uri() . '/js/wpstream-playback-ui-bridge.js';

	// Get modification time. Enqueue files with modification date to prevent browser from loading cached scripts and styles when file content changes.
	$modificated_wpstream_theme_css = ( file_exists( get_template_directory() . '/css/main.css' ) ) ? gmdate( 'YmdHi', filemtime( get_template_directory() . '/css/main.css' ) ) : 1;
	$modificated_style_css          = gmdate( 'YmdHi', filemtime( get_stylesheet_directory() . '/style.css' ) );
	$modificated_fontawesome_css    = gmdate( 'YmdHi', filemtime( get_template_directory() . "/fontawesome/css/all{$suffix}.css" ) );
	$modificated_bootstrap_js       = gmdate( 'YmdHi', filemtime( get_template_directory() . "/js/lib/bootstrap.bundle{$suffix}.js" ) );
	$modificated_theme_js           = gmdate( 'YmdHi', filemtime( get_template_directory() . '/js/theme.js' ) );
	$modificated_bridge_js          = ( file_exists( $bridge_path ) ) ? gmdate( 'YmdHi', filemtime( $bridge_path ) ) : $modificated_theme_js;

	if ( defined( 'WPSTREAM_PLUGIN_PATH' ) && defined( 'WPSTREAM_PLUGIN_DIR_URL' ) ) {
		$plugin_bridge_path = WPSTREAM_PLUGIN_PATH . 'hello-wpstream/js/wpstream-playback-ui-bridge.js';
		if ( file_exists( $plugin_bridge_path ) ) {
			$bridge_path           = $plugin_bridge_path;
			$bridge_url            = WPSTREAM_PLUGIN_DIR_URL . 'hello-wpstream/js/wpstream-playback-ui-bridge.js';
			$modificated_bridge_js = gmdate( 'YmdHi', filemtime( $bridge_path ) );
		}
	}



	// Enqueue theme fonts.
	wp_enqueue_style( 'wpstream-fonts', '//fonts.googleapis.com/css2?family=Mulish:ital,wght@0,400;0,900;1,400;1,900&family=Roboto:ital,wght@0,400;0,500;0,600;0,700;1,400;1,700&display=swap', '', '1.0.0', 'screen' );

	$fonts_data = wpstream_get_customizer_fonts_data();
	$url_font = wpstream_generate_google_fonts($fonts_data);

    if ($url_font){
	    wp_enqueue_style( 'google-fonts', $url_font, '', '1.0.0', 'screen' );
    }

	wp_enqueue_style( 'wpstream-main', get_template_directory_uri() . '/css/main.css', array(), $modificated_wpstream_theme_css );

	// Style CSS.
	wp_enqueue_style( 'wpstream_theme-style', get_stylesheet_uri(), array(), $modificated_style_css );

	// Fontawesome.
	wp_enqueue_style( 'fontawesome', get_template_directory_uri() . "/fontawesome/css/all{$suffix}.css", array(), $modificated_fontawesome_css );

	// Bootstrap JS.
	wp_enqueue_script( 'bootstrap', get_template_directory_uri() . "/js/lib/bootstrap.bundle{$suffix}.js", array(), $modificated_bootstrap_js, true );

	// Theme JS.
	wp_enqueue_script( 'wpstream_theme-script', get_template_directory_uri() . '/js/theme.js', array( 'jquery' ), $modificated_theme_js, true );
	wp_enqueue_script( 'wpstream-playback-ui-bridge', $bridge_url, array( 'jquery' ), $modificated_bridge_js, true );

	// IE Warning.
	wp_localize_script(
		'wpstream_theme-script',
		'wpstream_theme_vars',
		array(
			'ie_title'                 => __( 'Internet Explorer detected', 'hello-wpstream' ),
			'ie_limited_functionality' => __( 'This website will offer limited functionality in this browser.', 'hello-wpstream' ),
			'ie_modern_browsers_1'     => __( 'Please use a modern and secure web browser like', 'hello-wpstream' ),
			'ie_modern_browsers_2'     => __( 'Mozilla Firefox, Google Chrome,Opera', 'hello-wpstream' ),
			'ie_modern_browsers_3'     => __( 'or', 'hello-wpstream' ),
			'ie_modern_browsers_4'     => __( 'Microsoft Edge', 'hello-wpstream' ),
			'ie_modern_browsers_5'     => __( 'to display this site correctly.', 'hello-wpstream' ),
			'admin_url'                => get_admin_url(),
			'sending'                  => esc_html__( 'sending your data', 'hello-wpstream' ),
			'terms_cond'               => esc_html__( 'You have to agree with terms & conditions', 'hello-wpstream' ),
			'login_redirect'           => wpstream_return_dashboard_page(),
			'usecaptcha'               => 'no',
			'userpass'                 => 'yes',
			'no_more_items'            => '<div class="wpstream-no-more-items">' . esc_html__( 'There are no more items!', 'hello-wpstream' ) . '</div>',
			'like_nonce_ajax'          => wp_create_nonce( 'like_nonce_ajax_action' ),
			'dislike_nonce_ajax'       => wp_create_nonce( 'dislike_nonce_ajax_action' ),
			'shortcodes_nonce_ajax'    => wp_create_nonce( 'shortcode_nonce_ajax_action' ),
			'is_rtl'				   => is_rtl() 
		)
	);
	// IE Warning End.  $is_rtl=0;

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

	wp_enqueue_script( 'popper', get_template_directory_uri() . "/js/popper{$suffix}.js", array(), '1.0', true );

	wp_enqueue_script( 'slick-slider', get_template_directory_uri() . "/js/slick{$suffix}.js", array(), '1.0', true );
	wp_enqueue_style( 'slick-slider-css', get_template_directory_uri() . '/css/slick.css', array(), '1.0' );
}

add_action( 'admin_enqueue_scripts', 'wpstream_admin_enqueue_scripts_callback' );
/**
 * Admin enqueue scripts callback
 */
function wpstream_admin_enqueue_scripts_callback() {
	wp_enqueue_script( 'jquery-ui-sortable' );
	wp_enqueue_script( 'jquery-ui-autocomplete' );
	
	wp_enqueue_media();
    //wp_enqueue_script('media-uploader', get_template_directory_uri() . '/js/media-uploader.js', array('jquery'), null, true);

	wp_enqueue_style( 'wpstream-adminstyle', get_theme_file_uri( '/css/admin.css' ), '', '1.0' );
	wp_enqueue_script( 'wpstream-admin-control', get_theme_file_uri( '/js/admin-control.js?v=' . wp_rand() ), array( 'jquery' ), '1.0', true );
}


