 # Configuration:
 #   HUBOT_VSTS_API_TOKEN
 #   HUBOT_VSTS_API_URL
+#   HUBOT_VSTS_SERVICEACCOUNT
 #
 # Commands:
 #   hubot vsts projects - Gets the list of projects available in Visual Studio Team Services
 #   hubot vsts <project> pullrequests <showDetails>- Gets the active pull requests for the specified Visual Studio Team Services project. <showDetails> is optional 
+#   hubot vsts <project> newPBI <title>- Creates a new PBI in the specified Visual Studio Team Services project with the given title. <title> is required 
 #
 # Author:
 #   Nithin Shenoy <nshenoy@mimeo.com>
 
 vstsToken = 'Basic ' + new Buffer(":#{process.env.HUBOT_VSTS_API_TOKEN}").toString('base64')
 vstsBaseUrl = process.env.HUBOT_VSTS_DEFAULTCOLLECTION_URL
+vstsServiceAccount = process.env.HUBOT_VSTS_SERVICEACCOUNT
 
 module.exports = (robot) ->
-  waitMessages = [
+  prWaitMessages = [
     "Hold on, checking for active PRs ",
     "Wait a sec, let me see if I can find the active PRs ",
     "Mmmmkay, searching for PRs "
   ]
 
+  pbiWaitMessages = [
+    "Give me a sec to create your new PBI ",
+    "Creating new PBI ",
+    "On it, homeslice. Creating a new PBI "
+  ]
+
   robot.respond /vsts projects$/i, (res) ->
     unless (ensureEnvironment res)
       res.send "Retrieving list of VSTS projects..."


  unless process.env.HUBOT_VSTS_DEFAULTCOLLECTION_URL?
    msg.send "VSTS DefaultCollection URL is missing: Make sure the HUBOT_VSTS_DEFAULTCOLLECTION_URL is set"
    missingConfig |= true
    
  missingConfig

    
    return attachment

#################################################################
## Creates attachment message object for new Product Backlog Item
#################################################################
createPBIAttachment = (pbiDetails, showDetails) ->
    pbiId = pbiDetails.id
    pbiTitle = pbiDetails.fields['System.Title']
    project = pbiDetails.fields['System.AreaPath']
    projectUrl = "#{vstsBaseUrl}/#{project}/_backlogs"
    pbiUrl = "#{vstsBaseUrl}/#{project}/_workitems?id=#{pbiId}"
    attachment = 
        fallback: "Product Backlog Item #{pbiId} in #{project} \"#{pbiTitle}\" #{pbiUrl}"
        text: "<#{pbiUrl}|Product Backlog Item #{pbiId}> - \"#{pbiTitle}\" in <#{projectUrl}|#{project}>"
        color: "#68217A"
        mrkdwn_in: ["text","fields"]
        author_name: "Visual Studio Team Services"
        author_icon: "https://zapier.cachefly.net/storage/services/59152a3a91bfe0ddd2fc9b978448593a.128x128.png"
    
    if showDetails
        details = 
            fields: [
                {
                        title: "State"
                        value: "`#{pbiDetails.fields['System.State']}`"
                        short: true
                },
                {
                        title: "Backlog Status"
                        value: "`#{pbiDetails.fields['System.BoardColumn']}`"
                        short: true
                },
                {
                        title: "Created By"
                        value: "`#{pbiDetails.fields['SystemCreatedBy']}`"
                        short: true
                },
                {
                        title: "Created Dated"
                        value: "`#{pbiDetails.fields['System.CreatedDate']}`"
                        short: true
                },
                {
                        title: "Description"
                        value: "#{pbiDetails.fields['System.Description']}"
                        short: false
                }
            ]
        attachment.fields = details.fields

    return attachment
    
#################################################
## Retrieves list of projects
#################################################
