/**
 * fis 中工具类操作集合。{@link https://lodash.com/ lodash} 中所有方法都挂载在此名字空间下面。
 * @param  {String} path
 * @return {String}
 * @example
 *   /a/b//c\d/ -> /a/b/c/d
 * @namespace fis.util
 */

var lodash = require('lodash');
var path = require('path');

var _ = module.exports = function (path) {
  var type = typeof path;
  if (arguments.length > 1) {
    path = Array.prototype.join.call(arguments, '/');
  } else if (type === 'string') {
    //do nothing for quickly determining.
  } else if (type === 'object') {
    path = Array.prototype.join.call(path, '/');
  } else if (type === 'undefined') {
    path = '';
  }
  if (path) {
    path = pth.normalize(path.replace(/[\/\\]+/g, '/')).replace(/\\/g, '/');
    if (path !== '/') {
      path = path.replace(/\/$/, '');
    }
  }
  return path;
};

// 将lodash内部方法的引用挂载到utils上，方便使用
lodash.assign(_, lodash);

_.pad = function (str, len, fill, pre) {
  if (str.length < len) {
    fill = (new Array(len)).join(fill || ' ');
    if (pre) {
      str = (fill + str).substr(-len);
    } else {
      str = (str + fill).substring(0, len);
    }
  }
  return str;
};

var IS_WIN = process.platform.indexOf('win') === 0;
/**
 * 是否为windows系统
 * @return {Boolean}
 * @memberOf fis.util
 * @name isWin
 * @function
 */
_.isWin = function () {
  return IS_WIN;
};

_.getAllFiles = function (root, ignoreDir) {
  var fs = require('fs');
  var res = [], files = fs.readdirSync(root);
  files.forEach(function (file) {
    var pathname = root + '/' + file
      , stat = fs.lstatSync(pathname);

    if (!stat.isDirectory()) {
      res.push({path: pathname, dir: false});
    } else {
      if ((typeof(ignoreDir) === 'string' && file === ignoreDir ) || (_.isArray(ignoreDir) && ignoreDir.indexOf(file) !== -1)) {
        console.log(file);
        return;
      }
      res.push({path: pathname, dir: true});
      res = res.concat(_.getAllFiles(pathname));
    }
  });
  return res
};

_.insertDir = function (dirArray, dirPath) {
  var dir = path.normalize(path.dirname(dirPath)).replace(/\\/g,'\/');
  if (dir !== '.' && dir !== path.sep) {
    _.insertDir(dirArray, dir);
    dirArray.push(dirPath);
  }else{
    dirArray.push(dirPath);
  }
};
