#!/usr/bin/env node

/**
 * Module dependencies.
 */


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

program
  .version('0.0.1')
  .name('asi')
  .usage('<cmd> [options]')



program
  .command('config <cmd> [value]')
  .usage('<cmd> [value] [options]')
  .description('set and get configurations')
  .action((cmd, value) => {
    handlerConfigCmd(cmd, value)
  }).on('--help', () => {
    console.log('Commands:')
    console.log('  set-url', '    set rpc base url (e.g. https://test-rpc.asimov.network)')
    console.log('  url', '        get rpc base url')
  })

program
  //account command
  .command('account <cmd>')
  .usage('<cmd> [options]')
  .description('manage accounts on the configured environment')
  .option('-k,--key <key>', 'private key to import (e.g. 0x3cb581f720c265739ef4ab6aaf86264268dcc67648f8ac96119e3a255f565e12)')
  .option('-a,--address <address>', 'address to delete (e.g. 0x660dd70daa3f1eadd175c852b29e9c28fdf07bdc7b)')
  .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 account to use as default')
    console.log('  del', '       delete a given address')
  })

//rpc command
program
  .command('rpc')
  .description('interact with asimov blockchain via JSON-RPC')
  .option('-m,--method <method>', 'rpc method name (e.g. asimov_getBlock)')
  .option('-a,--args <args>', 'rpc method arguments, in json format which is surrounded by \'[]\' (e.g. \'["76c0decd5ed5dadac5f79cc4614e220b8b95c0a1ac402212ebaae6336bf1a4af",true,true]\')')
  .action((options) => {
    handlerRpcCmd(options)
  })

program
  .command('template <cmd>')
  .usage('<cmd> [options]')
  .description('manage templates on asimov blockchain')
  .option('-s,--source <source>', '.sol source file (e.g. ~/dapp-contracts/dapp-bin/testnet-tutorial/tutorial.sol)')
  .option('-n,--name <name>', 'template name (e.g. tutorial1)')
  .option('-c,--category <category>', 'template category (if not set, it defaults to 1 which means organization contract)')
  .option('-k,--key <key>', 'private key to import (if not set, it defaults to the account set by "asi account use" command)')
  //.option('-g,--gas-limit <gasLimit>', 'gas limit (if not set, it defaults to 100000000)')
  .option('-a,--args <args>', 'constructor arguments, in json format which is surrounded by \'[]\' (e.g. \'["dao",["0x66afbb7da55614a047652416773454472f92a3cd6a"]]\')')
  .option('-i,--id <id>', 'template id, which is generated when a template is uploaded to asimov blockchain')
  .option('-d,--deploy', 'whether to deploy a contract instance directly. if yes, should also provide constructor arguments.')
  .option('-p,--page <page>', 'which page of templates to display, there are 10 templates in each page. if not set, it defaults to the first page.')
  .option('--fee-value <feeValue>', 'transaction fee asset value. if not set, it defaults to 0.')
  .option('--fee-type <feeType>', 'transaction fee asset type. if not set, it defaults to Asim.')
  .action((cmd, options) => {
    //create
    //list
    handlerTemplateCmd(cmd, options)
  }).on('--help', () => {
    console.log('Commands:')
    console.log('  create', '    create a template (and deploy a contract instance directly if -d is set)')
    console.log('  list', '      get template list')
    console.log('  deploy', '    deploy a contract instance based on template id')
  })


program
  .command('call')
  .description('interact with contracts on asimov blockchain')
  .option('-c,--contract-address <address>', 'contract address (e.g. 0x631f34e56d497ceafc9e91945d33d262695c3b7f90)')
  .option('-m,--method <method>', 'method name, which is surrounded by \'\' (e.g. \'transfer(address,uint256)\', () is not needed if no arguments required, like \'checkBalance\')')
  .option('-a,--args <args>', 'method arguments, in json format which is surrounded by \'[]\' (e.g. \'["0x66afbb7da55614a047652416773454472f92a3cd6a",10000000]\')')
  .option('-k,--key <key>', 'private key to import (if not set, it defaults to the account set by "asi account use" command)')
  //.option('-g,--gas-limit <gasLimit>', 'gas limit (if not set, it defaults to 100000000)')
  .option('-v,--asset-value <assetValue>', 'value of asset to send (matches to msg.value in contract code)')
  .option('-t,--asset-type <assetType>', 'type of asset to send (matches to msg.assettype in contract code, if not set, it defaults to Asim)')
  .option('--fee-value <feeValue>', 'transaction fee asset value. if not set, it defaults to 0.')
  .option('--fee-type <feeType>', 'transaction fee asset type. if not set, it defaults to Asim.')
  .action((options) => {
    //call
    handlerCallCmd(options)

  })
program
  .command('tx <cmd>')
  .usage('<cmd> [options]')
  .description('send a transaction to asimov blockchain or get transaction detail from node')
  .option('-a,--address <address>', 'destination address (e.g. 0x631f34e56d497ceafc9e91945d33d262695c3b7f90)')
  //  .option('-g,--gas-limit <gasLimit>', 'gas limit (if not set, it defaults to 100000000)')
  .option('-v,--asset-value <assetValue>', 'value of asset to send (matches to msg.value in contract code)')
  .option('-t,--asset-type <assetType>', 'type of asset to send (matches to msg.assettype in contract code, if not set, it defaults to Asim)')
  .option('-i,--id <txid>', 'transaction id, used when fetching detail of a transaction')
  //.option('-c,--call-type <callType>', '(optional) call type when the transaction invokes a contract, can be "template", "create" and "call"')
  //.option('-d,--data <date>', '(optional) call data when the transaction invokes a contract')
  .option('--fee-value <feeValue>', 'transaction fee asset value. if not set, it defaults to 0.')
  .option('--fee-type <feeType>', 'transaction fee asset type. if not set, it defaults to Asim.')
  .action((cmd, options) => {
    //tx
    handlerTxCmd(cmd, options)

  }).on('--help', () => {
    console.log('Commands:')
    console.log('  send', '        send a transaction to asimov blockchain)')
    console.log('  detail', '      fetch detail of a transaction')

  })

program
  .command('vote')
  .description('vote to a contract on asimov blockchain')
  .option('-m,--method <method>', 'method name, which is surrounded by \'\' (e.g. \'vote(uint256)\', () is not needed if no arguments required, like \'check\')')
  .option('-c,--contract-address <address>', 'contract address (e.g. 0x631f34e56d497ceafc9e91945d33d262695c3b7f90)')
  .option('-k,--key <key>', 'private key to import (if not set, it defaults to the account set by "asi account use" command)')
  .option('-v,--vote-value <voteValue>', 'value of asset to vote (matches to flow.voteValue in contract code. set 0 to vote all.)')
  .option('-t,--vote-type <voteType>', 'type of asset to vote (matches to msg.assettype in contract code, if not set, it defaults to Asim)')
  .option('-a,--args <args>', 'method arguments, in json format which is surrounded by \'[]\' (e.g. \'["0x66afbb7da55614a047652416773454472f92a3cd6a",10000000]\')')
  .option('--fee-value <feeValue>', 'transaction fee asset value. if not set, it defaults to 0.')
  .option('--fee-type <feeType>', 'transaction fee asset type. if not set, it defaults to Asim.')
  .action((options) => {
    //call
    handlerVoteCmd(options)

  })


program.parse(process.argv);

//contract command
