const clrz  = require('colorz')
const clog  = require('json-colorz')
const slice = require('toolz/src/array/sliced')

// clrz settings for colorized output
const blk = clrz.black
const dim = clrz.dim
const blu = clrz.blue
const grn = clrz.green
const mag = clrz.magenta

module.exports = {
  logger: function () {
    var args = slice(arguments).join(' ') + '\n'
    return process.stdout.write(args)
  },

  dry: function (conf) {
    clog(conf)
  },

  log: function (conf) {
    // log metadata to stdout
    var opts = conf.meta
    var chkrmt = opts.type === 'public' && !opts.noRemote
    var chkpsh = opts.type === 'public' && !opts.noRemote && !opts.noPush
    var chktyp = opts.type === 'public' && opts.noRemote
      ? 'public but no remote'
      : opts.type

    this.logger()
    this.logger(blu(opts.date))
    this.logger()
    this.logger(dim(blk('---------------------------------------')))
    this.logger(grn('  user  :'), mag(opts.author + ' (' + opts.name + ')'))
    this.logger(grn('  email :'), mag(opts.email))
    this.logger(dim(blk('  ----------------')))
    this.logger(grn('  name        :'), mag(opts.packageName))
    this.logger(grn('  version     :'), mag(opts.version))
    this.logger(grn('  type        :'), mag(chktyp))
    this.logger(grn('  repository  :'), mag(opts.repo))
    this.logger(grn('  command     :'), mag(opts.remoteCmd))
    this.logger(grn('  remote      :'), mag(chkrmt))
    this.logger(grn('  push        :'), mag(chkpsh))
    this.logger(dim(blk('  ----------------')))
    this.logger(grn('  license     :'), mag(opts.license))
    this.logger(grn('  description :'), mag(opts.description))
    this.logger(grn('  packages    :'), mag(conf.install
      ? conf.packages.concat(conf.devpackages).join(', ')
      : 'none'
    ))
    // this.logger(grn('  tags        : ') + mag(opts.tags))
    this.logger(dim(blk('---------------------------------------')))
  },

  done: function () {
    this.logger()
    this.logger(mag('All done.'))
  },

  heading: function (title) {
    this.logger()
    this.logger(blu(title))
    this.logger()
  },

  event: function (label, text, clr) {
    this.logger(dim(blk('  ' + label + '  ')), clrz[clr](text))
  },

  error: function (err) {
    process.stderr.write(err + '\n')
    process.exit(1)
  }
}

@@ -262,16 +262,37 @@
 .magenta
+%0Aconst red = clrz.red
 %0A%0Amodule
@@ -564,16 +564,17 @@
 hkrmt = 
+(
 opts.typ
@@ -591,30 +591,38 @@
 lic' && 
-!
 opts.
-noR
+r
 emote
+ !== false)
 %0A    var
@@ -661,17 +661,14 @@
  && 
-!
 opts.
-noR
+r
 emot
@@ -676,17 +676,14 @@
  && 
-!
 opts.
-noP
+p
 ush%0A
@@ -1373,24 +1373,33 @@
    :'), mag(
+chkrmt ? 
 opts.remoteC
@@ -1401,11 +1401,17 @@
 mote
-Cmd
+ : 'none'
 ))%0A 
@@ -2310,16 +2310,16 @@
 + '%5Cn')%0A
-
     proc
@@ -2330,14 +2330,246 @@
 exit(1)%0A
+  %7D,%0A%0A  stderr: function (text, module) %7B%0A    this.logger(dim(blk('error installing: ')), grn(module))%0A    this.logger(red(text))%0A  %7D,%0A%0A  stdout: function (text) %7B%0A    this.logger(dim(blk('installed: ')))%0A    this.logger(grn(text))%0A
   %7D%0A%7D%0A

implement stderr and stdout display functions