const { Box } = require('neo-blessed')

const EMPTY = Buffer.from('')

class Request extends Box {
  constructor (options) {
    options = {
      tags: true,
      scrollable: true,

      methodColors: {
        'GET': 'yellow',
        'POST': 'yellow',
        'HEAD': 'yellow',
        'PUT': 'purple',
        'PATCH': 'red',
        'DELETE': 'red'
      },

      ...options
    }

    super(options)
  }

  display (request) {
    const { method, scheme, host, port, path, query, version = 'HTTP/1.1', headers = {}, body = EMPTY } = request

    const methodColor = this.options.methodColors[method] || 'white'

    let addressBlock

    if ((scheme === 'http' && port === 80) || (scheme === 'https' && port === 443)) {
      addressBlock = `${host}`
    } else {
      addressBlock = `${host}:${port}`
    }

    const headersBlock = Object.entries(headers).map(([name, value]) => {
      if (!Array.isArray(value)) {
        value = [value]
      }

      return value.map((value) => {
        return `{purple-fg}${name}:{/pruple-fg} ${value}`
      }).join('\n')
    }).join('\n')

    const bodyBlock = body.toString()

    this.setContent(`{${methodColor}-fg}${method}{/${methodColor}-fg} ${scheme}://${addressBlock}${path}${query ? '?' + query : ''} ${version}\n${headersBlock}\n${bodyBlock}`)
  }
}

module.exports = Request