                "(#{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"
# <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 = {}
    
    @robot.brain.on 'loaded', =>
      if @robot.brain.data.karma
        @cache = @robot.brain.data.karma

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

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

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

module.exports = (robot) ->
  karma = new Karma robot
  robot.hear /(\S+[^+\s])\+\+(\s|$)/, (msg) ->
    subject = msg.match[1].toLowerCase()
    karma.increment subject
    msg.reply "#{subject} #{karma.incrementResponse()} (Karma: #{karma.get(subject)})"
  
  robot.hear /(\S+[^-\s])--(\s|$)/, (msg) ->
    subject = msg.match[1].toLowerCase()
    karma.decrement subject
    msg.reply "#{subject} #{karma.decrementResponse()} (Karma: #{karma.get(subject)})"
  
  robot.respond /karma ?(\S+[^-\s])?$/i, (msg) ->
    if msg.match[1]
      match = msg.match[1].toLowerCase()
      msg.send "\"#{match}\" has #{karma.get(match)} karma."
