<?php
function ci_theme_include_generated_styles_files() {
	require_once get_theme_file_path( '/functions/customizer/generated-styles/global.php' );
}

function ci_theme_get_registered_typography_controls() {

	$controls = array(
		'global_typo_body'          => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_h1'            => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_h2'            => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_h3'            => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_h4'            => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_h5'            => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_h6'            => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_form_labels'   => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_form_text'     => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_buttons'       => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_widget_titles' => ci_theme_typography_control_defaults_empty_breakpoints(),
		'global_typo_widget_text'   => ci_theme_typography_control_defaults_empty_breakpoints(),
	);

	return apply_filters( 'ci_theme_registered_typography_controls', $controls );
}

function ci_theme_enqueue_google_fonts() {
	$css = CI_Theme_Customizer_CSS_Generator::get_instance();

	if ( is_customize_preview() ) {
		$css->register_typography_control(
			'placeholder_preview_font',
			ci_theme_typography_control_defaults_empty_breakpoints(
				array(
					'desktop' => array(
						'family'     => 'Open Sans',
						'variant'    => 'regular',
						'size'       => '',
						'lineHeight' => '',
						'transform'  => '',
						'spacing'    => '',
						'is_gfont'   => true,
					),
				)
			)
		);
	}

	foreach ( ci_theme_get_registered_typography_controls() as $option => $default ) {
		$css->register_typography_control( $option, $default );
	}

	$url = $css->get_google_fonts_url();
	if ( ! empty( $url ) && ! has_action( 'wp_head', 'ci_theme_head_preconnect_google_fonts' ) ) {
		add_action( 'wp_head', 'ci_theme_head_preconnect_google_fonts' );
	}

	wp_enqueue_style( 'ci-theme-user-google-fonts', $url, array(), wp_get_theme()->get( 'Version' ) );
}

/**
 * Generates CSS based on customizer settings.
 *
 * @return string
 */
function ci_theme_get_customizer_css() {
	ci_theme_include_generated_styles_files();

	$generator = CI_Theme_Customizer_CSS_Generator::get_instance();

	$css = ci_theme_get_part_customizer_css();

	$breakpoints = array(
		'desktop' => '',
		'tablet'  => 1031,
		'mobile'  => 797,
	);

	$desktop_min = $breakpoints['tablet'] + 1;
	$tablet_min  = $breakpoints['mobile'] + 1;

	$breakpoint_css = $generator->get( 'desktop' );
	if ( trim( $breakpoint_css ) ) {
		$css .= $breakpoint_css . PHP_EOL;
	}

	$breakpoint_css = $generator->get( 'tablet' );
	if ( trim( $breakpoint_css ) ) {
		$css .= "@media (max-width: {$breakpoints['tablet']}px) {
			{$breakpoint_css}
		}" . PHP_EOL;
	}

	$breakpoint_css = $generator->get( 'desktop-only' );
	if ( trim( $breakpoint_css ) ) {
		$css .= "@media (min-width: {$desktop_min}px) {
			{$breakpoint_css}
		}" . PHP_EOL;
	}

	$breakpoint_css = $generator->get( 'tablet-only' );
	if ( trim( $breakpoint_css ) ) {
		$css .= "@media (min-width: {$tablet_min}px) and (max-width: {$breakpoints['tablet']}px) {
			{$breakpoint_css}
		}" . PHP_EOL;
	}

	// 'mobile' breakpoint only applies to mobile aanyway, but we have 'mobile-only' as well, for completeness.
	// Merge the two under one media query.
	$breakpoint_css  = $generator->get( 'mobile' );
	$breakpoint_css .= $generator->get( 'mobile-only' );
	if ( trim( $breakpoint_css ) ) {
		$css .= "@media (max-width: {$breakpoints['mobile']}px) {
			{$breakpoint_css}
		}" . PHP_EOL;
	}

	return apply_filters( 'ci_theme_customizer_css', $css );
}

function ci_theme_get_all_customizer_css() {
	$styles = array(
		'customizer' => ci_theme_get_customizer_css(),
	);

	$styles = apply_filters( 'ci_theme_all_customizer_css', $styles );

	if ( is_customize_preview() ) {
		$styles[] = '/* Placeholder for preview. */';
	}

	return implode( PHP_EOL, $styles );
}


add_filter( 'ci_theme_customizer_css', 'ci_theme_minimize_css' );
function ci_theme_minimize_css( $css ) {
	$css = preg_replace( '/\s+/', ' ', $css );
	return $css;
}
