#!/bin/bash
scss_theme="$HOME/.config/eww/carbonmonoxide/colors.scss"
# dark="oxocarbon"
# light="oxocarbon_light"

switch_theme () {
    echo '@import '"\"./themes/$1.scss\"" > $scss_theme

    if [[ -f "$HOME/.config/foot/themes/$1.ini" ]]; then
        sed -i "s/include=.*/include=~\/.config\/foot\/themes\/$1.ini/" $HOME/.config/foot/foot.ini
    fi

    if [[ -f "$HOME/.config/wofi/themes/$1.css" ]]; then 
        sed -i "s/@import.*/@import \".config\/wofi\/themes\/$1.css\";/" $HOME/.config/wofi/style.css
    fi

    inactivebg=$(sed -n "s/\$surface0: \(.*\);/\1/p" $HOME/.config/eww/carbonmonoxide/themes/$1.scss)
    bg=$(sed -n "s/\$surface2: \(.*\);/\1/p" $HOME/.config/eww/carbonmonoxide/themes/$1.scss)
    text=$(sed -n "s/\$white0: \(.*\);/\1/p" $HOME/.config/eww/carbonmonoxide/themes/$1.scss)
    urgent="#ee5396"

    if [ -n $surface0 ] && [ -n $surface2 ] && [ -n $white0 ]; then
        swaymsg "client.focused          $bg           $bg          $text          $bg"
        swaymsg "client.unfocused        $inactivebg $inactivebg $text  $inactivebg"
        swaymsg "client.focused_inactive $inactivebg $inactivebg $text  $inactivebg"
        swaymsg "client.urgent           $urgent    $urgent   $text          $urgent"
    fi

    sleep 1
    notify-send -a Theme "Theme selected" "$1"
}

current=`cat $scss_theme | sed 's/@import \".\/themes\/\(.*\).scss\"/\1/'`

if [[ $1 == "toggle" ]]; then
    case $current in 
    "oxocarbon")
        switch_theme "oxocarbon_light"
        swaymsg output \* bg ~/.config/sway/wallpaper/tile.png tile
        ;;
    "oxocarbon_light") 
        switch_theme "catppuccin"
        swaymsg output \* bg ~/.config/sway/wallpaper/catppuccinlan.png fill
        ;;
    "catppuccin") 
        switch_theme "rosepine"
        swaymsg output \* bg ~/.config/sway/wallpaper/rosepine.jpg fill
        ;;
    "rosepine") 
        switch_theme "swamp"
        swaymsg output \* bg ~/.config/sway/wallpaper/swamp.png fill
        ;;
    "swamp") 
        switch_theme "biscuit"
        swaymsg output \* bg ~/.config/sway/wallpaper/biscuitcat.png fill
        ;;
    "biscuit") 
        switch_theme "camellia"
        swaymsg output \* bg ~/.config/sway/wallpaper/camelliawoman.jpg fill
        ;;
    *)
        switch_theme "oxocarbon"
        swaymsg output \* bg ~/.config/sway/wallpaper/tile.png tile
        ;;
    esac
elif [[ $1 == "select" ]]; then
    case $2 in 
    "oxocarbon_light")
        switch_theme "oxocarbon_light"
        swaymsg output \* bg ~/.config/sway/wallpaper/tile.png tile
        ;;
    "catppuccin") 
        switch_theme "catppuccin"
        swaymsg output \* bg ~/.config/sway/wallpaper/catppuccinlan.png fill
        ;;
    "catppuccin_latte") 
        switch_theme "catppuccin_latte"
        swaymsg output \* bg ~/.config/sway/wallpaper/forrest.png fill
        ;;
    "rosepine") 
        switch_theme "rosepine"
        swaymsg output \* bg ~/.config/sway/wallpaper/rosepine.jpg fill
        ;;
    "swamp") 
        switch_theme "swamp"
        swaymsg output \* bg ~/.config/sway/wallpaper/swamp.png fill
        ;;
    "biscuit") 
        switch_theme "biscuit"
        swaymsg output \* bg ~/.config/sway/wallpaper/biscuitcat.png fill
        ;;
    "camellia") 
        switch_theme "camellia"
        swaymsg output \* bg ~/.config/sway/wallpaper/camelliawoman.jpg fill
        ;;
    *)
        switch_theme "oxocarbon"
        swaymsg output \* bg ~/.config/sway/wallpaper/tile.png tile
        ;;
    esac
elif [[ $1 == "setup" ]]; then
    case $current in 
    "oxocarbon_light")
        switch_theme "oxocarbon_light"
        swaymsg output \* bg ~/.config/sway/wallpaper/tile.png tile
        ;;
    "catppuccin") 
        switch_theme "catppuccin"
        swaymsg output \* bg ~/.config/sway/wallpaper/catppuccinlan.png fill
        ;;
    "catppuccin_latte") 
        switch_theme "catppuccin_latte"
        swaymsg output \* bg ~/.config/sway/wallpaper/forrest.png fill
        ;;
    "rosepine") 
        switch_theme "rosepine"
        swaymsg output \* bg ~/.config/sway/wallpaper/rosepine.jpg fill
        ;;
    "swamp") 
        switch_theme "swamp"
        swaymsg output \* bg ~/.config/sway/wallpaper/swamp.png fill
        ;;
    "biscuit") 
        switch_theme "biscuit"
        swaymsg output \* bg ~/.config/sway/wallpaper/biscuitcat.png fill
        ;;
    "camellia") 
        switch_theme "camellia"
        swaymsg output \* bg ~/.config/sway/wallpaper/camelliawoman.jpg fill
        ;;
    *)
        switch_theme "oxocarbon"
        swaymsg output \* bg ~/.config/sway/wallpaper/tile.png tile
        ;;
    esac
else
    echo $current
fi
