#!/usr/bin/env bash
declare -A CONFIG_PATH
test ! $# -gt 0
THERE_IS_NO_ARGUMENT="$?"
XDG_CONFIG_HOME="$HOME/.config"
cd "$(dirname "$0")"

CONFIG_PATH['i3']="$XDG_CONFIG_HOME/i3/config"
CONFIG_PATH['dunst']="$XDG_CONFIG_HOME/dunst/dunstrc"
CONFIG_PATH['polybar']="$XDG_CONFIG_HOME/polybar/config"
CONFIG_PATH['xresources']="$XDG_CONFIG_HOME/../.Xresources"

function printHelp() {
    cat << HELP
$(basename "$0"), a non-interactive program that applies themes to the i3 window manager

Usage:
    ./$(basename "$0") -a [THEME_ID] [OPTIONS]

Example:
    ./$(basename "$0") -a 000

Options:
  -h, --help                Display this help message
  -a, --apply THEME_ID      Apply the theme with given id
  -r, --restore             Restore to distribution default configs
  -o, --output              Custom directory to save the files

Available themes:
$(listThemes)

Check /assets/screenshots to see how the themes look like and
send bugs and questions to <https://github.com/cizordj/i3-themer>
HELP
exit 0
}
function listThemes(){
    themesList=$(ls themes/ | grep -v ".md")
    i=0

    for line in $themesList; do
        if [ $i -eq 3 ] ; then
            output="$line\n"
            i=-1
        else
            output="$line\t"
        fi
        ((i++))
        printf '%b' "$output"
    done
    printf '%b' "\n"
}
function restoreDefaults(){
   rm "${CONFIG_PATH[@]}" "$XDG_CONFIG_HOME/polybar/i3wmthemer_bar_launch.sh"
   rm "$XDG_CONFIG_HOME/xwallpaper/"*
   rmdir "$XDG_CONFIG_HOME/i3"
   rmdir "$XDG_CONFIG_HOME/polybar"
   rmdir "$XDG_CONFIG_HOME/xwallpaper"
   rmdir "$XDG_CONFIG_HOME/dunst"
   cat << EOF
i3-themer successfully removed! You may want to remove the dependencies manually.
Check the README for more details.

Type 'i3-msg restart' to go back to defaults.

Have a great day!
EOF
    exit
}
function missingTheme(){
    cat << EOF
The theme you chose does not exist in the themes' directory

$(listThemes)
EOF
exit 1
}
function touchConfigFiles(){
    mkdir -p "$XDG_CONFIG_HOME"
    mkdir -p "$XDG_CONFIG_HOME/i3"
    mkdir -p "$XDG_CONFIG_HOME/polybar"
    mkdir -p "$XDG_CONFIG_HOME/dunst"
    mkdir -p "$XDG_CONFIG_HOME/xwallpaper"
    touch "${CONFIG_PATH[@]}"
}

function copyWallpapers(){
    cp assets/wallpapers/* "$XDG_CONFIG_HOME/xwallpaper/"
}
function copyPolybarLauncher(){
    cp assets/scripts/i3wmthemer_bar_launch.sh "$XDG_CONFIG_HOME/polybar/"
}
function restartApps(){
    pkill dunst
    xrdb "${CONFIG_PATH['xresources']}"
    i3 restart
    notify-send "Theme applied successfully!"
}


while true
do
    if [ $THERE_IS_NO_ARGUMENT -eq 0 ]; then
        printHelp
    elif [ "$1" = "-h" -o "$1" = "--help" -o "$1" = "help" ] ; then
        printHelp
    elif [ "$1" = "-a" -o "$1" = "--apply" -o "$1" = "apply" ] ; then
        shift 1
        CHOSEN_THEME="$PWD/themes/$1"
    elif [ "$1" = "-o" -o "$1" = "--output" -o "$1" = "output" ] ; then
        shift 1
        cd "$1" || exit 1
        XDG_CONFIG_HOME=`pwd`
        unset -f restartApps
        cd "$OLDPWD"
    elif [ "$1" = "-r" -o "$1" = "--restore" -o "$1" = "restore" ] ; then
        restoreDefaults
    elif [ "$1" = "-l" -o "$1" = "--list" -o "$1" = "list" ] ; then
        listThemes
        exit
    else
        break
    fi
    shift 1
done

if [[ -f "$CHOSEN_THEME" ]] ; then
    source "$CHOSEN_THEME"
else
    missingTheme
fi

touchConfigFiles
copyWallpapers
copyPolybarLauncher

for file in defaults/* ; do
    source "$file"
done

dunstConfig > "${CONFIG_PATH['dunst']}"
i3config > "${CONFIG_PATH['i3']}"
polybarConfig > "${CONFIG_PATH['polybar']}"
xresourcesConfig > "${CONFIG_PATH['xresources']}"

for file in defaults.d/* ; do
    source "$file"
done
i3config >> "${CONFIG_PATH['i3']}"
polybarConfig >> "${CONFIG_PATH['polybar']}"
xresourcesConfig >> "${CONFIG_PATH['xresources']}"
restartApps 2&> /dev/null
