#!/bin/bash
# set -o nounset
# set -e

declare -A sessions=(
	["misc"]="~/"
    ["qivOld"]="~/dev/projects/P11/workspace/qivserver"
    ["qiv"]="~/dev/projects/qiv/code/java/server"
	# ["buchi"]="~/dev/projects/P17.02/Code/"
	# ["bosch"]="~/dev/projects/bosch/Code/port/fredwick/source"
)

vimSessionsDir="~/.config/nvim/obsessions"

function setup() {
	local session attach
	session=$1
	attach=$2

	if tmux has-session -t $1>/dev/null 2>/dev/null; then
		if [[ $attach == true ]]; then
			attachSession $session
		fi
	else
		makeSession $session
		if [[ $attach == true ]]; then
			attachSession $session
		fi
	fi
}

function makeSession() {
	local session dir oldTMUX
	session=$1
	dir="${sessions[$session]}"

	if [[ $insideSession == true ]]; then
		oldTMUX="$TMUX"
		TMUX=""
	fi

	case "$session" in
		misc)
			tmux new-session -s $session -n bash -d >/dev/null 2>/dev/null
			tmux send-keys -t $session:0 "cd $dir" C-m
			tmux send-keys -t $session:0 'clear' C-m
			tmux selectp -t $session:0
			tmux splitw -h -p 50
			tmux selectp -l

			tmux new-window -t $session:1 -n bash
			tmux send-keys -t $session:1 "cd $dir" C-m
			tmux send-keys -t $session:1 'clear' C-m
			;;

		qiv)
			tmux new-session -s $session -n git -d >/dev/null 2>/dev/null
			tmux send-keys -t $session:0 "cd $dir" C-m
			tmux send-keys -t $session:0 'clear' C-m
			tmux send-keys -t $session:0 'git st' C-m
			# tmux send-keys -t $session:0 "cd $dir" C-m
			# tmux send-keys -t $session:0 'clear' C-m
			# tmux send-keys -t $session:0 "nvim" C-m

			tmux new-window -t $session:1 -n bash
			;;

		qivOld)
			tmux new-session -s $session -n git -d >/dev/null 2>/dev/null
			tmux send-keys -t $session:0 "cd $dir" C-m
			tmux send-keys -t $session:0 'clear' C-m
			tmux send-keys -t $session:0 'git st' C-m
			# tmux send-keys -t $session:0 "cd $dir" C-m
			# tmux send-keys -t $session:0 'clear' C-m
			# tmux send-keys -t $session:0 "nvim" C-m

			tmux new-window -t $session:1 -n bash
			;;

		# bosch)
		# 	tmux new-session -s $session -n code -d >/dev/null 2>/dev/null
		# 	tmux send-keys -t $session:0 "cd $dir" C-m
		# 	tmux send-keys -t $session:0 'clear' C-m
		# 	tmux send-keys -t $session:0 "nvim" C-m

		# 	tmux new-window -t $session:1 -n bash
		# 	tmux send-keys -t $session:1 "cd $dir" C-m
		# 	tmux send-keys -t $session:1 'clear' C-m
		# 	tmux send-keys -t $session:1 'git st' C-m
		# 	;;
	esac
	tmux select-window -t $session:0

	if [[ $insideSession == true ]]; then
		TMUX="$oldTMUX"
	fi
}

function attachSession() {
	if [[ $insideSession == true ]]; then
		tmux switch-client -t $1
	else
		tmux attach -t $1
	fi
}


if [ $# -gt 0 ]; then
	arg=$1

	if [[ ! -z ${TMUX:-} ]]; then
		insideSession=true
	fi

	case "$arg" in
		all)
			for session in "${!sessions[@]}"; do
				setup $session false
			done
			attachSession misc
			;;

		kill)
			if [ $# -eq 2 ]; then
				tmux kill-session -t $2
			else
				tmux kill-server && echo "killed all sessions"
			fi
			;;

		params)
			parameters="${!sessions[@]} all kill"
			echo "$parameters"
			;;

		*)
			if [ ! -z "${sessions[$arg]:-}" ]; then
				setup $arg true
			else
				echo "no such session"
				exit 1
			fi
			;;
	esac

else
	setup misc true
fi
