 #
 # Dependencies:
 #   "githubot": "^1.0.0-beta2"
+#   "lodash": "^2.4.1"
 #
 # Configuration:
 #   HUBOT_GITHUB_TOKEN
-#   HUBOT_GITHUB_USER for a default user, in case it is missing
-#   HUBOT_GITHUB_USER_(.*) to map GitHub users to
-#   HUBOT_GITHUB_API
-#
-#   Put this url <HUBOT_URL>:<PORT>/hubot/gh-commits?room=<room> into your github hooks
+#   HUBOT_GITHUB_USER for a default user, in case it is missing from a repository definition
+#   HUBOT_GITHUB_USER_(.*) to map chat users to GitHub users
+#
+#   Put this url <HUBOT_URL>:<PORT>/hubot/gh-notify into your GitHub Webhooks
+#   and enable 'issues', 'pull_request' and 'issue_comment' as events to receive.
 #
 # Commands:
 #   hubot notify [me] [of] mentions [in <user/repo>] -- Registers the calling user for mentions in a repo's issues.

  # handle new comments and new issue assignments
  robot.router.post '/hubot/gh-notify', (req, res) ->
    payload = req.body
    console.log payload
    # discriminate the payload according to the action type
    res.send 'OK'


    user, qualified_user, kind, repo, qualified_repo
  }

# Add a notification to the user settings, checking that all other notifications of the same kind
# don't overlap with the new one (highest priority, same exact notification, etc.)
add_notification = (robot, params) ->
  # get all specific notifications of user
  userSettings = _.find(robot.brain.get(GITHUB_NOTIFY_PRE + params.kind), {userName: params.qualified_user}) or {}
  userNotifications = userSettings.userNotifications or []
  userInfo = userSettings.info or {}

  # if a global notification is already contained, throw an error
  if userNotifications.indexOf(true) >= 0
    throw new Error("You are already set to be notified for #{params.kind} in every repo.")

  # if the notification is already contained in the collection, throw an error
  if userNotifications.indexOf(params.qualified_repo) >= 0
    throw new Error("You are already set to be notified for #{params.kind} in #{params.qualified_repo}.")

  # if the notification is for every repo, remove all the notifications of the same kind
  if not params.qualified_repo?
    userNotifications = []

  # add the new notification
  userNotifications.push not params.repo? or params.qualified_repo
  # update the user information
  userInfo = params.user
  # build the new userSettings
  newUserSettings = {
    userName: params.qualified_user
    userNotifications: userNotifications,
    userInfo: userInfo
  }

  newSettings = robot.brain.get(GITHUB_NOTIFY_PRE + params.kind) or []
  _.remove(newSettings, {userName: params.qualified_user})
  newSettings.push(newUserSettings)

  # update the user info
  robot.brain.set GITHUB_NOTIFY_PRE + params.kind, newSettings

# Remove a notification from the user settings, if it exists, otherwise it throws an appropriate Error
remove_notification = (robot, params) ->
  # get all specific notifications of user
  userSettings = _.find(robot.brain.get(GITHUB_NOTIFY_PRE + params.kind), {userName: params.qualified_user}) or {}
  userNotifications = userSettings.userNotifications or []
  userInfo = userSettings.info or {}

  # if the notification is for every repo, remove all the notifications of the same kind
  if not params.qualified_repo?
    userNotifications.length = 0
    throw new Error("You have no notifications set up for #{params.kind}.") if deleted?.length <= 0
  else
    deleted = _.remove(userNotifications, params.qualified_repo)
    throw new Error("You have no notifications set up for #{params.kind} in #{params.qualified_repo}.") if deleted?.length <= 0

  # build the new userSettings
  newUserSettings = {
    userName: params.qualified_user
    userNotifications: userNotifications,
    userInfo: userInfo
  }

  newSettings = robot.brain.get(GITHUB_NOTIFY_PRE + params.kind) or []
  _.remove(newSettings, {userName: params.qualified_user})
  newSettings.push(newUserSettings)

  # update the user info
  robot.brain.set GITHUB_NOTIFY_PRE + params.kind, newSettings

# Handle a generic comment, it can be the first of the issue (the issue itself) or any next comment
on_commented_issue = (robot, comment, repository, users) ->
  userInfos = []
  # find all users that want to be notified of mentions
  for user in users
    userName = user.userName
    # match the repository first, as it usually is the easier check
    match_repo = user.userNotifications.indexOf(true) >= 0 or user.userNotifications.indexOf(repository.full_name) >= 0
    # after matching repo, also match for a mention
    if match_repo is true and comment.body.indexOf('@' + userName) >= 0
      userInfos.push user.userInfo
  # return the array
  userInfos

# Handle a generic comment, it can be the first of the issue (the issue itself) or any next comment
on_assigned_issue = (robot, assignee, repository, users) ->
  userInfos = []
  # find all users that want to be notified of mentions
  for user in users
    userName = user.userName
    # match the repository first, as it usually is the easier check
    match_repo = user.userNotifications.indexOf(true) >= 0 or user.userNotifications.indexOf(repository.full_name) >= 0
    # after matching repo, also match for a mention
    if match_repo is true and assignee.login is userName
      userInfos.push user.userInfo
  # return the array
  userInfos

module.exports = (robot) ->

