 #                                                      (both of which may be abbreviated, Hubot
 #                                                      will ask you if your input is ambigious).
 #                                                      An existing timer (if any) will be stopped.
-#   hubot finish harvest at <project> - TODO
+#   hubot stop harvest at <project>/<task> - Stop the timer the for a task, if any.
 #   hubot daily harvest [of <user>] - Hubot responds with your/a specific user's entries for today
 #
 # Notes:

        task_id: task.id
      this.request(msg).path("/daily/add").post(JSON.stringify(data)) (err, res, body) ->
        callback res.statusCode, JSON.parse(body)

  # (internal method)
  # Assembles the basic parts of a request to the Harvest


  # Kick off a new timer, stopping the previously running one, if any.
  robot.respond /start harvest at (.+)\/(.+): (.*)/i, (msg) ->
    user    = msg.message.user
    project = msg.match[1]
    task    = msg.match[2]
    notes   = msg.match[3]

    # Check if we know the detected user's credentials.
    unless user.harvest_account
      msg.reply "You have to tell me your Harvest credentials first."
      return
    
    user.harvest_account.start msg, project, task, notes, (status, body) ->
      if 200 <= status <= 299
        if body.hours_for_previously_running_timer?
          msg.reply "Previously running timer stopped at #{body.hours_for_previously_running_timer}h."
        msg.reply "Started tracking. Back to your work, #{msg.message.user.name}!"
      else
        msg.reply "Request failed with status #{status}."

  # Stops the timer running for a project/task combination,
  # if any.
  robot.respond /stop harvest at (.+)\/(.+)/i, (msg) ->
    user    = msg.message.user
    project = msg.match[1]
    task    = msg.match[2]
    unless user.harvest_account
      msg.reply "You have to tell me your Harvest credentials first."
      return
    
    user.harvest_account.stop msg, project, task, (status, body) ->
      if 200 <= status <= 299
        msg.reply "Timer stopped."
      else
        msg.reply "Request failed with status #{status}."
        msg.reply body

  # Stop the first running timer it can find.
  # Not yet implemented.
  #robot.respond /stop harvest/i, (msg) ->
  #  msg.reply "Not yet implemented."

# Class managing the Harvest account associated with
# a user. Keeps track of the user's credentials and can
