<?php
/**
 * Dynamic CSS Style plugin file.
 *
 * @package DCLM\Dynamic-style
 */

	$is_wps_theme = DCLM()->is_wps_theme();

	$disclaimer_atmosphere                = DCLM()->get_xbox_field_with_default( 'dclm-options', 'disclaimer-atmosphere' );
	$disclaimer_logo_opacity              = DCLM()->get_xbox_field_with_default( 'dclm-options', 'logo-disclaimer-opacity' ) / 100;
	$disclaimer_custom_background         = DCLM()->get_xbox_field_with_default( 'dclm-options', 'disclaimer-custom-background' );
	$disclaimer_background_color          = DCLM()->get_xbox_field_with_default( 'dclm-options', 'disclaimer-background-color' );
	$disclaimer_background_color_opacity  = DCLM()->get_xbox_field_with_default( 'dclm-options', 'disclaimer-background-color-opacity' ) / 100;
	$disclaimer_background_image          = DCLM()->get_xbox_field_with_default( 'dclm-options', 'disclaimer-background-image' );
	$disclaimer_background_blur           = DCLM()->get_xbox_field_with_default( 'dclm-options', 'disclaimer-background-blur' );
	$disclaimer_yes_button_color          = DCLM()->get_xbox_field_with_default( 'dclm-options', 'yes-button-color' );
	$disclaimer_no_button_color           = DCLM()->get_xbox_field_with_default( 'dclm-options', 'no-button-color' );
	$disclaimer_logo_disclaimer_grayscale = DCLM()->get_xbox_field_with_default( 'dclm-options', 'logo-disclaimer-grayscale' );

	$disclaimer_background_repeat         = DCLM()->get_xbox_field_with_default( 'dclm-options', 'disclaimer-background-repeat' );
	$disclaimer_background_size           = DCLM()->get_xbox_field_with_default( 'dclm-options', 'disclaimer-background-size' );
	$disclaimer_background_position       = DCLM()->get_xbox_field_with_default( 'dclm-options', 'disclaimer-background-position' );
?>

<style>
	<?php if ( 'dark' === $disclaimer_atmosphere  ) : ?>
		#dclm_modal_screen {
			background-color: rgba(0,0,0,0.8);
		}
		#dclm_modal_content {
			background-color: #000;
		}
		#dclm_modal_content h2 {
			color: #ccc;
		}
		#dclm_modal_content p {
			color: #999;
		}
	<?php else : ?>
		#dclm_modal_screen {
			background-color: rgba(255,255,255,0.8);
		}
		#dclm_modal_content {
			background-color: #fff;
		}
		#dclm_modal_content h2 {
			color: #333;
		}
		#dclm_modal_content p {
			color: #666;
		}
	<?php endif; ?>
	#dclm_modal_content nav .av_go {
		background-color: <?php echo esc_html( $disclaimer_yes_button_color ); ?>!important;
	}
	#dclm_modal_content nav .av_no {
		background-color: <?php echo esc_html( $disclaimer_no_button_color ); ?>!important;
	}
	#dclm-logo img {
		opacity: <?php echo esc_html( $disclaimer_logo_opacity ); ?>;
	}
	<?php if ( 'on' === $disclaimer_logo_disclaimer_grayscale ) : ?>
		#dclm-logo img {
			filter: grayscale(1);
		}
	<?php endif; ?>
	<?php if ( 'on' === $disclaimer_custom_background ) : ?>
		#dclm_modal_screen {
			background-color: <?php echo dclm_hex2rgba( $disclaimer_background_color, $disclaimer_background_color_opacity ); ?>!important;
			backdrop-filter: blur(<?php echo $disclaimer_background_blur; ?>px)!important;
		}
	<?php endif; ?>
	<?php if ( 'on' === $disclaimer_custom_background && '' !== $disclaimer_background_image ) : ?>
		#dclm_modal_screen {
			background-image: url('<?php echo $disclaimer_background_image; ?>');
			background-repeat: <?php echo $disclaimer_background_repeat; ?>;
			background-size: <?php echo $disclaimer_background_size; ?>;
			background-position: <?php echo $disclaimer_background_position; ?>;
		}
	<?php endif; ?>
</style>


<?php
function dclm_hex2rgba( $color, $opacity = false ) {

	$default = 'rgb(0,0,0)';

	// Return default if no color provided
	if ( empty( $color ) ) {
		return $default;
	}

	// Sanitize $color if "#" is provided
	if ( $color[0] == '#' ) {
		$color = substr( $color, 1 );
	}

	 // Check if color has 6 or 3 characters and get values
	if ( strlen( $color ) == 6 ) {
			$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
	} elseif ( strlen( $color ) == 3 ) {
			$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
	} else {
			return $default;
	}

	 // Convert hexadec to rgb
	 $rgb = array_map( 'hexdec', $hex );

	 // Check if opacity is set(rgba or rgb)
	if ( $opacity ) {
		if ( abs( $opacity ) > 1 ) {
			$opacity = 1.0;
		}
		$output = 'rgba(' . implode( ',', $rgb ) . ',' . $opacity . ')';
	} else {
		$output = 'rgb(' . implode( ',', $rgb ) . ')';
	}

	 // Return rgb(a) color string
	 return $output;
}
