<?php

function travel_tour_dynamic_css() {
    wp_enqueue_style(
        'travel-tour-style',
        get_stylesheet_uri(),
        array(),
        TRAVEL_TOUR_VERSION
    );
    function HexToBootstrapRgb(  $hex, $alpha = false  ) {
        $hex = str_replace( '#', '', $hex );
        $length = strlen( $hex );
        $rgb['r'] = hexdec( ( $length == 6 ? substr( $hex, 0, 2 ) : (( $length == 3 ? str_repeat( substr( $hex, 0, 1 ), 2 ) : 0 )) ) );
        $rgb['g'] = hexdec( ( $length == 6 ? substr( $hex, 2, 2 ) : (( $length == 3 ? str_repeat( substr( $hex, 1, 1 ), 2 ) : 0 )) ) );
        $rgb['b'] = hexdec( ( $length == 6 ? substr( $hex, 4, 2 ) : (( $length == 3 ? str_repeat( substr( $hex, 2, 1 ), 2 ) : 0 )) ) );
        if ( $alpha ) {
            $rgb['a'] = $alpha;
        }
        return strval( $rgb['r'] ) . ', ' . strval( $rgb['g'] ) . ', ' . strval( $rgb['b'] );
    }

    $primary_color = get_theme_mod( 'primary_colors', '#1bb0ce' );
    $primary_color_rgb = HexToBootstrapRgb( $primary_color );
    $secondary_color = get_theme_mod( 'secondary_colors', '#f59410' );
    $secondary_color_rgb = HexToBootstrapRgb( $secondary_color );
    $dark_color = '#000';
    $dark_color_rgb = '0, 0, 0';
    $light_color = '#fff';
    $light_color_rgb = '255, 255, 255';
    $text_color = '#333';
    $text_color_rgb = '51, 51, 51';
    $font_family = esc_attr( get_theme_mod( 'font_family', 'Montserrat' ) );
    $font_size = esc_attr( get_theme_mod( 'font_size', '14px' ) );
    $font_weight = absint( get_theme_mod( 'travel_tour_font_weight', 400 ) );
    $line_height = absint( get_theme_mod( 'travel_tour_line_height', 22 ) );
    $logo_size = absint( get_theme_mod( 'travel_tour_logo_size', 60 ) );
    $logo_font_size = absint( $logo_size / 1.5 );
    $logo_font_family = get_theme_mod( 'travel_tour_site_identity_font_family', 'Montserrat' );
    $heading_font_family = esc_attr( get_theme_mod( 'heading_font_family', 'Montserrat' ) );
    $heading_font_weight = esc_attr( get_theme_mod( 'heading_font_weight', 400 ) );
    $scroll_to_top_size = absint( get_theme_mod( 'travel_tour_scroll_to_top_size', 36 ) );
    $scroll_to_top_color = get_theme_mod( 'travel_tour_scroll_to_top_color', '#1bb0ce' );
    $img_width_val = get_theme_mod( 'travel_tour_featured_image_width', 'full' );
    $featured_image_width = ( $img_width_val === 'full' ? '100%' : absint( $img_width_val ) . 'px' );
    $img_height_val = get_theme_mod( 'travel_tour_featured_image_max_height', 'full' );
    $featured_image_max_height = ( $img_height_val === 'full' ? 'none' : absint( $img_height_val ) . 'px' );
    $default_size = array(
        '1' => 35,
        '2' => 32,
        '3' => 18,
        '4' => 17,
        '5' => 16,
        '6' => 12,
    );
    for ($i = 1; $i <= 6; $i++) {
        $heading[$i] = absint( get_theme_mod( 'travel_tour_heading_' . $i . '_size', absint( $default_size[$i] ) ) );
    }
    $dynamic_css = "\r\n        :root {\r\n            --primary-color: {$primary_color} !important;\r\n            --primary-color_rgb: {$primary_color_rgb};\r\n            --secondary-color: {$secondary_color};\r\n            --secondary-color_rgb: {$secondary_color_rgb};\r\n            --hover-color: {$secondary_color};\r\n            --dark-color: {$dark_color};\r\n            --dark-color_rgb: {$dark_color_rgb};\r\n            --light-color: {$light_color};\r\n            --light-color_rgb: {$light_color_rgb};\r\n            --text-color: {$text_color};\r\n            --text-color_rgb: {$text_color_rgb};\r\n            --font-size: {$font_size};\r\n            --font-weight: {$font_weight};\r\n            --line-height: {$line_height}px;\r\n            --font-family: {$font_family};\r\n            --heading-font-family: {$heading_font_family};\r\n            --heading-font-weight: {$heading_font_weight};\r\n            --h1-font-size: {$heading[1]}px;\r\n            --h2-font-size: {$heading[2]}px;\r\n            --h3-font-size: {$heading[3]}px;\r\n            --h4-font-size: {$heading[4]}px;\r\n            --h5-font-size: {$heading[5]}px;\r\n            --h6-font-size: {$heading[6]}px;\r\n            --logo-size: {$logo_size}" . "px;\r\n            --logo-font-size: {$logo_font_size}" . "px;\r\n            --logo-font-family: {$logo_font_family};\r\n            --featured-image-width: {$featured_image_width};\r\n            --featured-image-max-height: {$featured_image_max_height};\r\n\r\n            /* WP Travel Engine variables */\r\n            --primary-color-rgb: {$primary_color_rgb} !important;\r\n            --wpte-discount-color: {$secondary_color};\r\n            --wpte-featured-color: {$secondary_color};\r\n            --scroll-to-top-size: {$scroll_to_top_size}px;\r\n        }\r\n\r\n        .scroll-top-wrapper {\r\n            width: {$scroll_to_top_size}px;\r\n            height: {$scroll_to_top_size}px;\r\n            background-color: {$scroll_to_top_color} !important;\r\n        }\r\n    ";
    wp_add_inline_style( 'travel-tour-style', $dynamic_css );
}

add_action( 'wp_enqueue_scripts', 'travel_tour_dynamic_css' );