#!/bin/bash

COUNT_FILE=~/Ressources/Divscripts/Timers/count.txt
secs=$(cat $COUNT_FILE)
freq=1
source /home/user/Ressources/Divscripts/Video_watch/mplayer-with-status.sh 

# Sleep for specified frequency
sleep $freq

# List of applications to check
apps=("netflix" "mpv" "mgba" " - youtube" "slot: " "SevTech")

for app in "${apps[@]}"; do
    if xdotool search --onlyvisible --limit 1 --name "$app" getwindowpid &>/dev/null; then
        # If the application is found, take appropriate action and exit the loop
        if [ "$secs" -le 0 ]; then
            case "$app" in
            "netflix")
                displayapp=Netflix
                xdotool search --limit 1 --name "$app" windowactivate
                zenity --warning --width=300 --title="Limite de temps atteinte" --text="Vous ne pouvez plus regarder netflix, fermeture forcée" --timeout=10
                xdotool search --limit 1 --name "$app" windowkill
                ;;
            "mpv")
                displayapp=Mpv
                xdotool search --limit 1 --class mpv windowactivate && sleep 1 && xdotool key space
                zenity --warning --width=300 --title="Limite de temps atteinte" --text="Vous ne pouvez plus utiliser mpv média player, fermeture forcée" --timeout=10
                xdotool search --limit 1 --class mpv windowactivate && sleep 1 && xdotool key Shift+q
                ;;
            "mgba")
                displayapp=mGBA
                zenity --warning --width=300 --title="Limite de temps atteinte" --text="Vous ne pouvez plus utiliser mGBA, cessez immédiatement !"
                sleep 15
                ;;
            " - youtube")
                displayapp=Youtube
                xdotool search --limit 1 --name "$app" windowactivate
                zenity --warning --width=300 --title="Limite de temps atteinte" --text="Vous ne pouvez plus regarder youtube, fermeture forcée" --timeout=100
                xdotool search --limit 1 --name "$app" windowkill
                ;;
            "slot: ")
                displayapp=Pcsx2
                zenity --warning --width=300 --title="Limite de temps atteinte" --text="Vous ne pouvez plus utiliser l'émulateur, cessez immédiatement !"
                ;;
            "SevTech")
                displayapp=Minecraft
                date
                ;;
            esac
        # Increment count and exit the loop
        exec bash ~/Ressources/Divscripts/Timers/increasecount.sh
		# If there is time left, update the count and log the action
        else
            case "$app" in
                "netflix")
                    displayapp=Netflix;;
                "mpv")
                    displayapp=Mpv;;
                "mgba")
                    displayapp=mGBA;;
                " - youtube")
                    displayapp=Youtube;;
                "slot: ")
                    displayapp=Pcsx2;;
                "SevTech")
                    displayapp=Minecraft;;
            esac
            echo $(($secs-$freq)) > $COUNT_FILE
            if (($secs % 60 == 0))
                then
                echo "Count: $secs removed 60 for $displayapp at $(date)" >> ~/Ressources/Divscripts/Timers/timers.log
            fi
        fi
    fi
done

if pgrep mplayer >/dev/null; then
    if ! isPaused; then
        echo "MPlayer is running and not paused."
        echo $(($secs-$freq)) > $COUNT_FILE
        if (($secs % 60 == 0))
            then
            echo "Count: $secs removed 60 for MPlayer at $(date)" >> ~/Ressources/Divscripts/Timers/timers.log
        fi
    fi
fi

exec bash $0