 #   Dweet.io powered Hubot Brain
 #
 # Dependencies:
-#   None
+#   "moment": "^2.5.1"
+#
+# Commands:
+#   hubot brain save - Forces a save to Dweet.io
+#   hubot brain status - Returns the brain status
 #
 # Configuration:
 #   DWEET_THING

    if not DWEET_THING?
        return

    robot.brain.resetSaveInterval(600)
    #robot.brain.resetSaveInterval(10)

    dweet_request robot, '/get/latest/dweet', null, (dweet) ->
        if dweet.with is 404
            robot.brain.mergeData {}
            robot.logger.info 'Initializing new data for brain'
            robot.brain.save()
            return
        else if dweet.this is 'failed'
            return robot.logger.error dweet.because

        content = decodeChildren dweet.with[0].content
        robot.brain.mergeData content
        robot.logger.info 'Data for brain retrieved from Dweet.io'

    robot.brain.on 'save', (data = {}) ->
        dweet_request robot, '/dweet', encodeChildren(data), (dweet) ->
            return robot.logger.error dweet.because if dweet.this is 'failed'
            robot.logger.info "Successfully Dweeted to #{DWEET_THING}"

dweet_request = (robot, endpoint, params = {}, handler) ->
    params['key'] = DWEET_KEY if DWEET_KEY?

#
# Author:
#   MrSaints

moment = require 'moment'

DWEET_THING = process.env.DWEET_THING
DWEET_KEY = process.env.DWEET_KEY
