                "(#{s[s.length-2].karma}), and \"#{s[s.length-3].name}\" " +
                "(#{s[s.length-3].karma})."
       else
-        msg.send "There aren't enough items with karma to give a top and bottom 3"
+        msg.send "There aren't enough items with karma to give a top and bottom 3"
module.exports = (robot) ->
  karma = new Karma robot
  robot.hear /(\S+[^+\s])\+\+(\s|$)/, (msg) ->
    karma.add msg.match[1].toLowerCase()
    msg.reply "The operation succeeded."

  robot.hear /(\S+[^-\s])--(\s|$)/, (msg) ->
    karma.subtract msg.match[1].toLowerCase()
    msg.reply "The operation succeeded."

  robot.respond /karma ?(\S*)/, (msg) ->
    if msg.match[1]
      match = msg.match[1].toLowerCase()
      msg.send "\"#{match}\" has #{karma.get(match)} karma."

# <thing>-- - take away some of thing's karma
# karma <thing> - check thing's karma, if <thing> is ommitted get top and bottom 3
class Karma
  
  constructor: (@robot) ->
    @cache = {}
    
    @increment_responses = [
      "+1!", "gained a level!", "is on the rise!", "leveled up!"
    ]
  
    @decrement_responses = [
      "took a hit! Ouch.", "took a dive.", "lost a life.", "lost a level."
    ]
    
    @robot.brain.on 'loaded', =>
      if @robot.brain.data.karma
        @cache = @robot.brain.data.karma

  increment: (thing) ->
    @cache[thing] ?= 0
    @cache[thing] += 1
    @robot.brain.data.karma = @cache

  decrement: (thing) ->
    @cache[thing] ?= 0
    @cache[thing] -= 1
    @robot.brain.data.karma = @cache
  
  incrementResponse: ->
     @increment_responses[Math.floor(Math.random() * @increment_responses.length)]
  
  decrementResponse: ->
     @decrement_responses[Math.floor(Math.random() * @decrement_responses.length)]

  get: (thing) ->
    k = if @cache[thing] then @cache[thing] else 0
