<?php
/**
 * Register Google Fonts
 */
function ci_theme_fonts_url() {
	$fonts_url = '';
	$fonts     = array();
	$subsets   = 'latin,latin-ext';

	/* translators: If there are characters in your language that are not supported by Montserrat, translate this to 'off'. Do not translate into your own language. */
	if ( 'off' !== _x( 'on', 'Montserrat font: on or off', 'ci_theme' ) ) {
		$fonts[] = 'Montserrat:400,700';
	}

	/* translators: If there are characters in your language that are not supported by Lato, translate this to 'off'. Do not translate into your own language. */
	if ( 'off' !== _x( 'on', 'Lato font: on or off', 'ci_theme' ) ) {
		$fonts[] = 'Lato:300,400,400i,700,700i';
	}

	if ( $fonts ) {
		$fonts_url = add_query_arg( array(
			'family' => urlencode( implode( '|', $fonts ) ),
			'subset' => urlencode( $subsets ),
		), 'https://fonts.googleapis.com/css' );
	}

	return $fonts_url;
}

add_action( 'init', 'ci_register_theme_styles' );
if ( ! function_exists( 'ci_register_theme_styles' ) ):
function ci_register_theme_styles() {
	//
	// Register all front-end and admin styles here.
	// There is no need to register them conditionally, as the enqueueing can be conditional.
	//
	$theme = wp_get_theme();

	wp_register_style( 'ci-theme-google-font', ci_theme_fonts_url(), array(), $theme->get( 'Version' ) );
	wp_register_style( 'ci-theme-typography-google', get_theme_file_uri( '/css/typography-gfont.css' ), array(), $theme->get( 'Version' ) );
	wp_register_style( 'ci-theme-typography-default', get_theme_file_uri( '/css/typography-default.css' ), array(), $theme->get( 'Version' ) );

	wp_register_style( 'ci-base', get_theme_file_uri( '/css/base.css' ) );
	wp_register_style( 'ci-flexslider', get_theme_file_uri( '/css/flexslider.css' ) );
	wp_register_style( 'mmenu', get_theme_file_uri( '/css/mmenu.css' ) );
	wp_register_style( 'magnific', get_theme_file_uri( '/css/magnific.css' ), array(), '1.0.0' );
	wp_register_style( 'jquery-ui-style', get_theme_file_uri( '/css/admin/jquery-ui.css' ), array(), '1.10.4' );
	wp_register_style( 'stapel', get_theme_file_uri( '/css/stapel.css' ), array(), '1.0.0' );

	wp_register_style( 'ci-repeating-fields', get_theme_file_uri( '/css/admin/repeating-fields.css' ) );
	wp_register_style( 'ci-admin-widgets', get_theme_file_uri( '/css/admin/admin-widgets.css' ), array( 'ci-repeating-fields' ) );
	wp_register_style( 'ci-post-edit-screens', get_theme_file_uri( '/css/admin/post_edit_screens.css' ), array( 'ci-repeating-fields' ) );

	wp_register_style( 'ci-color-scheme', get_theme_file_uri( '/colors/' . ci_setting( 'stylesheet' ) ) );

	wp_register_style( 'ci-theme-dependencies', false, array(
		'ci-base',
		'ci-theme-common',
		'ci-flexslider',
		'font-awesome',
		'mmenu',
		'stapel',
		'magnific',
	), $theme->get( 'Version' ) );

	$main_dependencies = array(
		'ci-theme-dependencies',
	);

	if ( get_theme_mod( 'global_typo_is_google_active', ci_theme_customizer_defaults( 'global_typo_is_google_active' ) ) ) {
		add_action( 'wp_head', 'ci_theme_head_preconnect_google_fonts' );

		$main_dependencies = array_merge(
			$main_dependencies,
			array(
				'ci-theme-google-font',
				'ci-theme-typography-google',
			)
		);
	} else {
		$main_dependencies = array_merge(
			$main_dependencies,
			array(
				'ci-theme-typography-default',
			)
		);
	}

	if ( is_child_theme() ) {
		wp_register_style( 'ci-theme-style-parent', get_template_directory_uri() . '/style.css', $main_dependencies, $theme->get( 'Version' ) );
	}

	wp_register_style( 'ci-style', get_stylesheet_uri(), $main_dependencies, $theme->get( 'Version' ) );
}
endif;


add_action( 'wp_enqueue_scripts', 'ci_enqueue_theme_styles' );
if ( ! function_exists( 'ci_enqueue_theme_styles' ) ) :
function ci_enqueue_theme_styles() {
	//
	// Enqueue all (or most) front-end styles here.
	//
	if ( wp_style_is( 'woocommerce_prettyPhoto_css', 'enqueued' ) ) {
		wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
	}

	if ( is_child_theme() ) {
		wp_enqueue_style( 'ci-theme-style-parent' );
	}

	ci_theme_enqueue_google_fonts();

	wp_enqueue_style( 'ci-style' );
	wp_enqueue_style( 'ci-color-scheme' );
	wp_add_inline_style( 'ci-color-scheme', ci_theme_get_all_customizer_css() );
}
endif;


if ( ! function_exists( 'ci_enqueue_admin_theme_styles' ) ) :
add_action( 'admin_enqueue_scripts', 'ci_enqueue_admin_theme_styles' );
function ci_enqueue_admin_theme_styles() {
	global $pagenow;

	//
	// Enqueue here styles that are to be loaded on all admin pages.
	//

	if ( is_admin() and $pagenow == 'themes.php' and isset( $_GET['page'] ) and $_GET['page'] == 'ci_panel.php' ) {
		//
		// Enqueue here styles that are to be loaded only on CSSIgniter Settings panel.
		//
	}

	if ( in_array( $pagenow, array( 'widgets.php', 'customize.php' ) ) ) {
		wp_enqueue_style( 'ci-admin-widgets' );
	}

}
endif;
