<?php

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

/**
 * Get all custom css rules from the theme options,
 * minify them and print them out using wp_add_inline_style
 * overriding the theme's default stylesheet.
 * 
 * @return none
 */





function shrk_add_custom_css() {
	global $shrk_options;

	if ( empty( $shrk_options ) ) return;
	
	ob_start();

	$site_width_max_grid = shrk_get_mso( 'site_width_max_grid' );
	$main_content_width  = shrk_get_mso( 'main_content_width' );
	$shop_content_width  = shrk_get_mso( 'shop_width_max_grid' );

	if ( isset( $site_width_max_grid['width'] ) && isset( $site_width_max_grid['units'] ) ) {
		$npmw = $site_width_max_grid['units'] == 'px' ? ( intval( $site_width_max_grid['width'] ) - 55 ) . 'px' : $site_width_max_grid['width'];
		echo '.container { max-width: ' . esc_attr( $site_width_max_grid['width'] ) . '; } ';
		echo '
		.woocommerce .woocommerce-message-wrapper .woocommerce-message,
		.woocommerce .woocommerce-message-wrapper .woocommerce-error,
		.woocommerce .woocommerce-message-wrapper .woocommerce-info { 
			max-width: ' . esc_attr( $npmw ) . '; } ';
	}

	if ( isset( $main_content_width['width'] ) && isset( $main_content_width['units'] ) ) {
		echo '.container.container-main-content { max-width: ' . esc_attr( $main_content_width['width'] ) . '; } ';
	}

	if ( !empty( $shop_content_width['width'] ) && isset( $shop_content_width['units'] ) ) {
		echo '
			.tax-product_cat .container-main-content,
			.post-type-archive-product .container-main-content { 
				max-width: ' . esc_attr( $shop_content_width['width'] ) . '; 
			}';
	}

	if ( $shrk_options['main_theme_color'] ) {
		echo '::selection 		{ color: #fff; background-color: ' . esc_attr( $shrk_options['main_theme_color'] ) . '; }';
		echo '::-moz-selection 	{ color: #fff; background-color: ' . esc_attr( $shrk_options['main_theme_color'] ) . '; }';
	}

	if ( $shrk_options['main_theme_color'] ) {
		echo '.theme-sep path { fill: ' . esc_attr( $shrk_options['main_theme_color'] ) . '; }';
	}	

	if ( isset( $shrk_options['header_height'] ) ) {
		$topbar_height = $shrk_options['topbar_enabled'] == '1' ? 32 : 0; // Static for now. TODO
		$top_offset    = intval($shrk_options['header_height']) + $topbar_height;

		echo ".navbar { height: " . esc_attr( $shrk_options['header_height'] ) . "px; }";
		echo ".transparent-header .page-cover.has-cover-image { padding-top: " . esc_attr( $top_offset ) . "px; }";

		echo ".main-content { padding-top: " . esc_attr( $top_offset ) . "px; }";
		echo ".transparent-header .main-content, .single-pagesections .main-content { padding-top: 0; }";

		// When topbar is hidden via css
		if ( $topbar_height ) {
			echo "@media (max-width: 767px) { ";
			echo ".transparent-header .page-cover.has-cover-image { padding-top: " . esc_attr( $top_offset - $topbar_height ) . "px; }";
			echo ".main-content { padding-top: " . esc_attr( $top_offset - $topbar_height ) . "px; }";
			echo "}";
		}
	}

	if ( isset( $shrk_options['header_height_sticky'] ) ) {
		echo ".site-header.head-sticky .navbar { height: " . esc_attr( $shrk_options['header_height_sticky'] ) . "px; }";
	}

	$logo_max_width        = isset( $shrk_options['header_logo_max_width'] ) ? $shrk_options['header_logo_max_width'] : 0;
	$logo_transp_max_width = isset( $shrk_options['header_logo_transp_max_width'] ) ? $shrk_options['header_logo_transp_max_width'] : 0;

	if ( $logo_max_width ) {
		echo ".site-navigation .navbar-brand.image img.solid-logo { max-width: " . esc_attr( $logo_max_width ) . "px; }";
		echo "@media (max-width: 767px) { ";
		echo ".site-navigation .navbar-brand.image img.solid-logo { max-width: " . esc_attr( $logo_max_width < 200 ? $logo_max_width : 200 ) . "px; }"; // Max out at 200px;
		echo "}";
		echo "@media (max-width: 479px) { ";
		echo ".site-navigation .navbar-brand.image img.solid-logo { max-width: " . esc_attr( $logo_max_width < 140 ? $logo_max_width : 140 ) . "px; }"; // Max out at 140px;
		echo "}";
	}

	if ( $logo_transp_max_width ) {
		echo ".site-navigation .navbar-brand.image img.transp-logo { max-width: " . esc_attr( $logo_transp_max_width ) . "px; }";
		echo "@media (max-width: 767px) { ";
		echo ".site-navigation .navbar-brand.image img.transp-logo { max-width: " . esc_attr( $logo_transp_max_width < 200 ? $logo_transp_max_width : 200 ) . "px; }"; // Max out at 200px;
		echo "}";
		echo "@media (max-width: 479px) { ";
		echo ".site-navigation .navbar-brand.image img.transp-logo { max-width: " . esc_attr( $logo_transp_max_width < 140 ? $logo_transp_max_width : 140 ) . "px; }"; // Max out at 140px;
		echo "}";
	}

	if ( $logo_max_width || $logo_transp_max_width ) {
		$logo_abs_max_width = $logo_max_width > $logo_transp_max_width ? $logo_max_width : $logo_transp_max_width;
		echo ".site-header .site-navigation .navbar .navbar-brand.image { width: " . esc_attr( $logo_abs_max_width ) . "px; }";
		echo "@media (max-width: 767px) { ";
		echo ".site-header .site-navigation .navbar .navbar-brand.image { width: " . esc_attr( $logo_abs_max_width < 200 ? $logo_abs_max_width : 200 ) . "px; }"; // Max out at 200px;
		echo "}";
		echo "@media (max-width: 479px) { ";
		echo ".site-header .site-navigation .navbar .navbar-brand.image { width: " . esc_attr( $logo_abs_max_width < 140 ? $logo_abs_max_width : 140 ) . "px; }"; // Max out at 140px;
		echo "}";
	}

	if ( isset( $shrk_options['sidepanel_left_logo_max_width'] ) ) {
		echo ".side-panel-menu.left .navbar-brand img { max-width: " . esc_attr( $shrk_options['sidepanel_left_logo_max_width'] ) . "px; }";
	}

	if ( isset( $shrk_options['menu_bg_color']['rgba'] ) ) {
		echo '
			.site-header .site-navigation, 
			.site-header.solid-color .site-navigation, 
			.transparent-header .site-header.solid-color .site-navigation { 
				background-color: '. esc_attr( $shrk_options['menu_bg_color']['rgba'] ) . '; 
			}';
	}

	if ( $shrk_options['menu_fg_color'] && isset( $shrk_options['menu_bg_color']['color'] ) ) {
		echo '
			.site-header .site-navigation .navbar .navbar-brand,
			.site-header .site-navigation .navbar .navbar-nav > li > a, 
			.site-header .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a,
			.site-header .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a:visited,
			.site-header.solid-color .site-navigation .navbar .navbar-brand,
			.site-header.solid-color .site-navigation .navbar .navbar-nav > li > a, 
			.site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a,
			.site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a:visited,
			.transparent-header .site-header.solid-color .site-navigation .navbar .navbar-nav > li > a,
			.transparent-header .site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a,
			.transparent-header .site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a:visited,
			.site-header .site-navigation .megamenu > ul.sub-menu > li > a:hover { 
				color: ' . esc_attr( $shrk_options['menu_fg_color'] ) . '; 
				fill: ' . esc_attr( $shrk_options['menu_fg_color'] ) . '; 
			}';

		echo ' .site-navigation .megamenu > .sub-menu li.nolink > a:hover { color: ' . esc_attr( $shrk_options['menu_fg_color'] ) . ' !important; }'; 

		if ( $shrk_options['main_theme_color'] ) {
			echo '
				.site-header .site-navigation .navbar .navbar-nav .megamenu .sub-menu a:hover,
				.site-header .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu .current-menu-item > a,
				.site-header .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a:hover,
				.site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu .sub-menu a:hover,
				.site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu .current-menu-item > a,
				.site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a:hover,
				.transparent-header .site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu .sub-menu a:hover,
				.transparent-header .site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu .current-menu-item > a, 
				.transparent-header .site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a:hover { 
					color: ' . esc_attr( $shrk_options['main_theme_color'] ) . '; 
				}';
		}

		if ( isset( $shrk_options['menu_fg_color_hover'] ) && $shrk_options['menu_fg_color_hover'] ) {
			echo '
				.site-header .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a:hover, 
				.site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu .sub-menu a:hover, 
				.site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu .current-menu-item > a, 
				.site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a:hover,
				.transparent-header .site-header .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a:hover, 
				.transparent-header .site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu .sub-menu a:hover, 
				.transparent-header .site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu .current-menu-item > a, 
				.transparent-header .site-header.solid-color .site-navigation .navbar .navbar-nav .megamenu.fullwidth .sub-menu a:hover,
				header.site-header .header-icon > ul > li:hover > a > i, 
				header.site-header .header-icon > ul > li:focus > a > i, 
				header.site-header .header-icon > ul > li:active > a > i, 
				header.site-header .header-icon > ul > li:hover > a > div > i {
					color: ' . esc_attr( $shrk_options['menu_fg_color_hover'] ) . '; 
				}';

			echo '
				header.site-header .header-icon > ul > li:hover > a > svg, 
				header.site-header .header-icon > ul > li:focus > a > svg, 
				header.site-header .header-icon > ul > li:active > a > svg, 
				header.site-header .header-icon > ul > li:hover > a > div > svg {
					fill: ' . esc_attr( $shrk_options['menu_fg_color_hover'] ) . '; 
				}';

			echo '
				.navbar .navbar-nav > .menu-item > a:before,
				header.site-header .header-icon .product-count,
				header.site-header .header-icon .icon-counter {
					background-color: ' . esc_attr( $shrk_options['menu_fg_color_hover'] ) . '; 
				}';
		}

		echo '
			header.site-header .header-icon.navbar-sep:after {
				background-color: ' . esc_attr( $shrk_options['menu_fg_color'] ) . '
			}';
	}

	if ( $shrk_options['menu_fg_color_transp'] ) {
		echo '.transparent-header .site-header .site-navigation .navbar .navbar-nav > li > a { 
			color: '. esc_attr( $shrk_options['menu_fg_color_transp'] ) . '; 
			fill: '. esc_attr( $shrk_options['menu_fg_color_transp'] ) . '; 
		}'; 
	}

	if ( $shrk_options['menu_fg_color'] && isset( $shrk_options['menu_bg_color']['color'] ) ) {
		echo '
			.transparent-header header.head-sticky .navbar .navbar-nav > li > a,
			.transparent-header header.head-sticky .navbar .navbar-brand { 
				color: '. esc_attr( $shrk_options['menu_fg_color'] ) . '; 
				fill: '. esc_attr( $shrk_options['menu_fg_color'] ) . '; 
		}'; 
	}

	if ( $shrk_options['topbar_fg_color'] && isset( $shrk_options['topbar_fg_color']['hover'] ) ) {
		echo '
			.top-bar, 
			.top-bar a
			{ color: ' . esc_attr( $shrk_options['topbar_fg_color']['regular'] ) . '; }
			';
			
		echo '
			.top-bar a:hover,
			.top-bar ul li:hover > a
			{ color: ' . esc_attr( $shrk_options['topbar_fg_color']['hover'] ) . '; }
			';
	}

	if ( $shrk_options['topbar_fg_color_transp'] && isset( $shrk_options['topbar_fg_color_transp']['hover'] ) ) {
		echo '
			.transparent-header .top-bar, 
			.transparent-header .top-bar a
			{ color: ' . esc_attr( $shrk_options['topbar_fg_color_transp']['regular'] ) . '; }
			';
			
		echo '
			.transparent-header .top-bar a:hover,
			.transparent-header .top-bar ul li:hover > a
			{ color: ' . esc_attr( $shrk_options['topbar_fg_color_transp']['hover'] ) . '; }
			';
	}

	if ( shrk_get_mso( 'cover_overlay_enabled' ) == '1' && shrk_get_mso( 'cover_overlay_opacity_min' ) ) {
		echo '.page-cover .cover-overlay { opacity: ' . esc_attr( intval( shrk_get_mso( 'cover_overlay_opacity_min' ) ) / 100 ) . '}';
	}

	if ( isset( $shrk_options['woo_meta_bg_products_list_color']['rgba'] ) ) {
		echo '
			.woocommerce ul.products.list li.product .product-meta-wrapper .product-meta,
			.woocommerce.products-transparent ul.products.list li.product .product-meta-wrapper .product-meta,
			.woocommerce.products-transparent.products-meta-bg ul.products.list li.product .product-meta-wrapper .product-meta
			{ background-color: '. esc_attr( $shrk_options['woo_meta_bg_products_list_color']['rgba'] ) . '; }
		'; 
	}

	if ( isset( $shrk_options['woo_loop_categories_title_bg']['rgba'] ) ) {
		echo '
			.woocommerce ul.products li.product.product-category h3,
			.woocommerce ul.products li.product.product-category .woocommerce-loop-category__title
			{ background-color: '. esc_attr( $shrk_options['woo_loop_categories_title_bg']['rgba'] ) . '; }
		'; 
	}

	if ( isset( $shrk_options['woo_transparent_product_images'] ) && $shrk_options['woo_transparent_product_images'] == '1' && !empty( $shrk_options['body_background_color']['background-color'] ) ) {
		echo '
			.yith_magnifier_zoom_wrap .yith_magnifier_zoom_magnifier
			{ background-color: '. esc_attr( $shrk_options['body_background_color']['background-color'] ) . '; }
		'; 
	}

	do_action( 'shrk-pre-custom-css' );
	
	/*
	 * Custom CSS
	 */
	
	/* Custom CSS comes last */
	if ( !empty( $shrk_options['custom_css'] ) ) {
		echo str_replace('&gt;', '>', wp_kses( $shrk_options['custom_css'], array( "\'", '\"' ) ) ); 
	}

	$output = shrk_simple_minify( ob_get_clean() );
	wp_add_inline_style( 'shrk-style-overrides', $output );
}


/**
 * shrk_custom_map_style_obj output custom map style
 * 
 * @return none
 */
function shrk_custom_map_style_obj() {
	global $shrk_options;
	if ( !empty( $shrk_options['shrk_map_custom_style'] ) ) {
		$map_style_js = 'if ( typeof(shrk) !== "undefined") { shrk.theme_custom_map_style = ' . $shrk_options['shrk_map_custom_style'] . '; }';
		wp_add_inline_script( 'shrk-theme-js', $map_style_js );
	}
}

?>