#!/usr/bin/env node
import chalk from 'chalk'
import program from 'commander'
import { CliFs } from './lib/cli'
import { CreateComponentFs } from './lib/create-component-fs'
import { CreatePageFs } from './lib/create-page-fs'
import { CreateProjectFs } from './lib/create-project-fs'
import { DeleteComponentFs } from './lib/delete-component-fs'
import { DeletePageFs } from './lib/delete-page-fs'
import { version } from './package.json'
import { log } from './ulits/log'

program.version(version, '-v, --version')

program.command('create')
  .description('创建')
  .action(async (cmd) => {
    switch (cmd) {
      case 'project':
        CreateProjectFs.init()
        break
      case 'page':
        await CreatePageFs.init()
        break
      case 'component':
        await CreateComponentFs.init()
        break
      default:
        log.table([
          ['create <project>', '新建小程序项目'],
          ['create <page>', '新建小程序页面'],
          ['create <component>', '新建小程序组件']
        ])
        break
    }
  })

program.command('delete')
  .description('删除')
  .action(async (cmd) => {
    switch (cmd) {
      case 'page':
        await DeletePageFs.init()
        break
      case 'component':
        await DeleteComponentFs.init()
        break
      default:
        log.table([
          ['delete <page>', '删除小程序页面'],
          ['delete <component>', '删除小程序组件']
        ])
        break
    }
  })

program.command('open')
  .description('打开')
  .action((cmd) => {
    if (typeof cmd === 'object') {
      log.table([
        ['open <this>', '打开当前小程序，如果目录配置不正确则默认打开开发工具'],
        ['open <tool>', '打开开发工具'],
        ['open <projectPath>', '打开指定路径小程序项目，如果目录配置不正确则默认打开开发工具'],
        ['了解更多', 'https://developers.weixin.qq.com/miniprogram/dev/devtools/cli.html']
      ])
    } else {
      switch (cmd) {
        case 'this':
          CliFs.openThis()
          break
        case 'tool':
          CliFs.openTool()
          break
        default:
          CliFs.openThis(cmd)
          break
      }
    }
  })

program.command('upload')
  .description('上传项目')
  .action((cmd) => CliFs.upload(cmd))

program.command('login')
  .description('登录')
  .action(() => CliFs.login())

program.command('preview')
  .description('预览')
  .action((cmd) => CliFs.preview(cmd))

program.command('auto')
  .description('自动预览')
  .action((cmd) => CliFs.auto(cmd))

program.command('close')
  .description('关闭当前项目')
  .action((cmd) => CliFs.close(cmd))

program.command('quit')
  .description('退出开发工具')
  .action(() => CliFs.quit())

program.command('config <cmd> [params]')
  .description('工具设置')
  .action((cmd, params) => CliFs.config(cmd, params))

program.on('command:*', (cmd) => {
  const command: string[] = ['create', 'delete', 'open', 'upload', 'login', 'preview', 'auto', 'close', 'quit']
  const state: number = command.findIndex(v => v === cmd[0])
  if (state === -1) {
    log.error(`输入的命令无效，请输入${chalk.green(' wxapp-cli -h ')}查看帮助`, false)
  }
})

program.parse(process.argv)

// 判断用户是否输入了执行字段
if (process.argv.length < 3) {
  log.error(`输入${chalk.green(' wxapp-cli -h ')}查看帮助`, false)
}


