<?php
/**
 * Customizer CSS Output
 *
 * Generates inline CSS from theme customizer settings.
 * Replaces Kirki's automatic CSS output.
 *
 * @package Brooklyn_Lite
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Generate and output inline CSS from customizer settings.
 */
function brooklyn_lite_customizer_css() {
	$css = '';

	// --- Primary Color ---
	$color_primary = get_theme_mod( 'color_primary', '#ffd200' );

	if ( '#ffd200' !== $color_primary ) {
		$css .= sprintf(
			'.woocommerce div.product .woocommerce-tabs ul.tabs li.active a,
			.product div.entry-summary p.price,
			.byline a:hover,
			.single-post .posted-on a,
			.blog-loop .posted-on a,
			.entry-title a:hover,
			.brooklyn_lite_recent_entries .post-date,
			.menuStyle3 .top-bar .contact-item .fa,
			.widget_categories li:hover a,
			.post-navigation a { color: %1$s; }
			.call-to-action .btn:hover { border-color: %1$s; }
			.copyright a { background-color: %1$s; }
			header .navbar .navbar-collapse .navbar-nav li a:hover,
			header .navbar .navbar-collapse .navbar-nav li a:focus,
			header .navbar .navbar-collapse .navbar-nav .current-menu-item > a { color: %1$s; }
			.banner-contents .btn { border-color: %1$s; color: %1$s; }
			.entry-footer .read-more:hover { border-color: %1$s; color: %1$s; }',
			esc_attr( $color_primary )
		);
	}

	// --- Header Colors ---
	$header_colors = array(
		array(
			'setting'  => 'color_mt1_site_title',
			'default'  => '#ffffff',
			'selector' => '.header-default a.navbar-brand',
			'property' => 'color',
		),
		array(
			'setting'  => 'color_mt1_site_desc',
			'default'  => '#ffffff',
			'selector' => '.main-header .site-description',
			'property' => 'color',
		),
		array(
			'setting'  => 'color_mt1_top_menu_items',
			'default'  => '#262626',
			'selector' => '.main-header #main-menu a, .header-default .fa-search',
			'property' => 'color',
		),
		array(
			'setting'  => 'color_mt1_bg_color',
			'default'  => '',
			'selector' => '.main-header',
			'property' => 'background-color',
		),
		array(
			'setting'  => 'color_submenu_items',
			'default'  => '#262626',
			'selector' => '#main-menu .sub-menu li a',
			'property' => 'color',
		),
		array(
			'setting'  => 'color_submenu_bg',
			'default'  => '#fff',
			'selector' => '#main-menu ul ul li',
			'property' => 'background-color',
		),
		array(
			'setting'  => 'mobile_toggle_color',
			'default'  => '',
			'selector' => '.navbar-toggler-icon, .main-menu li.menu-item-has-children > .subnav-toggle, .main-menu li.page_item_has_children > .subnav-toggle',
			'property' => 'color',
		),
	);

	foreach ( $header_colors as $rule ) {
		$value = get_theme_mod( $rule['setting'], $rule['default'] );
		if ( $value && $value !== $rule['default'] ) {
			$css .= sprintf( '%s { %s: %s; }', $rule['selector'], $rule['property'], esc_attr( $value ) );
		}
	}

	// --- Blog Colors ---
	$blog_colors = array(
		array(
			'setting'  => 'color_index_post_title',
			'default'  => '#262626',
			'selector' => '.page-header .page-title',
			'property' => 'color',
		),
		array(
			'setting'  => 'color_single_post_title',
			'default'  => '#262626',
			'selector' => '.blog-posts .entry-title',
			'property' => 'color',
		),
		array(
			'setting'  => 'color_meta_cat_bg',
			'default'  => '#fff',
			'selector' => '.blog-posts .cat-links',
			'property' => 'background-color',
		),
		array(
			'setting'  => 'color_meta_text',
			'default'  => '#999',
			'selector' => '.single .entry-meta',
			'property' => 'color',
		),
		array(
			'setting'  => 'color_meta_links',
			'default'  => '#999',
			'selector' => '.single .entry-meta a',
			'property' => 'color',
		),
		array(
			'setting'  => 'color_post_text',
			'default'  => '#262626',
			'selector' => '.single-post .entry-content, .blog-loop .entry-content, article .entry-content p',
			'property' => 'color',
		),
	);

	foreach ( $blog_colors as $rule ) {
		$value = get_theme_mod( $rule['setting'], $rule['default'] );
		if ( $value && $value !== $rule['default'] ) {
			$css .= sprintf( '%s { %s: %s; }', $rule['selector'], $rule['property'], esc_attr( $value ) );
		}
	}

	// --- Font Sizes ---
	$font_sizes = array(
		array(
			'setting'  => 'font_size_body',
			'default'  => 16,
			'selector' => 'body, article .entry-content p',
		),
		array(
			'setting'  => 'font_size_site_title',
			'default'  => 36,
			'selector' => '.site-title',
		),
		array(
			'setting'  => 'font_size_site_desc',
			'default'  => 16,
			'selector' => '.site-description',
		),
		array(
			'setting'  => 'font_size_menu_top_items',
			'default'  => 16,
			'selector' => '#main-menu li',
		),
		array(
			'setting'  => 'font_size_menu_sub_items',
			'default'  => 13,
			'selector' => '#main-menu ul ul li',
		),
		array(
			'setting'  => 'font_size_index_title',
			'default'  => '',
			'selector' => '.archive article .entry-title, .category article .entry-title',
		),
		array(
			'setting'  => 'font_size_single_title',
			'default'  => 36,
			'selector' => '.single article .entry-title',
		),
		array(
			'setting'  => 'font_size_footer_social_icons',
			'default'  => 16,
			'selector' => '.footer-social a',
		),
		array(
			'setting'  => 'font_size_footer_credits',
			'default'  => 13,
			'selector' => 'footer.site-footer',
		),
	);

	foreach ( $font_sizes as $rule ) {
		$value   = get_theme_mod( $rule['setting'], $rule['default'] );
		$default = $rule['default'];

		if ( '' !== $value && $value !== $default && (int) $value !== (int) $default ) {
			$css .= sprintf( '%s { font-size: %spx; }', $rule['selector'], intval( $value ) );
		}
	}

	// --- Typography (Font Family) ---
	$headings_font = get_theme_mod( 'headings_font_family', 'Muli' );
	$body_font     = get_theme_mod( 'body_font_family', 'Montserrat' );

	if ( 'Muli' !== $headings_font ) {
		$css .= sprintf(
			'h1, h2, h3, h4, h5, h6, .site-title, .entry-title, .section-title { font-family: %s, sans-serif; }',
			esc_attr( $headings_font )
		);
	}

	if ( 'Montserrat' !== $body_font ) {
		$css .= sprintf(
			'body, article .entry-content p { font-family: %s, sans-serif; }',
			esc_attr( $body_font )
		);
	}

	// Output inline CSS if we have any.
	if ( ! empty( $css ) ) {
		wp_add_inline_style( 'brooklyn-lite-style', $css );
	}
}
add_action( 'wp_enqueue_scripts', 'brooklyn_lite_customizer_css', 20 );

// Google Fonts are enqueued via brooklyn_lite_google_fonts_url() in functions.php.
