#!/bin/bash

calendar() {
	LOCK_FILE="$HOME/.cache/eww-calendar.lock"
	EWW_BIN="$HOME/.local/bin/eww"

	run() {
		${EWW_BIN} -c $HOME/.config/eww open calendar
	}

	# Open widgets
	if [[ ! -f "$LOCK_FILE" ]]; then
		${EWW_BIN} -c $HOME/.config/eww close system music_win audio_ctl brightness
		touch "$LOCK_FILE"
		run && echo "ok good!"
	else
		${EWW_BIN} -c $HOME/.config/eww close calendar
		rm "$LOCK_FILE" && echo "closed"
	fi
}

system() {
	LOCK_FILE_MEM="$HOME/.cache/eww-system.lock"
	EWW_BIN="$HOME/.local/bin/eww"

	run() {
		${EWW_BIN} -c $HOME/.config/eww open system
	}

	# Open widgets
	if [[ ! -f "$LOCK_FILE_MEM" ]]; then
		${EWW_BIN} -c $HOME/.config/eww close calendar music_win audio_ctl brightness
		touch "$LOCK_FILE_MEM"
		run && echo "ok good!"
	else
		${EWW_BIN} -c $HOME/.config/eww close system
		rm "$LOCK_FILE_MEM" && echo "closed"
	fi
}

music() {
	LOCK_FILE_SONG="$HOME/.cache/eww-song.lock"
	EWW_BIN="$HOME/.local/bin/eww"

	run() {
		${EWW_BIN} -c $HOME/.config/eww open music_win
	}

	# Open widgets
	if [[ ! -f "$LOCK_FILE_SONG" ]]; then
		${EWW_BIN} -c $HOME/.config/eww close system calendar brightness
		touch "$LOCK_FILE_SONG"
		run && echo "ok good!"
	else
		${EWW_BIN} -c $HOME/.config/eww close music_win
		rm "$LOCK_FILE_SONG" && echo "closed"
	fi
}

audio() {
	LOCK_FILE_AUDIO="$HOME/.cache/eww-audio.lock"
	EWW_BIN="$HOME/.local/bin/eww"

	run() {
		${EWW_BIN} -c $HOME/.config/eww open audio_ctl
	}

	# Open widgets
	if [[ ! -f "$LOCK_FILE_AUDIO" ]]; then
		${EWW_BIN} -c $HOME/.config/eww close system calendar music brightness
		touch "$LOCK_FILE_AUDIO"
		run && echo "ok good!"
	else
		${EWW_BIN} -c $HOME/.config/eww close audio_ctl
		rm "$LOCK_FILE_AUDIO" && echo "closed"
	fi
}

brightness() {
	LOCK_FILE_AUDIO="$HOME/.cache/eww-brightness.lock"
	EWW_BIN="$HOME/.local/bin/eww"

	run() {
		${EWW_BIN} -c $HOME/.config/eww open brightness_window
	}

	# Open widgets
	if [[ ! -f "$LOCK_FILE_AUDIO" ]]; then
		${EWW_BIN} -c $HOME/.config/eww close system calendar music audio_ctl
		touch "$LOCK_FILE_AUDIO"
		run && echo "ok good!"
	else
		${EWW_BIN} -c $HOME/.config/eww close brightness_window
		rm "$LOCK_FILE_AUDIO" && echo "closed"
	fi
}

hoverGet() {
	NAME="$1"
	EWW_BIN="$HOME/.local/bin/eww"
	if [[ -f "$STORE" ]]; then
		rm "$STORE"
		${EWW_BIN} -c $HOME/.config/eww close "$NAME"
	fi
}

hoverCreate() {
	if [[ ! -f "$STORE" ]]; then
		touch "$STORE"
	else
		echo "failure!"
	fi
}
STORE=true
if [ "$1" = "calendar" ]; then
	calendar
elif [ "$1" = "system" ]; then
	system
#elif [ "$1" = "music" ]; then
#music
elif [ "$1" = "audio" ]; then
	audio
elif [ "$1" = "brightness" ]; then
	brightness
elif [ "$1" = "hoverGet" ]; then
	hoverGet "$2"
elif [ "$1" = "hoverCreate" ]; then
	hoverCreate
fi
