import { homedir } from 'os'
import { resolve, basename } from 'path'
import { exec } from 'child_process'
import { writeFileSync } from 'fs'
import mkdirp from 'mkdirp'
import debugFactory from 'debug'
import shellEnv from 'shell-env'

const debug = debugFactory('halland-proxy:plugins')

function getPluginDir () {
  const pluginDir = resolve(homedir(), '.halland-proxy-plugins')
  mkdirp.sync(pluginDir)
  return pluginDir
}

export function syncPlugins (plugins = []) {
  const packageJsonPath = resolve(getPluginDir(), 'package.json')
  const packageJson = require(packageJsonPath)
  packageJson.dependencies = []
  plugins.forEach(p => (packageJson.dependencies[p] = 'latest'))

  writeFileSync(packageJsonPath, JSON.stringify(packageJson))
}

export function loadPlugins (plugins = []) {
  const pluginDir = getPluginDir()
  const paths = plugins.map(plugin => {
    return resolve(pluginDir, 'node_modules', plugin.split('#')[0])
  })

  const pluginSetup = {
    requestSetup: [],
    requestPipe: [],
    responseHeaders: [],
    responsePipe: []
  }

  paths.forEach(path => {
    let plugin
    const name = basename(path)
    try {
      plugin = require(path)()
    } catch (err) {
      debug(`Failed to load plugin ${name}.`)
    }
    plugin = plugin || {}
    plugin.requestSetup = plugin.requestSetup || []
    plugin.requestPipe = plugin.requestPipe || []
    plugin.responseHeaders = plugin.responseHeaders || []
    plugin.responsePipe = plugin.responsePipe || []

    pluginSetup.requestSetup = [...pluginSetup.requestSetup, ...plugin.requestSetup]
    pluginSetup.requestPipe = [...pluginSetup.requestPipe, ...plugin.requestPipe]
    pluginSetup.responseHeaders = [...pluginSetup.responseHeaders, ...plugin.responseHeaders]
    pluginSetup.responsePipe = [...pluginSetup.responsePipe, ...plugin.responsePipe]
  })

  return plugins
}

export function updatePlugins (cb) {
  shellEnv().then(env => {
    env.npm_config_runtime = 'electron'
    env.npm_config_target = process.versions.electron
    env.npm_config_disturl = 'https://atom.io/download/atom-shell'
    exec('npm prune && npm install --production', {
      cwd: getPluginDir(),
      env
    }, err => {
      if (err) {
        return cb(err)
      }
      cb(null)
    })
  }).catch(cb)
}


@@ -108,16 +108,28 @@
 import %7B
+ existsSync,
  writeFi
@@ -147,16 +147,16 @@
 om 'fs'%0A
-
 import m
@@ -295,16 +295,171 @@
 gins')%0A%0A
+const defaultPluginPackageJson = %7B%0A  name: 'halland-proxy-plugins',%0A  version: '1.0.0',%0A  description: 'Plugins for Halland Proxy.',%0A  dependencies: %5B%5D%0A%7D%0A%0A
 function
@@ -706,52 +706,188 @@
 )%0A  
-const packageJson = require(packageJsonPath)
+let packageJson = null%0A  if (existsSync(packageJsonPath)) %7B%0A    packageJson = require(packageJsonPath)%0A  %7D else %7B%0A    packageJson = Object.assign(%7B%7D, defaultPluginPackageJson)%0A  %7D%0A
 %0A  p

Handle plugins when package.json does not exists for plugins.