#!/bin/bash

WORKSOUND_FILE="/Users/jirayu/Projects/JR/sw/dotfiles/.config/sketchybar/sound/ytmp3free.cc_green-screen-happy-happy-happy-cat-meme-youtubemp3free.org.mp3"
BREAKSOUND_FILE="/Users/jirayu/Projects/JR/sw/dotfiles/.config/sketchybar/sound/sad.mp3"
STATE_FILE="/Users/jirayu/.pomodoro_sound_state"

playsound() {
  local current_status="$1"

  local last_status=""
  
  if [ -f "$STATE_FILE" ]; then
    last_status=$(cat "$STATE_FILE")
  fi 

  if [ "$current_status" != "$last_status" ]; then
    if [ "$current_status" == "break_done" ]; then
      afplay "$BREAKSOUND_FILE" &
    else
      afplay "$WORKSOUND_FILE" &
    fi

    rm -f "$STATE_FILE"
    echo "$current_status" > "$STATE_FILE"
  fi
}

STATUS=$(/usr/local/bin/pomodoro status)

if [ -z "$STATUS" ]; then
  sketchybar --set pomodoro icon="" \
                            label="Start a Session!" \
                            icon.color="0xffed8796" \
                            label.color="0xffed8796" 
elif /usr/local/bin/pomodoro status | grep -q "Break: ❗️"; then
  sketchybar --set pomodoro icon="" \
                            label="Start a new Session!" \
                            icon.color="0xffed8796" \
                            label.color="0xffed8796" 
  playsound "break_done"
elif /usr/local/bin/pomodoro status | grep -q "Pomodoro: ❗"; then
  sketchybar --set pomodoro icon="" \
                            label="Good Job! Take a break!" \
                            icon.color="0xffed8796" \
                            label.color="0xffed8796" 
  playsound "pomodoro_done"
elif /usr/local/bin/pomodoro status | grep -q "Pomodoro"; then
  sketchybar --set pomodoro icon="" \
                            label="$(/usr/local/bin/pomodoro status -f "%r")" \
                            icon.color="0xfff5bde6" \
                            label.color="0xfff5bde6" 
elif /usr/local/bin/pomodoro status | grep -q "Break"; then
  sketchybar --set pomodoro icon="󱅻" \
                            label="$(/usr/local/bin/pomodoro status -f "%r")" \
                            icon.color="0xff7dc4e4" \
                            label.color="0xff7dc4e4" 
fi
