import colors from 'colors'
import path from 'path'
import Promise from 'bluebird'

const fs = Promise.promisifyAll(require('fs'))

const {
  readdirAsync,
  renameAsync,
  statAsync,
} = fs

class Traverser {
  constructor (initialPath, options) {
    const { excludes, dest, src } = options

    this.initialPath = initialPath
    this.excludes = excludes
    this.dest = dest
    this.src = src
  }

  async traverse (options = {}, rootPath = this.initialPath) {
    const { dry } = options
    const files = await readdirAsync(rootPath)

    for (let file of files) {
      const filePath = makePath(rootPath, file)

      const stats = await statAsync(filePath)

      if (stats.isDirectory() && !this.excludes.has(file)) {
        await this.traverse(options, filePath)
      } else {
        await handleFile(file, filePath, this.src, this.dest, dry)
      }
    }

    return Promise.resolve()
  }

}

const handleFile = async (file, filePath, src, dest, dry) => {
  const splitFilePath = filePath.split('.')
  const filePathNoExt = splitFilePath[0]
  const srcExt = splitFilePath[1]

  if (srcExt === src) {
    if (dry) {
      console.log(`${filePath}`.red + ' --> '.yellow + `${filePathNoExt}.${dest}`.green)
    } else {
      console.log(`Renaming: ${filePathNoExt}`.green)
      await renameAsync(filePath, `${filePathNoExt}.${dest}`)
    }
  }
}

const makePath = (base = __dirname, file) => path.join(base, file)

export default Traverser

@@ -1245,17 +1245,17 @@
 ole.log(
-%60
+'
 Renaming
@@ -1256,16 +1256,63 @@
 naming: 
+'.blue + %60$%7BfilePath%7D%60.red + ' --%3E '.yellow + %60
 $%7BfilePa
@@ -1319,16 +1319,24 @@
 thNoExt%7D
+.$%7Bdest%7D
 %60.green)

Change output on rename