#!/usr/bin/env node

/**
 * Module dependencies.
 */


const program = require('commander');
//const program = new commander.Command();
const {
  handlerConfigCmd,
  handlerAccountCmd,
  handlerRpcCmd,
  handlerDeployCmd,
  handlerCallCmd,
  handlerTemplateCmd
} = require('./handler.js')

program.version('0.0.1')



program
  .command('config <cmd> [value]')
  .description('set and get configurations')
  .action((cmd, value) => {
    handlerConfigCmd(cmd, value)
  }).on('--help', () => {
    console.log('Commands:')
    console.log('  set-url', '    set rpc base url')
    console.log('  url', '        get rpc base url')
  })

program
  //account command
  .command('account [cmd]')
  .description('manage accounts on the configured environment')
  .option('-k,--key <key>', 'private key to import')
  .action((cmd, options) => {

    handlerAccountCmd(cmd, options)

  }).on('--help', () => {
    console.log('Commands:')
    console.log('  create', '    create an account')
    console.log('  list', '      get account list')
    console.log('  balance', '   get total balance of all accounts')
    console.log('  utxo', '      get total utxos of all accounts')
    console.log('  use', '       set default account to use')
  })

//rpc command
program
  .command('rpc')
  .description('interact with asimov blockchain via JSON-RPC')
  .option('-m,--method <method>', 'rpc method name')
  .option('-a,--args <args>', 'rpc method arguments')
  .action((options) => {

    handlerRpcCmd(options)

  })

program
  .command('template [cmd]')
  .description('manage templates on asimov blockchain')
  .option('-s,--source <source>', '.sol source file')
  .option('-n,--name <name>', 'template name')
  .option('-c,--category <category>', 'template category')
  .option('-k,--key <key>', 'private key to import')
  .option('-g,--gas-limit <gasLimit>', 'gas limit')
  .option('-a,--args <args>', 'constructor arguments')
  .option('-i,--id <id>', 'template id')
  .option('-d,--deploy', '')
  .action((cmd, options) => {
    //create
    //list
    handlerTemplateCmd(cmd, options)
  }).on('--help', () => {
    console.log('Commands:')
    console.log('  create', '    create a template')
    console.log('  list', '      get template list')
  })

// program
//   .command('deploy')
//   .description('deploy a contract instance via template on asimov chain')

//   .action((options) => {
//     //deploy
//     handlerDeployCmd(options)
//   })


program
  .command('call')
  .description('interact with contracts on asimov blockchain')
  .option('-c,--contract-address <address>', 'contract address')
  .option('-m,--method <method>', 'method name')
  .option('-a,--args <args>', 'method arguments')
  .option('-k,--key <key>', 'private key to import')
  .option('-g,--gas-limit <gasLimit>', 'gas limit')
  .action((options) => {
    //call
    handlerCallCmd(options)

  })


program.parse(process.argv);

//contract command
