const glob = require("glob"),
  fs = require('fs'),
  path = require('path');

const log = require('nl-clilog'),
  mkdirp = require('mkdirp');

const format = require('./format');

/**
 *  Reg Parse
 */
const LESSPARSE = /([..\/]+[\w-]+)+\.less/g;
//TODO Other fileTypes
const IMGPARSE = 0;

//TODO
//目前不可变
const DEFAULTDIST = 'build/';

/**
 *  Tree of Dependenies
 */
var Tree = {};

/**
 * @param {varType} path Description
 * @param {varType} option Description
 * @return {void} description
 */
function LessDiff(srcPath, distPath, options) {

  srcPath = srcPath || "src/**/*.less";
  distPath = distPath || DEFAULTDIST;
}



/**
 * @param {varType} src Description
 * @return {void} description
 */
LessDiff.prototype._getTree = function(srcPath, opts, callback) {
  glob(srcPath, opts, (err, files) => {
    if (err) return;
    files.forEach((file, idx) => {
      const data = fs.readFileSync(file, 'utf8');
      const dpPathList = this.getDpList(data, file);
      this.writeDpList(dpPathList, file);
    });
    fs.writeFileSync('tree.js', format(JSON.stringify(Tree)));
    log.debug('已在当前目录生成依赖关系文件"tree.js"');
    callback && callback(Tree);
  });
}


/**
 * @param {varType} path Description
 * @return {void} description
 */
LessDiff.prototype._getChangedList = function(path, pathList) {

  if (!pathList) pathList = [];
  if (!changedList) changedList = [];

  pathList.push(path);
  //changedList.push(path);

  if (Tree[path] && Tree[path].length > 0) {
    Tree[path].forEach((_path, idx) => {
      //changedList.push(_path);
      pathList.concat(this._getChangedList(_path, pathList));
    })
  }
  return pathList
}

/**
 * Get Dependencies Path List
 * @param {varType} buffer Description
 * @return {void} description
 */
LessDiff.prototype.getDpList = function(buffer, currentPath) {
  const list = buffer.match(LESSPARSE) || [];
  return list.map((_path, idx) => {
    const dirPath = path.dirname(currentPath);
    return path.join(dirPath, _path);
  });
}

/**
 * @param {varType} srcPath Description
 * @param {varType} pathsList Description
 * @return {void} description
 */
LessDiff.prototype.writeDpList = function(dpPathList, currentPath) {
  //if (!Tree[currentPath]) Tree[currentPath] = [];
  dpPathList.forEach((_path, idx) => {
    if (!Tree[_path]) Tree[_path] = [];
    Tree[_path].push(currentPath);
  });
  return Tree;
}

/**
 * @param {varType} _array Description
 * @return {void} description
 */
function _unique(_array) {
  var res = [],
    json = {};
  for (var i = 0; i < _array.length; i++) {
    if (!json[_array[i]]) {
      res.push(_array[i]);
      json[_array[i]] = 1;
    }
  }
  return res;
}

/**
 * Export LessDiff
 * @param {varType} path Description
 * @param {varType} options Description
 * @return {void} description
 */
exports.LessDiff = LessDiff;

@@ -1299,24 +1299,26 @@
 hList) %7B%0A%0A  
+//
 if (!pathLis
@@ -1341,47 +1341,311 @@
 ;%0A  
-if (!changedList) changedList = %5B%5D;%0A%0A  
+this.pathList = %5B%5D;%0A  this.changedList = %5B%5D;%0A%0A  this.redex(path);%0A  console.log(this.pathList);%0A  console.log(this.changedList);%0A%0A  return _unique(this.pathList);%0A%7D%0A%0ALessDiff.prototype.redex = function(path,hL) %7B%0A%0A  if(!hL) hL = %5B%5D;%0A  hL.push(path);%0A  %0A  var dpList = %5B%5D;%0A  //console.log(path);%0A%0A  this.
 path
@@ -1663,18 +1663,21 @@
 ath);%0A  
-//
+this.
 changedL
@@ -1685,20 +1685,59 @@
 st.push(
+hL.join('=%3E'));%0A  //console.log( Tree%5B
 path
+%5D
 );%0A%0A  if
@@ -1773,24 +1773,55 @@
 ngth %3E 0) %7B%0A
+%09 // console.log( Tree%5Bpath%5D);%0A
     Tree%5Bpat
@@ -1886,66 +1886,78 @@
 h);%0A
+%09
   
-    pathList.concat(this._getChangedList(_path, pathList
+//console.log('redex:'+path);%0A      dpList.push(this.redex(_path,hL
 ));%0A
@@ -1966,31 +1966,14 @@
   %7D)
+;
 %0A  %7D%0A
-  return pathList%0A
 %7D%0A%0A/
@@ -3147,8 +3147,9 @@
 ssDiff;%0A
+%0A

add _getChangedList