 # Resolve user name to a potential GitHub username by using, in order:
 #   - GitHub credentials as handled by the github-credentials.coffee script
 #   - environment variables in the form of HUBOT_GITHUB_USER_.*
-#   - the exact chat name
+#   - throws an Error if the username can't be matched
 #
 # user - The chat username to be resolved
 #

  name = user.name.replace('@', '')
  resolve_cred = (u) -> u.githubLogin
  resolve_env = (n) -> process.env["HUBOT_GITHUB_USER_#{n.replace(/\s/g, '_').toUpperCase()}"]
  resolve_cred(user) or resolve_env(name) or resolve_env(name.split(' ')[0]) or name

# Resolve all parameters for a given subscription/unsubscription request:
# chat user, qualified GitHub user, kind of notification, repository, qualified GitHub repository.

  # return the array
  userInfos

# Send a private message to multiple users.
#
# robot - The current robot instance
# user - A User array, each one is as stored in a `respond` msg object
# message - The private message String
#
# Returns nothing.
private_messages = (robot, users, message) ->
  users.forEach (user) ->
    private_message robot, user, message

# Send a private message to a given user.
#
# robot - The current robot instance
# user - A User, as stored in a `respond` msg object
# message - The private message String
#
# Returns nothing.
private_message = (robot, user, message) ->
  user = get_pm_user user
  robot.send {user: user}, message

# Delete the reply_to information from a given User,
# so that it is possible to force a private message on send.
#
# user - A User instance
#
# Returns nothing.
get_pm_user = (user) ->
  delete user.reply_to
  user

module.exports = (robot) ->

  github = require('githubot')(robot)

  # handle notification subscriptions
  robot.respond NOTIFY_REGEX, (msg) ->
    try
      params = resolve_params github, msg
      add_notification(robot, params)
      reply = "You will now receive notifications for #{params.kind} #{(if params.qualified_repo? then 'in ' + params.qualified_repo else 'everywhere')}."
    catch error
      reply = error
    finally
      robot.brain.save
      private_message robot, get_pm_user(msg.message.user), reply

  # handle notification unsubscriptions
  robot.respond UNNOTIFY_REGEX, (msg) ->
    try
      params = resolve_params github, msg
      remove_notification(robot, params)
      reply = "You will no longer receive notifications for #{params.kind} #{(if params.qualified_repo? then 'in ' + params.repo else 'anywhere')}."
    catch error
      reply = error
    finally
      robot.brain.save
      private_message robot, get_pm_user(msg.message.user), reply

  # handle notification subscription listing
  robot.respond LIST_REGEX, (msg) ->
    try
      params = resolve_params github, msg, 2
      # get the notifications
      userSettings = _.find(robot.brain.get(GITHUB_NOTIFY_PRE + params.kind), {userName: params.qualified_user}) or {}
      userNotifications = userSettings.userNotifications or []
      # build an appropriate response by joining all checked repositories
      if userNotifications?.length > 0
        s = _.map(userNotifications, (n) ->
          if n is true then 'every repository' else n
        ).join(', ')
        reply = "You will receive notifications for #{params.kind} in #{s}."
      else reply = "You will receive no notifications for #{params.kind}."
    catch error
      reply = error
    finally
      private_message robot, get_pm_user(msg.message.user), reply

  # handle new comments and new issue assignments
  robot.router.post '/hubot/gh-notify', (req, res) ->
