<?php
/**
 * Style functions
 *
 * @package wpstream-theme
 */

require_once 'customizer-settings/customizer-dashboard-options.php';
require_once 'customizer-settings/customizer-comments-settings.php';
require_once 'customizer-settings/customizer-metadata-share-settings.php';
require_once 'customizer-settings/customizer-taxonomies-settings.php';

/**
 * @param $customizer_styles
 *
 * @return string
 */
function wpstream_generate_customizer_styles( $customizer_styles ) {
	$css_style = '';

	foreach ( $customizer_styles as $type => $styles ) {
		$css_style .= "\r\t/* $type variable START */";

		foreach ( $styles as $style ) {
			if ( ! empty( $style['value'] ) ) {
				$css_style .= "\n\t" . esc_attr( $style['var'] ) . ' : ' . esc_attr( $style['value'] ) . ( ! empty( $style['unit'] ) ? esc_attr( $style['unit'] ) : '' ) . ';';
			}
		}

		$css_style .= "\n\t/* $type variable END */\n";
	}

	return $css_style;
}

/**
 * Prints customizer style variables
 */
add_action( 'wp_head', 'wpstream_print_style_customizer', 1 );
if ( ! function_exists( 'wpstream_print_style_customizer' ) ) {
	/**
	 * Print style customizer
	 */
	function wpstream_print_style_customizer() {
		// Header menu font color and hover color from customizer.

		$customizer_styles = array(
			'header'                         => array(
				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_menu_font_color' ) : get_theme_mod( 'wpstream_theme_menu_font_color' ),
					'var'   => '--wpstream-header-menu-font-color',
				),


				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_menu_font_color_hover' ) : get_theme_mod( 'wpstream_theme_menu_font_color_hover' ),
					'var'   => '--wpstream_theme_menu_font_color_hover',
				),

				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_menu_back_color_hover' ) : get_theme_mod( 'wpstream_theme_menu_back_color_hover' ),
					'var'   => '--wpstream_theme_menu_back_color_hover',
				),

				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_menu_font_size' ) : get_theme_mod( 'wpstream_theme_menu_font_size' ),
					'var'   => '--wpstream-header-menu-font-size',
					'unit'  => 'px',
				),
				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_submenu_background_color' ) : get_theme_mod( 'wpstream_theme_submenu_background_color' ),
					'var'   => '--wpstream-header-submenu-background-color',
				),
				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_submenu_font_color' ) : get_theme_mod( 'wpstream_theme_submenu_font_color' ),
					'var'   => '--wpstream-header-submenu-font-color',
				),
				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_submenu_font_color_hover' ) : get_theme_mod( 'wpstream_theme_submenu_font_color_hover' ),
					'var'   => '--wpstream-header-submenu-font-color-hover',
				),
				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_submenu_font_size' ) : get_theme_mod( 'wpstream_theme_submenu_font_size' ),
					'var'   => '--wpstream-header-submenu-font-size',
					'unit'  => 'px',
				),
				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_search_form_border_color' ) : get_theme_mod( 'wpstream_theme_search_form_border_color' ),
					'var'   => '--wpstream-header-search-border-color',
				),
				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_menu_item_back_color_hover' ) : get_theme_mod( 'wpstream-header-menu-item-back-color-hover' ),
					'var'   => '--wpstream-header-menu-item-back-color-hover',
				),
				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_border_bottom_color' ) : get_theme_mod( 'wpstream_theme_header_border_bottom_color' ),
					'var'   => '--wpstream-header-border-bottom-color',
				),
				array(
					'value' => wpstream_theme_is_transparent_header() ? get_theme_mod( 'wpstream_theme_transparent_header_border_bottom_height' ) : get_theme_mod( 'wpstream_theme_header_border_bottom_height' ),
					'var'   => '--wpstream-header-border-bottom-height',
					'unit'  => 'px',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_max_height_logo' ),
					'var'   => '--wpstream-header-max-height-logo',
					'unit'  => 'px',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_max_width_logo' ),
					'var'   => '--wpstream-header-max-width-logo',
					'unit'  => 'px',
				),
				array(
					'value' => wpstream_hex_2_rgba( get_theme_mod( 'wpstream_theme_header_background_color' ), get_theme_mod( 'wpstream_theme_header_background_opacity' ) ),
					'var'   => '--wpstream-header-background-rgba',
				),
			),
			'footer'                         => array(
				array(
					'value' => get_theme_mod( 'wpstream_theme_footer_background_color' ),
					'var'   => '--wpstream-footer-background-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_footer_sidebar_title_color' ),
					'var'   => '--wpstream-footer-sidebar-title-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_footer_sidebar_title_size' ),
					'var'   => '--wpstream-footer-sidebar-title-size',
					'unit'  => 'px',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_footer_sidebar_item_list_color' ),
					'var'   => '--wpstream-footer-sidebar-item-list-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_footer_sidebar_item_list_color_hover' ),
					'var'   => '--wpstream-footer-sidebar-item-list-color-hover',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_footer_sidebar_item_list_size' ),
					'var'   => '--wpstream-footer-sidebar-item-list-size',
					'unit'  => 'px',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_footer_copyright_area_background_color' ),
					'var'   => '--wpstream-footer-copyright-area-background-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_footer_copyright_area_text_color' ),
					'var'   => '--wpstream-footer-copyright-area-text-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_footer_copyright_area_text_size' ),
					'var'   => '--wpstream-footer-copyright-area-text-size',
					'unit'  => 'px',
				),
			),
			'sidebar'                        => array(
				array(
					'value' => get_theme_mod( 'wpstream_theme_sidebar_title_color' ),
					'var'   => '--wpstream-sidebar-title-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_sidebar_title_size' ),
					'var'   => '--wpstream-sidebar-title-size',
					'unit'  => 'px',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_sidebar_item_color' ),
					'var'   => '--wpstream-sidebar-item-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_sidebar_item_color_hover' ),
					'var'   => '--wpstream-sidebar-item-color-hover',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_sidebar_item_size' ),
					'var'   => '--wpstream-sidebar-item-size',
					'unit'  => 'px',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_sidebar_widget_background_color' ),
					'var'   => '--wpstream-sidebar-background-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_sidebar_widget_padding_top' ),
					'var'   => '--wpstream-sidebar-padding-top',
					'unit'  => 'px',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_sidebar_widget_padding_right' ),
					'var'   => '--wpstream-sidebar-padding-right',
					'unit'  => 'px',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_sidebar_widget_padding_bottom' ),
					'var'   => '--wpstream-sidebar-padding-bottom',
					'unit'  => 'px',
				),
				array(
					'value' => get_theme_mod( 'wpstream_theme_sidebar_widget_padding_left' ),
					'var'   => '--wpstream-sidebar-padding-left',
					'unit'  => 'px',
				),
			),
			'video-unit-card'                => array(
				array(
					'value' => get_theme_mod( 'wpstream_unit_card_title_color' ),
					'var'   => '--wpstream-unit-card-title-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_unit_card_title_hover_color' ),
					'var'   => '--wpstream-unit-card-title-hover-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_unit_card_author_name_color' ),
					'var'   => '--wpstream-unit-card-author-name-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_unit_card_views_color' ),
					'var'   => '--wpstream-unit-card-views-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_unit_card_views_background_color' ),
					'var'   => '--wpstream-unit-card-views-background-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_unit_card_image_border_radius' ),
					'var'   => '--wpstream-unit-card-image-border-radius',
					'unit'  => 'px',
				),
				/*	array(
					'value' => get_theme_mod( 'wpstream_unit_card_image_height' ),
					'var'   => '--wpstream-unit-card-image-height',
					'unit'  => 'px',
				),
				*/
			),
			'blog-post-unit-card'            => array(
				array(
					'value' => get_theme_mod( 'wpstream_blog_post_unit_card_title_color' ),
					'var'   => '--wpstream-blog-post-unit-card-title-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_blog_post_unit_card_meta_color' ),
					'var'   => '--wpstream-blog-post-unit-meta-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_blog_post_unit_card_views_color' ),
					'var'   => '--wpstream-blog-post-unit-card-views-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_blog_post_unit_card_views_background_color' ),
					'var'   => '--wpstream-blog-post-unit-card-views-background-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_blog_post_unit_card_image_border_radius' ),
					'var'   => '--wpstream-blog-post-unit-card-image-border-radius',
					'unit'  => 'px',
				),
				/*array(
					'value' => get_theme_mod( 'wpstream_blog_post_unit_card_image_height' ),
					'var'   => '--wpstream-blog-post-unit-card-image-height',
					'unit'  => 'px',
				),
				*/
			),
			'general-design-settings-colors' => array(
				array(
					'value' => intval(get_theme_mod( 'wpstream_theme_width_container' )),
					'var'   => '--wpstream-theme-width-container',
					'unit'  => 'px',
				),

				// Color options for the type 1 button - color, background, and background hover
				array(
					'value' => get_theme_mod( 'wpstream_type_1_button_text_color' ),
					'var'   => '--wpstream-type-1-button-text-color',
				),
				array(
					'value' => get_theme_mod('wpstream_type_1_button_background_color_gradient_angle'),
					'var'   => '--wpstream-type-1-button-background-color-gradient-angle',
					'unit' => 'deg'
				),
				array(
					'value' => wpstream_is_type_1_button_background_gradient() ?
						get_theme_mod('wpstream_type_1_button_background_color_gradient_first_color') :
						get_theme_mod('wpstream_type_1_button_background_color_option_simple'),
					'var'   => '--wpstream_type_1_button_background_color_gradient_first_color',
				),
				array(
					'value' => wpstream_is_type_1_button_background_gradient() ?
						get_theme_mod('wpstream_type_1_button_background_color_gradient_second_color') :
						get_theme_mod('wpstream_type_1_button_background_color_option_simple'),
					'var'   => '--wpstream_type_1_button_background_color_gradient_second_color',
				),
				array(
					'value' => get_theme_mod('wpstream_type_1_button_hover_background_color_gradient_angle'),
					'var'   => '--wpstream-type-1-button-hover_background-color-gradient-angle',
					'unit' => 'deg'
				),
				array(
					'value' => wpstream_is_type_1_button_hover_background_gradient() ?
						get_theme_mod('wpstream_type_1_button_hover_background_color_gradient_first_color') :
						get_theme_mod('wpstream_type_1_button_hover_background_color_option_simple'),
					'var'   => '--wpstream_type_1_button_hover_background_color_gradient_first_color',
				),
				array(
					'value' => wpstream_is_type_1_button_hover_background_gradient() ?
						get_theme_mod('wpstream_type_1_button_hover_background_color_gradient_second_color') :
						get_theme_mod('wpstream_type_1_button_hover_background_color_option_simple'),
					'var'   => '--wpstream_type_1_button_hover_background_color_gradient_second_color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_type_1_button_border_width' ),
					'var'   => '--wpstream-type-1-button-border-width',
				),
				array(
					'value' => get_theme_mod( 'wpstream_type_1_button_border_color' ),
					'var'   => '--wpstream-type-1-button-border-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_type_1_button_border_hover_color' ),
					'var'   => '--wpstream-type-1-button-border-hover-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_type_1_button_border_radius' ),
					'var'   => '--wpstream-type-1-button-border-radius',
				),
				array(
					'value' => get_theme_mod( 'wpstream_type_1_button_opacity' ),
					'var'   => '--wpstream-type-1-button-opacity',
				),

				// Color options for the type 2 button - color, background, and background hover
				array(
					'value' => get_theme_mod( 'wpstream_type_2_button_text_color' ),
					'var'   => '--wpstream-type-2-button-text-color',
				),
				array(
					'value' => get_theme_mod('wpstream_type_2_button_background_color_gradient_angle'),
					'var'   => '--wpstream-type-2-button-background-color-gradient-angle',
					'unit' => 'deg'
				),
				array(
						'value' => wpstream_is_type_2_button_background_gradient() ?
							get_theme_mod('wpstream_type_2_button_background_color_gradient_first_color') :
							get_theme_mod('wpstream_type_2_button_background_color_option_simple'),
						'var'   => '--wpstream_type_2_button_background_color_gradient_first_color',
				),
				array(
						'value' => wpstream_is_type_2_button_background_gradient() ?
							get_theme_mod('wpstream_type_2_button_background_color_gradient_second_color') :
							get_theme_mod('wpstream_type_2_button_background_color_option_simple'),
						'var'   => '--wpstream_type_2_button_background_color_gradient_second_color',
				),
				array(
					'value' => get_theme_mod('wpstream_type_2_button_hover_background_color_gradient_angle'),
					'var'   => '--wpstream-type-2-button-hover_background-color-gradient-angle',
					'unit' => 'deg'
				),
				array(
					'value' => wpstream_is_type_2_button_hover_background_gradient() ?
						get_theme_mod('wpstream_type_2_button_hover_background_color_gradient_first_color') :
						get_theme_mod('wpstream_type_2_button_hover_background_color_option_simple'),
					'var'   => '--wpstream_type_2_button_hover_background_color_gradient_first_color',
				),
				array(
					'value' => wpstream_is_type_2_button_hover_background_gradient() ?
						get_theme_mod('wpstream_type_2_button_hover_background_color_gradient_second_color') :
						get_theme_mod('wpstream_type_2_button_hover_background_color_option_simple'),
					'var'   => '--wpstream_type_2_button_hover_background_color_gradient_second_color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_type_2_button_border_width' ),
					'var'   => '--wpstream-type-2-button-border-width',
				),
				array(
					'value' => get_theme_mod( 'wpstream_type_2_button_border_color' ),
					'var'   => '--wpstream-type-2-button-border-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_type_2_button_border_hover_color' ),
					'var'   => '--wpstream-type-2-button-border-hover-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_type_2_button_border_radius' ),
					'var'   => '--wpstream-type-2-button-border-radius',
				),
				array(
					'value' => get_theme_mod( 'wpstream_type_2_button_opacity' ),
					'var'   => '--wpstream-type-2-button-opacity',
				),

				// Style of WooCommerce cart
				array(
					'value' => get_theme_mod('wpstream_woocommerce_cart_font_size'),
					'var'   => '--wpstream_woocommerce_cart_font_size',
					'unit'  => 'px',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_cart_font_color'),
					'var'   => '--wpstream_woocommerce_cart_font_color',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_cart_product_name_color'),
					'var'   => '--wpstream_woocommerce_cart_product_name_color',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_cart_product_name_hover_color'),
					'var'   => '--wpstream_woocommerce_cart_product_name_hover_color',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_add_to_cart_color'),
					'var'   => '--wpstream_woocommerce_add_to_cart_color',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_add_to_cart_hover_color'),
					'var'   => '--wpstream_woocommerce_add_to_cart_hover_color',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_header_search_button_color'),
					'var'   => '--wpstream_woocommerce_header_search_button_color',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_header_search_button_hover_color'),
					'var'   => '--wpstream_woocommerce_header_search_button_hover_color',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_header_cart_button_color'),
					'var'   => '--wpstream_woocommerce_header_cart_button_color',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_header_cart_button_hover_color'),
					'var'   => '--wpstream_woocommerce_header_cart_button_hover_color',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_header_account_button_color'),
					'var'   => '--wpstream_woocommerce_header_account_button_color',
				),
				array(
					'value' => get_theme_mod('wpstream_woocommerce_header_account_button_hover_color'),
					'var'   => '--wpstream_woocommerce_header_account_button_hover_color',
				),

				// Style of the broadcaster section
				array(
					'value' => get_theme_mod( 'wpstream_broadcaster_section_title_color' ),
					'var'   => '--wpstream-broadcaster-section-title-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_broadcaster_section_background_color' ),
					'var'   => '--wpstream-broadcaster-section-background-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_broadcaster_section_link_color' ),
					'var'   => '--wpstream-broadcaster-section-link-color',
				),
				array(
					'value' => get_theme_mod( 'wpstream_broadcaster_section_link_hover_color' ),
					'var'   => '--wpstream-broadcaster-section-link-hover-color',
				),

			),
		);

		// Style of the taxonomies section
        $customizer_styles['general-design-settings-colors'] =
            array_merge( $customizer_styles['general-design-settings-colors'], wpstream_get_taxonomies_tabs_color_settings() );

		// Style of the metadata share section
		$customizer_styles['general-design-settings-colors'] =
			array_merge( $customizer_styles['general-design-settings-colors'], wpstream_get_metadata_share_settings() );

		// Style of the comments section
		$customizer_styles['general-design-settings-colors'] =
			array_merge( $customizer_styles['general-design-settings-colors'], wpstream_get_comments_settings() );

		// Style of the dashboard section
		$customizer_styles['general-design-settings-colors'] =
			array_merge( $customizer_styles['general-design-settings-colors'], wpstream_get_dashboard_style_options() );

		?>

		<style>
			:root{
				<?php echo wpstream_generate_customizer_styles( $customizer_styles ); ?>
			}
			/* Customizer fonts */
			<?php
			$fonts_data = wpstream_get_customizer_fonts_data();
			$selectors  = array(
				'header'             => '.site-header .bg-light *:where(:not(i))',
				'transparent-header' => '.site-header .wpstream_transparent_header *:where(:not(i))',
				'sidebar'            => '#secondary *:where(:not(i))',
				'footer'             => 'footer *:where(:not(i))',
				'body'               => 'body',
				'links'              => 'a',
				'h1'                 => 'h1',
				'h2'                 => 'h2',
				'h3'                 => 'h3',
				'h4'                 => 'h4',
				'h5'                 => 'h5',
				'h6'                 => 'h6',
			);

			foreach ( $fonts_data as $type => $font ) {
				$font['font_family'] = str_replace( array( 'system_fonts_', 'google_fonts_' ), '', $font['font_family'] );

				if ( isset( $selectors[ $type ] ) ) {
					echo "\r" . $selectors[ $type ] . "{\n";

					if ( ! empty( $font['font_family'] ) ) {
						echo "\tfont-family : " . $font['font_family'] . ", sans-serif!important;\n";
					}

					if ( ! empty( $font['font_weight'] ) ) {
							echo "\tfont-weight : " . $font['font_weight'] . "!important;\n";
					}

					if ( ! empty( $font['font_style'] ) ) {
							echo "\tfont-style : " . $font['font_style'] . "!important;\n";
					}

					echo "}\n";
				}
			}

			?>
		</style>

		<?php
	}
}
