
import { Mustr } from './index';
import { resolve } from 'path';
import { Pargv, Chalk } from 'pargv';

let mustrConfig;
const pargv = new Pargv();
const chalk = new Chalk();

const mustrPath = resolve(process.cwd(), 'mustr.json');

// Try to load the mustr.json config if exists.
try {
  mustrConfig = require(mustrPath);
}
catch (ex) {
  mustrConfig = {};
}

// Create Mustr instance.
const mu = new Mustr(mustrConfig);

// Just add some spacing.
console.log();

// Handler for help.
function help() {
  const padBtm = [0, 0, 1, 0];

  const msg =
    '- See additional required/optional arguments for each command below. -\n';

  pargv
    .logo('Mustr', 'cyan')
    .ui(95)
    // .join(chalk.magenta('Usage:'), 'mu', chalk.cyan('<cmd>'), '<type> [options]\n')
    .join(chalk.magenta('Usage:'), 'mu', chalk.cyan('<cmd>'), '\n')
    .div({ text: chalk.bgBlue.white(msg) })
    .div(
    { text: chalk.cyan('help, h'), width: 35, padding: padBtm },
    { text: chalk.gray('displays help and usage information.'), width: 40, padding: padBtm }
    )
    .div(
    { text: chalk.cyan('init, i'), width: 35, padding: padBtm },
    { text: chalk.gray('initialize the application for use with Mustr.'), width: 40, padding: padBtm }
    )
    .div(
    { text: `${chalk.cyan('generate, g')} ${chalk.white('<template>')} ${chalk.magenta('<name>')}`, width: 35, padding: padBtm },
    { text: chalk.gray('generates and renders a template.'), width: 40, padding: padBtm }
    )
    .div(
    { text: chalk.white('<template>'), width: 35, padding: [0, 2, 0, 2] },
    { text: chalk.gray('template to generate and compile.'), width: 40 },
    { text: chalk.red('[required]'), align: 'right' }
    )
    .div(
    { text: chalk.white('<name>'), width: 35, padding: [0, 2, 0, 2] },
    { text: chalk.gray('output name for template'), width: 40 },
    { text: chalk.red('[required]'), align: 'right' }
    )
    // .div(
    // { text: chalk.white('--data, -d'), width: 35, padding: [0, 2, 0, 2] },
    // { text: chalk.gray('inline data for compile.'), width: 40 },
    // { text: chalk.gray('[optional]'), align: 'right' }
    // )
    .show();
}

// Handler for initializing.
function init() {
  mu.init(pargv.f);
}

// Handler for generating templates.
function generate() {

  if (!mustrConfig || !mustrConfig.configDir)
    mu.log.warn('invalid or missing Mustr config, do you need to call mu init?').exit();

  // Ensure config/templates loaded.
  mu.load();

  const name = pargv.getCmd(0);
  const output = pargv.getCmd(1);
  const options = pargv.getFlags();

  if (!name || !output)
    mu.log.error('cannot generate template using name or output of undefined.').exit();

  // Generate the template.
  mu.render(name, output, options);

}

// Bind CLI methods.
pargv
  .action('help', 'h', help)
  .action('init', 'i', init)
  .action('generate', 'g', generate)
  .action('*', () => {
    mu.log
      .warn('no command selected, did you mean to call "mu help"?')
      .write()
      .exit();
  })
  .parse();




