"use strict";

(function () {

  var async = require('async');
  var chalk = require('chalk');
  var fs = require('fs');

  var fsp = require('../../../utils/fs.js');
  var walk = require('../../../utils/walk.js');
  var path = require('../../../utils/path.js');

  module.exports = function (data, callback) {

    var dirname = path.join(__dirname, '..', 'template', data.scaffold);

    var regIncludes = [];

    var regExcludes = [/\.git/, /\.tmp/, /.DS_Store/, /README.md/];

    if (!data.exCoffee) {
      regExcludes.push(/coffee/);
    }
    if (!data.exES6) {
      regExcludes.push(/es6/);
    }
    if (!data.exLess) {
      regExcludes.push(/less/);
    }
    if (!data.exJade) {
      regExcludes.push(/jade/);
    }
    if (!data.exIcons) {
      regExcludes.push(/icons/);
    }
    if (!data.modCallClient) {
      regExcludes.push(/call-client/);
    }
    if (!data.modDownload) {
      regExcludes.push(/download/);
    }
    if (!data.modLandscape) {
      regExcludes.push(/landscape-tip/);
    }
    if (!data.modScroll) {
      regExcludes.push(/transation/);
      regExcludes.push(/animations/);
    }
    if (!data.modWebp) {
      regExcludes.push(/webp/);
    }
    if (!data.modWeight) {
      regExcludes.push(/weight/);
    }
    if (!data.modShakeHand) {
      regExcludes.push(/shake-hand/);
    }

    walk(dirname, regIncludes, regExcludes, function (err, results) {

      async.each(results, function (repo, done) {

        var stat = fs.lstatSync(repo);
        var rdirname = repo.replace(dirname, '').replace(path.sep, '');
        var fall = [];

        if (stat.isDirectory()) {

          fall.push(fsp.pMkdir(rdirname));
        } else if (!/^\./.test(rdirname)) {

          fall.push(fsp.pWriteFile(rdirname.replace('_', '.'), repo, data));
        }
        async.waterfall(fall, done);
      }, function () {
        callback();
      });
    });
  };
})();