#!/usr/bin/env bash

# Initialize the variable that holds the current section
current_section=""

# Read the kdeglobals file
while IFS='=' read -r key value; do
    # Verify if the line is a section definition
    if [[ $key =~ ^\[.*\]$ ]]; then
        # Remove the brackets and replace non-alphanumeric characters with '_'
        current_section="${key//[\[\]]/}"
        current_section="${current_section,,}" # Convert to lowercase
        current_section="${current_section//[^a-z0-9]/_}" # Replace non-alphanumeric characters with '_'
    else
        # Process only lines that correspond to color settings
        if [[ $key =~ ^Foreground.*$ || $key =~ ^Background.*$ || $key =~ ^Decoration.*$ || $key =~ ^Button.*$ || $key =~ ^Window.*$ || $key =~ ^Highlight.*$ || $key =~ ^activeBackground.*$ || $key =~ ^activeBlend.*$ || $key =~ ^activeForeground.*$ || $key =~ ^inactiveBackground.*$ || $key =~ ^inactiveBlend.*$ || $key =~ ^inactiveForeground.*$  || $key =~ ^Header.*$ ]]; then
            # Normalize the key name to be used as a variable name
            normalized_key="${key,,}" # Convert to lowercase
            normalized_key="${normalized_key//[^a-z0-9_]/_}" # Replace non-alphanumeric characters with '_'
            
            # Create the variable name by combining the current section and the key
            var_name="${current_section}_${normalized_key}"
            
            # Format the value as RGB
            rgb_value="rgb(${value})"
            
            # Define the variable dynamically
            declare "$var_name=$rgb_value"
            # echo "$var_name=$rgb_value"
        fi
    fi
done < ~/.config/kdeglobals

echo "
:root,
body {
  --primary: $colors_window_backgroundalternate;
  --on-primary: $colors_window_decorationfocus;
  --primary-container: $colors_window_decorationhover;
  --on-primary-container: $colors_window_foregroundnormal;
  --scrollbar: ${colors_window_foregroundnormal/)/,0.14)};
  --scrollbar-hover: ${colors_window_decorationhover/)/,0.5)};
  --secondary:#f7f7f7;
  --on-secondary:#3c3a3a;
  --secondary-container:rgb(189 189 189 / 70%);
  --tertiary:#3a6df0;
  --on-tertiary:#fff;
  --tertiary-container:#ebddff;
  --on-tertiary-container:#201634;
  --error:#ba1a1a;
  --on-error:#ffffff;
  --error-container:#ffdad6;
  --on-error-container:#410002;
  --background: $colors_view_backgroundnormal;
  --on-background:#3c3a3a;
  --surface: $wm_activebackground;
  --on-surface: $wm_activeforeground;
  --surface-variant: ${colors_window_foregroundnormal/)/,0.1)};
  --on-surface-variant: $colors_window_foregroundnormal;
  --outline:#72787e;
  --outline-variant:#c1c7ce;
  --shadow:#000000;
  --scrim:#000000;
  --inverse-surface:#2e3133;
  --inverse-on-surface:#f0f0f3;
  --inverse-primary:#8dcdff;
  --surface-dim:#d9dadd;
  --surface-bright:#f9f9fc;
  --surface-container-lowest:#ffffff;
  --surface-container-low: rgba(0,0,0,0.03);
  --surface-container:rgba(0,0,0,0.06);
  --surface-container-high:rgba(0,0,0,0.09);
  --surface-container-highest:rgba(0,0,0,0.12);
  --overlay: rgb(0 0 0 /0.5);
  --active: rgb(0 0 0 / 0.1);
  --elevate1: 0 0.1rem 0.2rem 0 rgb(0 0 0 / 16%);
  --elevate2: 0 0.25rem 0.5rem 0 rgb(0 0 0 / 0.30);
  --elevate3: 0 0.375rem 0.75rem 0 rgb(0 0 0 / 0.45);
  --button-elevate1: 0 0.090rem 0.090rem 0 rgb(0 0 0 / 40%);
  --onhover-elevate1: 0 0.25rem 0.5rem 0 rgb(0 0 0 / 18%);
  --body: transparent;
}
"
# Show the defined variables for verification
# declare -p | grep '^declare --' | grep -E 'color_|button_|window_|tooltip_|foreground_|background_|decoration_|highlight_|wm_'

# Example:
# colors_button_backgroundalternate=rgb(163,212,250)
# colors_button_backgroundnormal=rgb(252,252,252)
# colors_button_decorationfocus=rgb(255,170,0)
# colors_button_decorationhover=rgb(170,85,0)
# colors_button_foregroundactive=rgb(61,174,233)
# colors_button_foregroundinactive=rgb(112,125,138)
# colors_button_foregroundlink=rgb(41,128,185)
# colors_button_foregroundnegative=rgb(218,68,83)
# colors_button_foregroundneutral=rgb(246,116,0)
# colors_button_foregroundnormal=rgb(35,38,41)
# colors_button_foregroundpositive=rgb(39,174,96)
# colors_button_foregroundvisited=rgb(155,89,182)
# colors_complementary_backgroundalternate=rgb(27,30,32)
# colors_complementary_backgroundnormal=rgb(42,46,50)
# colors_complementary_decorationfocus=rgb(61,174,233)
# colors_complementary_decorationhover=rgb(61,174,233)
# colors_complementary_foregroundactive=rgb(61,174,233)
# colors_complementary_foregroundinactive=rgb(161,169,177)
# colors_complementary_foregroundlink=rgb(29,153,243)
# colors_complementary_foregroundnegative=rgb(218,68,83)
# colors_complementary_foregroundneutral=rgb(246,116,0)
# colors_complementary_foregroundnormal=rgb(252,252,252)
# colors_complementary_foregroundpositive=rgb(39,174,96)
# colors_complementary_foregroundvisited=rgb(155,89,182)
# colors_header_backgroundalternate=rgb(0,85,0)
# colors_header_backgroundnormal=rgb(130,143,71)
# colors_header_decorationfocus=rgb(61,174,233)
# colors_header_decorationhover=rgb(61,174,233)
# colors_header_foregroundactive=rgb(61,174,233)
# colors_header_foregroundinactive=rgb(112,125,138)
# colors_header_foregroundlink=rgb(41,128,185)
# colors_header_foregroundnegative=rgb(218,68,83)
# colors_header_foregroundneutral=rgb(246,116,0)
# colors_header_foregroundnormal=rgb(35,38,41)
# colors_header_foregroundpositive=rgb(39,174,96)
# colors_header_foregroundvisited=rgb(155,89,182)
# colors_headerinactive_backgroundalternate=rgb(227,229,231)
# colors_headerinactive_backgroundnormal=rgb(239,240,241)
# colors_headerinactive_decorationfocus=rgb(61,174,233)
# colors_headerinactive_decorationhover=rgb(61,174,233)
# colors_headerinactive_foregroundactive=rgb(61,174,233)
# colors_headerinactive_foregroundinactive=rgb(112,125,138)
# colors_headerinactive_foregroundlink=rgb(41,128,185)
# colors_headerinactive_foregroundnegative=rgb(218,68,83)
# colors_headerinactive_foregroundneutral=rgb(246,116,0)
# colors_headerinactive_foregroundnormal=rgb(35,38,41)
# colors_headerinactive_foregroundpositive=rgb(39,174,96)
# colors_headerinactive_foregroundvisited=rgb(155,89,182)
# colors_selection_backgroundalternate=rgb(163,212,250)
# colors_selection_backgroundnormal=rgb(85,255,0)
# colors_selection_decorationfocus=rgb(255,170,0)
# colors_selection_decorationhover=rgb(170,85,0)
# colors_selection_foregroundactive=rgb(255,255,255)
# colors_selection_foregroundinactive=rgb(112,125,138)
# colors_selection_foregroundlink=rgb(253,188,75)
# colors_selection_foregroundnegative=rgb(176,55,69)
# colors_selection_foregroundneutral=rgb(198,92,0)
# colors_selection_foregroundnormal=rgb(255,255,255)
# colors_selection_foregroundpositive=rgb(23,104,57)
# colors_selection_foregroundvisited=rgb(155,89,182)
# colors_tooltip_backgroundalternate=rgb(239,240,241)
# colors_tooltip_backgroundnormal=rgb(247,247,247)
# colors_tooltip_decorationfocus=rgb(255,170,0)
# colors_tooltip_decorationhover=rgb(170,85,0)
# colors_tooltip_foregroundactive=rgb(61,174,233)
# colors_tooltip_foregroundinactive=rgb(112,125,138)
# colors_tooltip_foregroundlink=rgb(41,128,185)
# colors_tooltip_foregroundnegative=rgb(218,68,83)
# colors_tooltip_foregroundneutral=rgb(246,116,0)
# colors_tooltip_foregroundnormal=rgb(35,38,41)
# colors_tooltip_foregroundpositive=rgb(39,174,96)
# colors_tooltip_foregroundvisited=rgb(155,89,182)
# colors_view_backgroundalternate=rgb(247,247,247)
# colors_view_backgroundnormal=rgb(255,255,255)
# colors_view_decorationfocus=rgb(255,170,0)
# colors_view_decorationhover=rgb(170,85,0)
# colors_view_foregroundactive=rgb(61,174,233)
# colors_view_foregroundinactive=rgb(112,125,138)
# colors_view_foregroundlink=rgb(41,128,185)
# colors_view_foregroundnegative=rgb(218,68,83)
# colors_view_foregroundneutral=rgb(246,116,0)
# colors_view_foregroundnormal=rgb(0,0,0)
# colors_view_foregroundpositive=rgb(39,174,96)
# colors_view_foregroundvisited=rgb(155,89,182)
# colors_window_backgroundalternate=rgb(227,229,231)
# colors_window_backgroundnormal=rgb(243,243,243)
# colors_window_decorationfocus=rgb(255,170,0)
# colors_window_decorationhover=rgb(170,85,0)
# colors_window_foregroundactive=rgb(61,174,233)
# colors_window_foregroundinactive=rgb(112,125,138)
# colors_window_foregroundlink=rgb(41,128,185)
# colors_window_foregroundnegative=rgb(218,68,83)
# colors_window_foregroundneutral=rgb(246,116,0)
# colors_window_foregroundnormal=rgb(170,0,0)
# colors_window_foregroundpositive=rgb(39,174,96)
# colors_window_foregroundvisited=rgb(155,89,182)
# kfiledialog_settings_decoration_position=rgb(0)
# wm_activebackground=rgb(255,85,127)
# wm_activeblend=rgb(170,85,255)
# wm_activeforeground=rgb(35,38,41)
# wm_inactivebackground=rgb(255,0,0)
# wm_inactiveblend=rgb(170,170,255)
# wm_inactiveforeground=rgb(85,255,255)
