#compdef td

_td() {
	local -a commands=(
		"add:add a new task"
		"do:mark tasks as done"
		"ls:list tasks"
		"lsid:list IDs"
		"lsctx:list contexts"
		"lsproj:list projects"
		"modify:modify tasks"
		"restore:undo the last modification in todo.json/done.json using the bak file"
		"rm:delete tasks"
		"undo:undo tasks"
		"help:display help message"
  )
  describe_commands() {
    _describe "commands" commands
  }

  complete_contexts() {
    compadd $(td lsctx -a)
  }
  complete_projects() {
    compadd $(td lsproj -a)
  }

  local done_flag=""
  local done_pattern="(-D)|(--done)"
  # check if done flag is set
  if (($words[(I)$done_pattern])); then
    done_flag="-D"
  fi

  local command_name=${words[2]}
  local -a command_flags=(
    "(- *)"{-h,--help}"[show CLI help]"
    "(- *)"{-v,--version}"[show version]"
  )
  set_flags() {
    case $command_name in
      add)
        command_flags=(
          "(- *)"{-h,--help}"[show command help]"
          "(-P --priority)"{-P,--priority}"[priority (A-Z)]: :({A..Z})"
          "*"{-p,--project}"[one or more projects]: :complete_projects"
          "*"{-c,--context}"[one or more contexts]: :complete_contexts"
          "(-d --due)"{-d,--due}"[due date]:"
        )
      ;;
      do)
        command_flags=(
          "(- *)"{-h,--help}"[show command help]"
          "*:id:($(td lsid))"
        )
      ;;
      undo)
        command_flags=(
          "(- *)"{-h,--help}"[show command help]"
          "*:id:($(td lsid -D))"
        )
      ;;
      rm)
        command_flags=(
          "(- *)"{-h,--help}"[show command help]"
          "(-D --done)"{-D,--done}"[delete done tasks]"
          "*:id:($(td lsid $done_flag))"
        )
      ;;
      restore)
        command_flags=(
          "(- *)"{-h,--help}"[show command help]"
          "(-f --force)"{-f,--force}"[force overwriting without confirmation]"
          "(-D --done)"{-D,--done}"[restore done.json]"
        )
      ;;
      ls)
        command_flags=(
          "(- *)"{-h,--help}"[show command help]"
          "(-a --all)"{-a,--all}"[list all tasks including done ones]"
          "(-D --done)"{-D,--done}"[list only done tasks]"
          "(-P --priority)"{-P,--priority}"[priority (A-Z)]: :({A..Z})"
          "--without-projects[list tasks without projects]"
          "--without-contexts[list tasks without contexts]"
          "--and-projects[filter projects using AND operator instead of OR]"
          "--and-contexts[filter contexts using AND operator instead of OR]"

          "*"{-p,--project}"[one or more projects]: :complete_projects"
          "*"{-c,--context}"[one or more contexts]: :complete_contexts"
        )
      ;;
      lsid)
        command_flags=(
          "(- *)"{-h,--help}"[show command help]"
          "(-D --done)"{-D,--done}"[list IDs of only done tasks]"
        )
      ;;
      lsctx)
        command_flags=(
          "(- *)"{-h,--help}"[show command help]"
          "(-a --all)"{-a,--all}"[list contexts of all tasks including done ones]"
          "(-D --done)"{-D,--done}"[list contexts of only done tasks]"
        )
      ;;
      lsproj)
        command_flags=(
          "(- *)"{-h,--help}"[show command help]"
          "(-a --all)"{-a,--all}"[list projects of all tasks including done ones]"
          "(-D --done)"{-D,--done}"[list projects of only done tasks]"
        )
      ;;
      modify)
        command_flags=(
          "(- *)"{-h,--help}"[show command help]"
          "(-D --done)"{-D,--done}"[modify done tasks]"
          "(-t, --text)"{-t,--text}"[modify text]:"
          "(-P, --priority)"{-P,--priority}"[priority (A-Z)]: :({A..Z})"
          "*"{-p,--project}"[one or more projects (overwrite all)]: :complete_projects"
          "*"{-c,--context}"[one or more contexts (overwrite all)]: :complete_contexts"
          "(-d --due)"{-d,--due}"[modify due date]:"
          "--delete-priority[delete priority]"
          "--delete-projects[delete projects]"
          "--delete-contexts[delete contexts]"
          "--delete-due[delete due date]"
          "*:id:($(td lsid $done_flag))"
        )
      ;;
    esac
  }
  if [ $CURRENT -gt 2 ]; then
    set_flags
  fi

  echo $state >> /tmp/zsh_test

  _arguments "1: :describe_commands" $command_flags
}

_td
