const { join } = require('path');
const mkdirp = require('mkdirp');
const glob = require('glob');
const BasicGenerator = require('../../BasicGenerator');
const { validateFolder } = require('../../utils')

class FiresoonGenerator extends BasicGenerator {
    prompting() {
      const prompts = [
        {
          type: 'input',
          name: 'appName',
          message: '请输入小程序名称 (quickstart):',
          default: 'quickstart',
          validate: input => validateFolder(input)
        },
        {
          type: 'input',
          name: 'description',
          message: '请输入项目描述 (description):',
          default: 'description'
        },
        {
          type: 'input',
          name: 'version',
          message: '请输入项目版本 (1.0.0):',
          default: '1.0.0'
        },
        {
          type: 'confirm',
          name: 'weapp',
          message: '是否同步微信平台开发？',
        },
        {
          type: 'confirm',
          name: 'alipay',
          message: '是否同步支付宝平台开发？',
        },
        {
          type: 'confirm',
          name: 'qywx',
          message: '是否同步企业微信平台开发？',
        },
        {
          type: 'confirm',
          name: 'dd',
          message: '是否同步钉钉平台开发？',
        },
      ];
      return this.prompt(prompts).then(props => {
          this.prompts = props;
      });
    }

    writing() {

      const { weapp, alipay, qywx, dd, appName, description, version } = this.prompts;
      mkdirp.sync(`${appName}`);
      this.destinationRoot(`${appName}`);
      this.initGit(this.destinationRoot())
      
      let _scripts = {}
      // 微信小程序
      if (weapp) {
        _scripts = Object.assign(_scripts, {
          "build:weapp": "taro build --type weapp",
          "dev:weapp": "npm run build:weapp -- --watch",
        })
        let proConfig = this.fs.readJSON(this.templatePath('./project.config-tmpl.json'), {});
        proConfig.projectname = `@firesoon/${appName}`;
        proConfig.description = description;
        this.fs.writeJSON(this.destinationPath('project.config.json'), proConfig);
      }
      // 支付宝小程序
      if (alipay) {
        _scripts = Object.assign(_scripts, {
          "build:alipay": "taro build --type alipay",
          "dev:alipay": "npm run build:alipay -- --watch",
        })
        let proAlipay = this.fs.readJSON(this.templatePath('./project.alipay-tmpl.json'), {});
        proAlipay.projectname = `@firesoon/${appName}`;
        proAlipay.description = description;
        this.fs.writeJSON(this.destinationPath('project.alipay.json'), proAlipay);
      }
      // 企业微信小程序
      if (qywx) {
        _scripts = Object.assign(_scripts, {
          "build:qywx": "taro build --type qywx",
          "dev:qywx": "npm run build:qywx -- --watch",
        })
        let proAlipay = this.fs.readJSON(this.templatePath('./project.alipay-tmpl.json'), {});
        proAlipay.projectname = `@firesoon/${appName}`;
        proAlipay.description = description;
        this.fs.writeJSON(this.destinationPath('project.alipay.json'), proAlipay);
      }
      // 钉钉小程序
      if (dd) {
        _scripts = Object.assign(_scripts, {
          "build:dd": "taro build --type dd",
          "dev:dd": "npm run build:dd -- --watch",
        })
        let proAlipay = this.fs.readJSON(this.templatePath('./project.alipay-tmpl.json'), {});
        proAlipay.projectname = `@firesoon/${appName}`;
        proAlipay.description = description;
        this.fs.writeJSON(this.destinationPath('project.alipay.json'), proAlipay);
      }

      let pkg = this.fs.readJSON(this.templatePath('./package-tmpl.json'), {});
      let { scripts } = pkg;
      pkg.name = `${appName}`;
      pkg.description = description;
      pkg.version = version;
      pkg = {
        ...pkg,
        scripts: {...scripts, ..._scripts}
      };
      this.fs.writeJSON(this.destinationPath('package.json'), pkg);

      this.copyFiles({
        context: this.prompts,
      }, join(this.templatePath(), './dva-pro'));
    }
}

module.exports = FiresoonGenerator;
