#!/bin/sh
WORK_TIME=1500
REST_TIME=300
SOUND="$HOME/bin/time.ogg"
SAY_MESSAGE_STOP="Please stop working."
SAY_MESSAGE_CONTINUE="You can continue working now" 
POPUP_MESSAGE_REST="Time to rest."

function _pomodoro_clock {
    local time="$1"
    sleep "$time"
}

function _pomodoro_sound {
    local sound="$1"
    mpv "$sound"
}

function _pomodoro_say {
    local message="$1"
    espeak "$message" 2>/dev/null
}

function _pomodoro_popup {
    local message="$1"
    local time_out="$2"
    xmessage \
    	-buttons Ok:0,"Not sure":1,Cancel:2 \
	-default Ok \
	-nearmouse "$message" \
	-timeout "$time_out" 
}

function _pomodoro_work {
    # work for during WORK_TIME
    _pomodoro_clock "$WORK_TIME"
    
    # when clock ends it is time to stop
    _pomodoro_sound "$SOUND"
    _pomodoro_say "$SAY_MESSAGE_STOP"
    _pomodoro_popup "$POPUP_MESSAGE_REST" "$REST_TIME"
    
    # after popup ends you can go back to work
    _pomodoro_say "$SAY_MESSAGE_CONTINUE"
}

function _pomodoro_rest {
    # rest for a while
    _pomodoro_sound "$SOUND"
    _pomodoro_popup "$POPUP_MESSAGE_REST" "$REST_TIME"
    
    #time to go back to work after popup ends
    _pomodoro_say "$SAY_MESSAGE_CONTINUE"
}

function main {
    if [ -z "$1" ]; then
	_pomodoro_work
    else
	_handle_arguments "$@"
    fi
    return
}

#	    -p | --playlist-id )    argument_playlist_id "$2" ; shift ; shift  ;;
function _handle_arguments {
    while [ "$1" != "" ]; do
	case "$1" in
	    -r | --rest )        _pomodoro_rest ; shift ;;
	    -w | --work )        _pomodoro_work ; shift ;;
	    --)  shift; break ;;
	    * ) print_invalid_option $1 ; exit 1 ;;
	esac
    done
    return
}

main "$@"