<?php
/**
 * Wellness Coach Lite Dynamic Styles
 * 
 * @package Wellness_Coach_Lite
*/

function wellness_coach_lite_dynamic_css(){
    
    $primary_font    = get_theme_mod( 'primary_font', 'Questrial' );
    $primary_fonts   = wellness_coach_lite_get_fonts( $primary_font, 'regular' );
    $secondary_font  = get_theme_mod( 'secondary_font', 'Roboto Slab' );
    $secondary_fonts = wellness_coach_lite_get_fonts( $secondary_font, 'regular' );
    $font_size       = get_theme_mod( 'font_size', 18 );
    
    $site_title_font      = get_theme_mod( 'site_title_font', array( 'font-family'=>'Roboto Slab', 'variant'=>'regular' ) );
    $site_title_fonts     = wellness_coach_lite_get_fonts( $site_title_font['font-family'], $site_title_font['variant'] );
    $site_title_font_size = get_theme_mod( 'site_title_font_size', 28 );

    $site_title_color     = get_theme_mod( 'site_title_color', '#111111' );
    
    $logo_width       = get_theme_mod( 'logo_width', 250 );
    $background_color = get_theme_mod( 'background-color', '#ffffff' );

    $wheeloflife_color = get_theme_mod( 'wheeloflife_color', '#f3fcf9' );
   
    $rgb4 = wellness_coach_lite_hex2rgb( wellness_coach_lite_sanitize_hex_color( $background_color ) ); 
         
    echo "<style type='text/css' media='all'>"; ?>

    body{
        font-size: <?php echo absint( $font_size ); ?>px;
        --background-color: <?php echo wellness_coach_lite_sanitize_hex_color( $background_color ); ?>;
        }

    :root {
        --primary-font: <?php echo esc_html( $primary_fonts['font'] ); ?>;
        --secondary-font: <?php echo esc_html( $secondary_fonts['font'] ); ?>;
        --background-color: <?php echo wellness_coach_lite_sanitize_hex_color( $background_color ); ?>;
        --background-color-rgb: <?php printf( '%1$s, %2$s, %3$s', $rgb4[0], $rgb4[1], $rgb4[2] ); ?>;
    }

    .site-title a {    
        font-size   : <?php echo absint( $site_title_font_size ); ?>px;
        font-family : <?php echo esc_html( $site_title_fonts['font'] ); ?>;
        font-weight : <?php echo esc_html( $site_title_fonts['weight'] ); ?>;
        font-style  : <?php echo esc_html( $site_title_fonts['style'] ); ?>; 
        color       : <?php echo wellness_coach_lite_sanitize_hex_color( $site_title_color ); ?>;       
    }

    .custom-logo-link img{
        width    : <?php echo absint( $logo_width ); ?>px;
        max-width: 100%;
    }
     
    section#wheeloflife_section {
        background-color: <?php echo wellness_coach_lite_sanitize_hex_color( $wheeloflife_color ); ?>;
    }
       
    <?php echo "</style>";
}
add_action( 'wp_head', 'wellness_coach_lite_dynamic_css', 99 );

/**
 * Function for sanitizing Hex color 
 */
function wellness_coach_lite_sanitize_hex_color( $color ){
	if ( '' === $color )
		return '';

    // 3 or 6 hex digits, or the empty string.
	if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
		return $color;
}

/**
 * convert hex to rgb
 * @link http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/
*/
function wellness_coach_lite_hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }
   $rgb = array($r, $g, $b);
   //return implode(",", $rgb); // returns the rgb values separated by commas
   return $rgb; // returns an array with the rgb values
}

/**
 * Convert '#' to '%23'
*/
function wellness_coach_hash_to_percent23( $color_code ){
    $color_code = str_replace( "#", "%23", $color_code );
    return $color_code;
}

if ( ! function_exists( 'wellness_coach_lite_gutenberg_inline_style' ) ) : 
/**
 * Gutenberg Dynamic Style
 */
function wellness_coach_lite_gutenberg_inline_style(){
 
    $primary_font     = get_theme_mod( 'primary_font', 'Questrial' );
    $primary_fonts    = wellness_coach_lite_get_fonts( $primary_font, 'regular' );
    $secondary_font   = get_theme_mod( 'secondary_font', 'Roboto Slab' );
    $secondary_fonts  = wellness_coach_lite_get_fonts( $secondary_font, 'regular' );
    $background_color = get_theme_mod( 'background_custom_color', '#ffffff' );

    $rgb4 = wellness_coach_lite_hex2rgb(wellness_coach_lite_sanitize_hex_color( $background_color ) );
 
    
    $custom_css = ':root .block-editor-page {
        --primary-font: ' . esc_html($primary_fonts['font']) . ';
        --secondary-font: ' . esc_html($secondary_fonts['font']) . ';
        --background-color: ' . wellness_coach_lite_sanitize_hex_color($background_color) . ';
        --background-color-rgb: ' . sprintf('%1$s, %2$s, %3$s', $rgb4[0], $rgb4[1], $rgb4[2]) . ';
    }';

    return $custom_css;
}
endif;