 Fuse = require 'fuse.js'
 
 module.exports = (robot) ->
+
+  send = (context, message, prependUsername=no) ->
+    robot.adapter.customMessage
+      channel: context.message.room
+      text: "#{if prependUsername then "<@#{context.message.user.id}> " else ""}#{message}"
 
   getNotifications = ->
     robot.brain.get('github-notifications') or []

    [__, time] = msg.match
    notificationsCleared = clearSpecificNotificationForRoom(findRoom(msg), time)
    if notificationsCleared is 0
      msg.send "Nice try. You don't even have a notification at #{time}"
    else
      msg.send "Deleted your #{time} notification"

  robot.respond /(?:github|gh|git) notification ((?:[01]?[0-9]|2[0-4]):[0-5]?[0-9])$/i, (msg) ->
    [__, time] = msg.match
    room = findRoom(msg)
    saveNotification room, time
    msg.send "Ok, from now on I'll remind this room about open pull requests every weekday at #{time}"

  robot.respond /(?:github|gh|git) list notifications$/i, (msg) ->
    notifications = getNotificationsForRoom(findRoom(msg))
    if notifications.length is 0
      msg.send "Well this is awkward. You haven't got any github notifications set :-/"
    else
      msg.send "You have pull request notifcations at the following times: #{_.map(notifications, (notification) -> notification.time)}"

  robot.respond /(?:github|gh|git) notifications in every room/i, (msg) ->
    notifications = getNotifications()
    if notifications.length is 0
      msg.send "No, because there aren't any."
    else
      msg.send """
        Here's the notifications for every room: #{_.map(notifications, (notification) -> "\nRoom: #{notification.room}, Time: #{notification.time}")}
      """

  robot.respond /(github|gh|git) help/i, (msg) ->
    msg.send """
      I can remind you about open pull requests for the repo that belongs to this channel
      Use me to create a notification, and then I'll post in this room every weekday at the time you specify. Here's how:


  listOpenPullRequestsForRoom = (room, user) ->
    repo = repos[room]
    if not repo
      send message: room: room, "There is no github repository associated with this room. Contact your friendly <@#{robot.name}> administrator for assistance"
      return

    repo = octo.repos(githubOrg, repo)
