<?php

if ( ! function_exists( 'oaza_mikado_header_top_bar_styles' ) ) {
	/**
	 * Generates styles for header top bar
	 */
	function oaza_mikado_header_top_bar_styles() {
		$top_header_height = oaza_mikado_options()->getOptionValue( 'top_bar_height' );
		
		if ( ! empty( $top_header_height ) ) {
			echo oaza_mikado_dynamic_css( '.mkdf-top-bar', array( 'height' => oaza_mikado_filter_px( $top_header_height ) . 'px' ) );
			echo oaza_mikado_dynamic_css( '.mkdf-top-bar .mkdf-logo-wrapper a', array( 'max-height' => oaza_mikado_filter_px( $top_header_height ) . 'px' ) );
		}
		
		echo oaza_mikado_dynamic_css( '.mkdf-header-box .mkdf-top-bar-background', array( 'height' => oaza_mikado_get_top_bar_background_height() . 'px' ) );
		
		$top_bar_container_selector = '.mkdf-top-bar > .mkdf-vertical-align-containers';
		$top_bar_container_styles   = array();
		$container_side_padding     = oaza_mikado_options()->getOptionValue( 'top_bar_side_padding' );
		$top_bar_in_grid            = oaza_mikado_options()->getOptionValue( 'top_bar_in_grid' ) == 'yes' ? true : false;
		
		if ( $container_side_padding !== '' ) {
			if ( oaza_mikado_string_ends_with( $container_side_padding, 'px' ) || oaza_mikado_string_ends_with( $container_side_padding, '%' ) ) {
				$top_bar_container_styles['padding-left'] = $container_side_padding;
				$top_bar_container_styles['padding-right'] = $container_side_padding;
			} else {
				$top_bar_container_styles['padding-left'] = oaza_mikado_filter_px( $container_side_padding ) . 'px';
				$top_bar_container_styles['padding-right'] = oaza_mikado_filter_px( $container_side_padding ) . 'px';
			}
			
			echo oaza_mikado_dynamic_css( $top_bar_container_selector, $top_bar_container_styles );
		}
		
		if ( $top_bar_in_grid ) {
			$top_bar_grid_selector                = '.mkdf-top-bar .mkdf-grid .mkdf-vertical-align-containers';
			$top_bar_grid_styles                  = array();
			$top_bar_grid_background_color        = oaza_mikado_options()->getOptionValue( 'top_bar_grid_background_color' );
			$top_bar_grid_background_transparency = oaza_mikado_options()->getOptionValue( 'top_bar_grid_background_transparency' );
			
			if ( !empty($top_bar_grid_background_color) ) {
				$grid_background_color        = $top_bar_grid_background_color;
				$grid_background_transparency = 1;
				
				if ( $top_bar_grid_background_transparency !== '' ) {
					$grid_background_transparency = $top_bar_grid_background_transparency;
				}
				
				$grid_background_color                   = oaza_mikado_rgba_color( $grid_background_color, $grid_background_transparency );
				$top_bar_grid_styles['background-color'] = $grid_background_color;
			}
			
			echo oaza_mikado_dynamic_css( $top_bar_grid_selector, $top_bar_grid_styles );
		}

		$top_bar_styles      = array();
		$background_color    = oaza_mikado_options()->getOptionValue( 'top_bar_background_color' );
		$border_color        = oaza_mikado_options()->getOptionValue( 'top_bar_border_color' );
		$border_transparency = oaza_mikado_options()->getOptionValue( 'top_bar_border_transparency' );

		if ($border_transparency === '') {
			$border_transparency = 1;
		}

		if ( $background_color !== '' ) {
			$background_transparency = 1;
			if ( oaza_mikado_options()->getOptionValue( 'top_bar_background_transparency' ) !== '' ) {
				$background_transparency = oaza_mikado_options()->getOptionValue( 'top_bar_background_transparency' );
			}
			
			$background_color                   = oaza_mikado_rgba_color( $background_color, $background_transparency );
			$top_bar_styles['background-color'] = $background_color;
			
			echo oaza_mikado_dynamic_css( '.mkdf-header-box .mkdf-top-bar-background', array( 'background-color' => $background_color ) );
		}
		
		if ( oaza_mikado_options()->getOptionValue( 'top_bar_border' ) == 'yes' && $border_color != '' ) {

			if ( ! $top_bar_in_grid ) {
				$top_bar_styles['border-bottom'] = '1px solid ' . oaza_mikado_rgba_color( $border_color, $border_transparency );
			} else {
				$inner_top_bar_styles   = array();
				$inner_top_bar_styles['border-bottom'] = '1px solid ' . oaza_mikado_rgba_color( $border_color, $border_transparency );

				echo oaza_mikado_dynamic_css( '.mkdf-top-bar .mkdf-grid .mkdf-vertical-align-containers', $inner_top_bar_styles );
			}
		}
		
		echo oaza_mikado_dynamic_css( '.mkdf-top-bar', $top_bar_styles );
	}
	
	add_action( 'oaza_mikado_action_style_dynamic', 'oaza_mikado_header_top_bar_styles' );
}