import ora from 'ora'
import path from 'path'
import chalk from 'chalk'
import inquirer from 'inquirer'
import { sleep } from '../../utils/other'
import { downloadRepo } from '../../utils/git'
import { npm } from '../../utils/npm'
import { TEMP_PATH } from '../../constant/cli'
import {
  DOC_ROOT_PATH,
  PROJECT_DOC_ROOT_DIR,
  STORYBOOK_CONFIG_PATH,
  PROJECT_DOC_CONFIG_PATH,
  DOC_EXAMPLES_DIR,
  STORYBOOK_SRC_PATH,
  DOC_ASSETS_DIR,
} from '../../constant/doc'
const fsEx = require('fs-extra')

const docGit = {
  name: '文档上传模板', // 命令行选项名称
  git: 'https://gitee.com/njsyman/ark-doc.git',
  branch: 'master', // git分支
  script: 'npm run storybook', // 项目启动脚本
}

async function generateDoc(options: any) {
  const spinner = ora('获取文档模板信息').start()
  spinner.text = ''

  // 如果用户清除缓存
  if (options.clearCache) {
    fsEx.removeSync(DOC_ROOT_PATH)
  }

  // 判断脚手架里是否存在文档模板
  const isExistsDoc = fsEx.pathExistsSync(DOC_ROOT_PATH)
  if (!isExistsDoc) {
    // 下载模板到脚手架的temp目录
    spinner.text = '正在下载模板，请稍等...'
    await sleep()

    const downStatus = downloadRepo(docGit, {
      cwd: TEMP_PATH,
      stdio: 'ignore', // 屏蔽输出内容
    })
    if (!downStatus) {
      spinner.fail(chalk.red('模板下载失败'))
      return
    }
    spinner.succeed(chalk.green('模板下载成功'))

    // 去掉模板里的git，并安装依赖
    spinner.start('开始安装所需依赖，请稍等...')
    console.log('')
    const gitPath = path.join(DOC_ROOT_PATH, '.git')
    fsEx.removeSync(gitPath)
    const { status: installStatus } = (await npm(['install'], {
      cwd: DOC_ROOT_PATH,
    })) as any

    if (installStatus !== 0) {
      spinner.fail(chalk.red('依赖安装失败'))
      return
    }

    spinner.succeed(chalk.green('依赖安装成功'))
  }

  // 检查项目里是否已存在.arkdoc目录
  const destPath = path.join(process.cwd(), PROJECT_DOC_ROOT_DIR)
  let isCover = true
  if (fsEx.pathExistsSync(destPath)) {
    spinner.stop()
    const res = await inquirer.prompt({
      type: 'confirm',
      name: 'isCover',
      message: '检测到项目中已存在.arkdoc目录，请确认是否覆盖',
    })
    isCover = res.isCover
  }

  // 选择不覆盖时。脚手架的文档模板其实已经被更新了。所以可以通过这个方式更新文档模板
  if (!isCover) {
    return
  }

  // 将文档.arkdoc文件复制到项目组的命令执行目录
  const srcPath = path.join(DOC_ROOT_PATH, PROJECT_DOC_ROOT_DIR)
  fsEx.copySync(srcPath, destPath)
  // 复制preview.js,preview-head.html,preview-body.html,和example、assets文件夹
  fsEx.copySync(
    path.join(STORYBOOK_CONFIG_PATH, './preview.js'),
    path.join(PROJECT_DOC_CONFIG_PATH, './preview.js')
  )
  fsEx.copySync(
    path.join(STORYBOOK_CONFIG_PATH, './preview-head.html'),
    path.join(PROJECT_DOC_CONFIG_PATH, './preview-head.html')
  )
  fsEx.copySync(
    path.join(STORYBOOK_CONFIG_PATH, './preview-body.html'),
    path.join(PROJECT_DOC_CONFIG_PATH, './preview-body.html')
  )
  fsEx.copySync(
    path.join(STORYBOOK_SRC_PATH, `./${DOC_EXAMPLES_DIR}`),
    path.join(PROJECT_DOC_ROOT_DIR, `./${DOC_EXAMPLES_DIR}`)
  )
  fsEx.copySync(
    path.join(STORYBOOK_SRC_PATH, `./${DOC_ASSETS_DIR}`),
    path.join(PROJECT_DOC_ROOT_DIR, `./${DOC_ASSETS_DIR}`)
  )

  console.log('')
  spinner.succeed(
    chalk.green(
      '已在项目中生成文档文件.arkdoc。执行 ark preview --doc 命令即可预览文档'
    )
  )
}

export default generateDoc
