#compdef pdd

# number of words between latest `-d` and cursor
integer d_nargs=${${(Oa)words[1,CURRENT]}[(i)-d]}
integer t_nargs=${${(Oa)words[1,CURRENT]}[(i)-t]}
integer day_nargs=${${(Oa)words[1,CURRENT]}[(i)--day]}
# the word in cursor is ''
if [[ -z ${words[CURRENT]} ]]; then
  d_nargs+=1
  t_nargs+=1
  day_nargs+=1
fi
local d_metavars=(yyyy mmm dd yyyy/y mmm/m dd/d)
local t_metavars=(hh\\:mm\\:ss hh\\:mm\\:ss/h\\:m\\:s)
local day_metavars=(yyyy mmm dd)

if (($d_nargs <= $#d_metavars + 1 && $d_nargs <= $#words)); then
  _arguments "*:${d_metavars[d_nargs - 1]}"
elif (($t_nargs <= $#t_metavars + 1 && $t_nargs <= $#words)); then
  _arguments "*:${t_metavars[t_nargs - 1]}"
elif (($day_nargs <= $#day_metavars + 1 && $day_nargs <= $#words)); then
  _arguments "*:${day_metavars[day_nargs - 1]}"
else
  _arguments -s -S \
    '(- *)'{-h,--help}'[show this help message and exit]' \
  {-d,--date}'[calculate date difference]:yyyy mmm dd [yyyy mmm dd | y m d]' \
  {-t,--time}'[calculate time difference]:hh\:mm\:ss [hh\:mm\:ss | h\:m\:s]' \
  --add'[add to date (/today) or time (/now)]' \
  --sub'[subtract from date (/today) or time (/now)]' \
  --day'[show day of the week on a date]:yyyy mmm dd' \
  {-c,--timer}'[start a countdown timer]:hh\:mm\:ss' \
  {-r,--run}'[run command when countdown timer reaches 0]: :_command_names -e' \
  {-s,--stopwatch}'[start a stopwatch [default resolution: 3 (ms)\]]:resolution:(1 2 3 4 5 6 7 8 9)' \
  {-q,--quiet}'[quiet mode for background timer/stopwatch]' \
  '*:keywords'
fi
