/**
 * Created by geweimin on 17/7/17.
 */
const {
  listTemplatesAndExit,
  createProjectFromTemplate,
} = require('../generator/templates');
const execSync = require('child_process').execSync;
const fs = require('fs');
const minimist = require('minimist');
const path = require('path');
// const printRunInstructions = require('../generator/printRunInstructions');
const process = require('process');
const yarn = require('../util/yarn');

/**
 * Creates the template for a React Native project given the provided
 * parameters:
 * @param projectDir Templates will be copied here.
 * @param argsOrName Project name or full list of custom arguments
 *                   for the generator.
 * @param options Command line options passed from the react-native-cli directly.
 *                E.g. `{ version: '0.43.0', template: 'navigation' }`
 */
function init(projectDir) {
  console.log('aaaa' + path.resolve(projectDir));
  // const args = Array.isArray(argsOrName)
  //   ? argsOrName // argsOrName was e.g. ['AwesomeApp', '--verbose']
  //   : [argsOrName].concat(process.argv.slice(4)); // argsOrName was e.g. 'AwesomeApp'
  const args = [projectDir];

  // args array is e.g. ['AwesomeApp', '--verbose', '--template', 'navigation']
  if (!args || args.length === 0) {
    console.error('mxf init requires a project name.');
    return;
  }

  const newProjectName = args[0];
  const options = minimist(args);

  // if (listTemplatesAndExit(newProjectName, options)) {
  //   // Just listing templates using 'react-native init --template'
  //   // Not creating a new app.
  //   return;
  // } else {
  //   const projectPath = path.resolve(projectDir);
  //   console.log('Setting up new React Native app in ' + projectPath);
  //   generateProject(projectPath, newProjectName, options);
  // }
  const projectPath = path.resolve(projectDir);
  console.log('Setting up new mxf web in ' + projectPath);
  generateProject(projectPath, newProjectName, options);
}

/**
 * Generates a new React Native project based on the template.
 * @param Absolute path at which the project folder should be created.
 * @param options Command line arguments parsed by minimist.
 */
function generateProject(destinationRoot, newProjectName, options) {
  var reactNativePackageJson = require('../../package.json');
  var { peerDependencies } = reactNativePackageJson;

  const yarnVersion =
    (!options.npm) &&
    yarn.getYarnVersionIfAvailable() &&
    yarn.isGlobalCliUsingYarn(destinationRoot);

  createProjectFromTemplate(destinationRoot, newProjectName, options.template, yarnVersion);

  console.log('Installing React...');
  // console.log('Installing React...' + options);
  execSync('npm install', {stdio: 'inherit', cwd: destinationRoot});
  /*if (yarnVersion) {
    console.log('Adding React...');
    execSync(`yarn add react@${reactVersion}`, {stdio: 'inherit'});
  } else {
    console.log('Installing React...');
    execSync(`npm install react@${reactVersion} --save --save-exact`, {stdio: 'inherit'});
  }
  if (!options['skip-jest']) {
    const jestDeps = (
      `jest babel-jest babel-preset-react-native react-test-renderer@${reactVersion}`
    );
    if (yarnVersion) {
      console.log('Adding Jest...');
      execSync(`yarn add ${jestDeps} --dev --exact`, {stdio: 'inherit'});
    } else {
      console.log('Installing Jest...');
      execSync(`npm install ${jestDeps} --save-dev --save-exact`, {stdio: 'inherit'});
    }
    addJestToPackageJson(destinationRoot);*/
  // }
  // printRunInstructions(destinationRoot, newProjectName);
}

/**
 * Add Jest-related stuff to package.json, which was created by the react-native-cli.
 */
// function addJestToPackageJson(destinationRoot) {
//   var packageJSONPath = path.join(destinationRoot, 'package.json');
//   var packageJSON = JSON.parse(fs.readFileSync(packageJSONPath));
//
//   packageJSON.scripts.test = 'jest';
//   packageJSON.jest = {
//     preset: 'react-native'
//   };
//   fs.writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, '\t'));
// }

module.exports = init;
