'use strict'

const request = require('request')
const Build = require('./build')
const utils = require('./utils')
const chalk = require('chalk')
const columnify = require('columnify')
const archy = require('archy')

module.exports = NodeTestPR

function NodeTestPR(opts) {
  if (!(this instanceof NodeTestPR))
    return new NodeTestPR(opts)

  opts = opts || {}
  this.opts = opts
  this.id = opts.id
  this.depth = opts.depth || 5
  this.creds = {
    username: opts.user
  , password: opts.token
  }

  this.status = ''
  this.duration = ''
  this.url = this.getUrl()
  this.rebaseOnto = ''
  this.startedBy = ''
  this.prUrl = ''
}

NodeTestPR.prototype.getUrl = function getUrl() {
  const u = utils.ciURL
  const id = this.id
  const depth = this.depth
  return `${u}/job/node-test-pull-request/${id}/api/json?depth=${depth}`
}

NodeTestPR.prototype.fetch = function fetch(cb) {
  const opts = {
    uri: this.getUrl()
  , json: true
  , auth: this.creds
  }

  request.get(opts, (err, res, body) => {
    if (err) return cb(err)
    if (res.statusCode !== 200) {
      const er = new Error(`Received non-200 status code: ${res.statusCode}`)
      er.body = body
      return cb(er)
    }

    cb(null, body)
  })
}

NodeTestPR.prototype.buildToJSON = function buildToJSON(build) {
  const out = {
    label: utils.formatType(build.result, build.name)
  , nodes: build.nodes.map((i) => {
      return this.buildToJSON(i)
    })
  }

  if (build.duration)
    out.label += `  (${build.duration})`

  return out
}

NodeTestPR.print = function(opts) {
  const pr = new NodeTestPR(opts)
  pr.fetch((err, res) => {
    if (err) throw err
    const data = []

    pr.status = res.building
      ? 'building'
      : utils.formatType(res.result)

    pr.duration = utils.parseMS(res.duration)
    const params = utils.getBuildParams(res)
    pr.rebaseOnto = params.rebaseOnto
    pr.startedBy = params.startedBy
    pr.prUrl = params.prUrl

    printLine('status', pr.status)
    printLine('duration', pr.duration)
    printLine('url', pr.url)
    printLine('id', pr.id)
    printLine('rebase onto', pr.rebaseOnto)
    printLine('started by', pr.startedBy)
    printLine('pull request', pr.prUrl)

    console.log(columnify(data, {
      showHeaders: false
    , config: {
        heading: {
          align: 'right'
        }
      }
    }))

    console.log()

    const build = new Build(res, 'node-test-pull-request')
    console.log(archy(pr.buildToJSON(build)))

    function printLine(heading, val) {
      data.push({
        heading: chalk.cyan(`     ${heading}  `)
      , value: val
      })
    }
  })
}

@@ -687,150 +687,79 @@
 %7B%0A  
-const u = utils.ciURL%0A  const id = this.id%0A  const depth = this.depth%0A  return %60$%7Bu%7D/job/node-test-pull-request/$%7Bid%7D/api/json?depth=$%7B
+return utils.getBuildUrl(this.id, 'node-test-pull-request', this.
 depth
-%7D%60
+)
 %0A%7D%0A%0A

fix getUrl()