#!/usr/bin/env node
/* eslint-disable no-console */

const defaultsDeep = require('lodash/defaultsDeep')
const debug = require('debug')('nuxt:build')
const parseArgs = require('minimist')
const chokidar = require('chokidar')

const { version } = require('../package.json')
const { Nuxt, Builder, Utils } = require('..')

const { loadNuxtConfig, getLatestHost, nuxtConfigFile } = require('./common/utils')

debug.color = 2 // force green color

const argv = parseArgs(process.argv.slice(2), {
  alias: {
    h: 'help',
    H: 'hostname',
    p: 'port',
    c: 'config-file',
    s: 'spa',
    u: 'universal',
    v: 'version'
  },
  boolean: ['h', 's', 'u', 'v'],
  string: ['H', 'c'],
  default: {
    c: 'nuxt.config.js'
  }
})

if (argv.version) {
  console.log(version)
  process.exit(0)
}

if (argv.hostname === '') {
  Utils.fatalError('Provided hostname argument has no value')
}

if (argv.help) {
  console.log(`
    Description
      Starts the application in development mode (hot-code reloading, error
      reporting, etc)
    Usage
      $ nuxt dev <dir> -p <port number> -H <hostname>
    Options
      --port, -p          A port number on which to start the application
      --hostname, -H      Hostname on which to start the application
      --spa               Launch in SPA mode
      --universal         Launch in Universal mode (default)
      --config-file, -c   Path to Nuxt.js config file (default: nuxt.config.js)
      --help, -h          Displays this message
  `)
  process.exit(0)
}

// Load config once for chokidar
const nuxtConfig = loadNuxtConfig(argv)
defaultsDeep(nuxtConfig, { watchers: { chokidar: { ignoreInitial: true } } })

// Start dev
let dev = startDev()
let needToRestart = false

// Start watching for nuxt.config.js changes
chokidar
  .watch(nuxtConfigFile(argv), nuxtConfig.watchers.chokidar)
  .on('all', () => {
    debug('[nuxt.config.js] changed')
    needToRestart = true

    dev = dev.then(instance => {
      if (needToRestart === false) return instance
      needToRestart = false

      debug('Rebuilding the app...')
      return startDev(instance)
    })
  })

function startDev(oldInstance) {
  // Get latest environment variables
  const { port, host } = getLatestHost(argv)

  // Error handler
  const onError = (err, instance) => {
    Utils.printError(err)
    return Promise.resolve(instance) // Wait for next reload
  }

  // Load options
  let options = {}
  try {
    options = loadAndAugmentNuxtConfig()
  } catch (err) {
    return onError(err, oldInstance)
  }

  // Create nuxt and builder instance
  let nuxt
  let builder
  let instance
  try {
    nuxt = new Nuxt(options)
    builder = new Builder(nuxt)
    instance = { nuxt, builder }
  } catch (err) {
    return onError(err, oldInstance)
  }

  return (
    Promise.resolve()
      .then(
        () =>
          oldInstance && oldInstance.builder
            ? oldInstance.builder.unwatch()
            : Promise.resolve()
      )
      // Start build
      .then(() => builder.build())
      // Close old nuxt after successful build
      .then(
        () =>
          oldInstance && oldInstance.nuxt
            ? oldInstance.nuxt.close()
            : Promise.resolve()
      )
      // Start listening
      .then(() => nuxt.listen(port, host))
      // Pass new nuxt to watch chain
      .then(() => instance)
      // Handle errors
      .catch(err => onError(err, instance))
  )
}

function loadAndAugmentNuxtConfig() {
  const options = loadNuxtConfig(argv)

  // Force development mode for add hot reloading and watching changes
  options.dev = true

  return options
}