/*
  FancyLog - Logging made Pretty
  By Jack Baron
  Copyright (c) Jack Baron 2016
  Licensed under ISC License

  Logging Module
*/

// Require Dependants
const create  = require('create-if-not-exist')
const fs      = require('fs')

// Number Formatting for Date
if (typeof Number.prototype.digitFormat !== 'function') {
  Number.prototype.digitFormat = function format () {
    let parsed = `0${this}`.slice(-2)
    return parsed
  }
}

/**
 * FancyLog Constructor
 * @constructor
 * @param {string} [path] - Output Log Path
 */
let FancyLog = function FancyLog (path) {
  this.path = path
}

/**
 * Timestamp Creation
 * @returns {string} Current Timestamp
 */
const _timestamp = () => {
  // Create a timestamp
  let d = new Date()

  let date = `${d.getDate().digitFormat()}/${(d.getMonth() + 1).digitFormat()}/${d.getFullYear()}`
  let time = `${d.getHours().digitFormat()}:${d.getMinutes().digitFormat()}:${d.getSeconds().digitFormat()}`

  let timestamp = `[${date} ${time}]`
  return timestamp
}

/**
 * Logging function
 * @param {string} msg - Message to Log
 * @param {string} [level] - Logging Level
 */
const _log = (msg, level) => {
  // Define Level Colour
  // let colour
  switch (level.toLowerCase()) {
    case 'info':
      // colour = termGreen
      break
    case 'debug':
      // colour = termCyan
      break
    case 'error':
      // colour = termRed
      break
    case 'verbose':
      // colour = termMagenta
      break
    case 'warn':
      // colour = termYellow
      break
    default:
      // colour = termGreen
      break
  }

  // Level text is level all uppercase bcus reasons
  let levelText = level.toUpperCase()

  let padding = '', numSpaces = 7 - levelText.length
  for (let i = 0; i < numSpaces; i++) {
    padding += ' '
  }

  // Create formatted messages for console and text output
  let timestamp = _timestamp()
  let termData  = `[${levelText}]${padding} ${timestamp} ${msg}`
  let logData 	= `[${levelText}]${padding} ${timestamp} ${msg}`

  // Show this in the console
  console.log(termData)

  // If a path was given, ouput.
  if (this.path !== undefined) {
    try {
      create(this.path, '')
      fs.appendFile(this.path, `${logData}\n`, 'utf8', err => { if (err) console.error(`Failed to output to log.`) })
    } catch (ex) {
      console.log(ex)
    }
  }
}

/**
 * Logs at INFO Level
 * @param {string} msg - Message to Log
 */
const _info = msg => {
  _log(msg, 'info')
}

/**
 * Logs at DEBUG Level
 * @param {string} msg - Message to Log
 */
const _debug = msg => {
  _log(msg, 'debug')
}

/**
 * Logs at ERROR Level
 * @param {string} msg - Message to Log
 */
const _error = msg => {
  _log(msg, 'error')
}

/**
 * Logs at VERBOSE Level
 * @param {string} msg - Message to Log
 */
const _verbose = msg => {
  _log(msg, 'verbose')
}

/**
 * Logs at WARN Level
 * @param {string} msg - Message to Log
 */
const _warn = msg => {
  _log(msg, 'warn')
}

FancyLog.prototype.info     = _info
FancyLog.prototype.i        = _info

FancyLog.prototype.debug    = _debug
FancyLog.prototype.d        = _debug

FancyLog.prototype.error    = _error
FancyLog.prototype.e        = _error

FancyLog.prototype.verbose  = _verbose
FancyLog.prototype.v        = _verbose

FancyLog.prototype.warn     = _warn
FancyLog.prototype.w        = _warn

module.exports = FancyLog

@@ -429,24 +429,191 @@
 rsed%0A  %7D%0A%7D%0A%0A
+const colours = %7B%0A  red: %60%5Cx1b%5B31m%60,%0A  green: %60%5Cx1b%5B32m%60,%0A  yellow: %60%5Cx1b%5B33m%60,%0A  blue: %60%5Cx1b%5B34m%60,%0A  magenta: %60%5Cx1b%5B35m%60,%0A  cyan: %60%5Cx1b%5B36m%60,%0A  reset: %60%5Cx1b%5B39m%60,%0A%7D%0A%0A
 /**%0A * Fancy
@@ -1335,19 +1335,16 @@
 Colour%0A 
- //
  let col
@@ -1394,35 +1394,32 @@
 se 'info':%0A     
- //
  colour = termGr
@@ -1404,37 +1404,41 @@
 %0A      colour = 
-termG
+colours.g
 reen%0A      break
@@ -1453,35 +1453,32 @@
 e 'debug':%0A     
- //
  colour = termCy
@@ -1475,13 +1475,17 @@
 r = 
-termC
+colours.c
 yan%0A
@@ -1511,35 +1511,32 @@
 e 'error':%0A     
- //
  colour = termRe
@@ -1533,13 +1533,17 @@
 r = 
-termR
+colours.r
 ed%0A 
@@ -1570,35 +1570,32 @@
 'verbose':%0A     
- //
  colour = termMa
@@ -1592,13 +1592,17 @@
 r = 
-termM
+colours.m
 agen
@@ -1634,27 +1634,24 @@
 warn':%0A     
- //
  colour = te
@@ -1652,13 +1652,17 @@
 r = 
-termY
+colours.y
 ello
@@ -1693,19 +1693,16 @@
 t:%0A     
- //
  colour 
@@ -1707,13 +1707,17 @@
 r = 
-termG
+colours.g
 reen
@@ -2051,16 +2051,25 @@
 ata  = %60
+$%7Bcolour%7D
 %5B$%7Blevel
@@ -2077,32 +2077,48 @@
 ext%7D%5D$%7Bpadding%7D 
+$%7Bcolours.reset%7D
 $%7Btimestamp%7D $%7Bm

Bring back colour support