"use strict";

(function () {
  var async = require('async');
  var chalk = require('chalk');
  var fs = require('fs');
  var childProcess = require('child_process');
  var assign = require("deep-assign");

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

  module.exports = function (dirname, options) {

    var spaceInfo = fsp.readJSONSync(path.join(dirname, 'efesproject.json'));

    var _spaceInfo = assign({
      "global": {
        "git": {
          "branch": {
            "local": "master",
            "remote": ""
          }
        }
      }
    }, spaceInfo);

    var _global = _spaceInfo.global;

    var localBranch = _global.git.branch.local;
    var remoteBranch = _global.git.branch.remote;
    var rBranch = new RegExp('\\*\\s' + localBranch + '\\b', 'm');
    var errors = [];

    var projects = _spaceInfo.projects;

    if (projects && projects.length > 0) {

      async.eachSeries(projects, function (pj, callback) {

        if (pj.git) {
          (function () {

            var repoName = (pj.git.host || _global.git.host) + pj.git.repo + ".git";
            //let repoPath = path.join(dirname, pj.git.mapping || pj.git.repo);// 取消git配置中的mapping参数，防止多人合作，git仓库本地目录不一致导致的跨库合目录不一致。
            var repoPath = path.join(dirname, pj.git.repo);

            var configRepo = function configRepo() {

              var _gitConfig = assign(_global.git.config, pj.git.config);

              if (_gitConfig) {
                for (var i in _gitConfig) {
                  childProcess.execSync('git config ' + i + ' "' + _gitConfig[i] + '"', {
                    cwd: repoPath
                  });
                }
              }

              childProcess.exec('efes hook', {
                cwd: repoPath
              }, function (err, stdout) {});
            };

            if (fs.existsSync(repoPath)) {
              childProcess.exec('git branch', {
                cwd: repoPath
              }, function (err, stdout) {
                console.log('\n');
                console.log(chalk.green('更新 '), '' + repoName);

                if (rBranch.test(stdout)) {

                  var _pull = childProcess.exec('git pull', {
                    cwd: repoPath,
                    stdio: 'inherit'
                  });

                  _pull.on('close', function () {
                    configRepo();
                    callback();
                  });
                } else {

                  console.log(chalk.yellow('Warnning'), repoName, '不是 ' + localBranch + ' 分支， 跳过更新。');
                  configRepo();
                  callback();
                }
              });
            } else {
              console.log('\n');
              console.log(chalk.green('克隆 '), '' + repoName);

              /*let _clone = childProcess.exec(`git clone ${repoName} ${pj.localDir}`, function(err, stdout, stderr) {
                 if (err) {
                   console.log(chalk.bgRed('Error'), repoName, stderr);
                  callback();
                 } else if (branch !== 'master') {
                   console.log(`检出 ${repoName} ${branch}`);
                  childProcess.exec(`git checkout -b ${branch}`, {
                    cwd: repoPath
                  }, function(err, stdout, stderr) {
                    if (err) {
                      console.log(chalk.bgRed('Error'), repoName, stderr);
                    }
                    callback();
                  });
                 } else {
                  callback();
                }
                configRepo();
              });*/
              var _clone = childProcess.spawn('git', ['clone', repoName, pj.git.repo], {
                stdio: 'inherit'
              });

              /*_clone.stdout.on('data', function(data) {
                console.log(data);
              });
               _clone.stderr.on('data', function(data) {
                console.log(chalk.red('ERROR: '), data);
              });*/

              _clone.on('exit', function (code) {

                console.log('---', code);

                configRepo();

                if (localBranch !== 'master') {

                  console.log(chalk.green('检出 '), repoName + ' 分支：' + localBranch + ' ' + remoteBranch);

                  var _checkout = childProcess.exec('git checkout -b ' + localBranch + ' ' + remoteBranch, {
                    cwd: repoPath,
                    stdio: 'inherit'
                  });

                  _checkout.on('exit', function (code) {
                    callback();
                  });
                }
              });
            }
          })();
        } else {
          callback();
        }
      }, function () {});
    }
  };
})();