#!/usr/bin/env node-localstorage

const _ = require('lodash')
const colors = require('colors');

const { request } = require('./scripts/rpc')
const account = require('./scripts/account')
const template = require('./scripts/template')
const call = require('./scripts/call')
let { setRpcConfig, getRpcConfig, getMainAccount } = require('./scripts/storage')

function handlerConfigCmd(cmd, value) {
  switch (cmd) {
    case 'set-url':
      setRpcConfig({
        url: value
      })
      break;
    case 'url':
      let rpcConfig = getRpcConfig()
      console.log(colors.green(rpcConfig.url))
      break;
    default:
      break
  }
  return

}


function handlerAccountCmd(cmd, options) {
  //default cmd
  //init
  //address
  //balance
  //utxo
  //list
  switch (cmd) {
    case 'create':
      let pk = options.key
      account.createAccount(pk)
      break;
    case 'list':
      account.listAccount()
      break;
    case 'balance':
      account.balance()
      break;
    case 'utxo':
      let address = options.address
      account.utxo()
      break;
    case 'use':
      account.use()
      break;
    default:
      break;

  }

}

function handlerRpcCmd(options) {
  console.log(options.args)
  let method = options.method || ''
  let args = (options.args && JSON.parse(options.args)) || []
  console.log(colors.yellow('sending request ' + method + ' to node...'))
  if (!method) {
    throw new Error('method name is empty')
  }
  request(method, args).then(res => {
    console.log(colors.green('success!'))
    if (res) {
      console.log(colors.green(res))
    }

  })
}

function handlerTemplateCmd(cmd, options) {
  let mainAccount = getMainAccount()
  let key = (options.key && options.key.replace('0x', ''))
  let gasLimit = options.gasLimit || 100000000
  let category = options.category || 1

  if (!key || mainAccount.pk) {
    key = mainAccount.pk
  } else {
    console.log(colors.warn('please specify a private key or  use a  main account'))
    return
  }

  switch (cmd) {
    case 'create':
      if (options.deploy) {
        template.createAndDeployTemplate(options.source, options.name, category, gasLimit, key, options.args)
      } else {
        template.createTemplate(options.source, options.name, category, gasLimit, key)
      }

      break;
    case 'list':
      template.listTemplate(category)
      break;
    case 'deploy':
      template.deployTemplate(options.id, options.args, gasLimit, key)
    default:
      break
  }
}

function handlerCallCmd(options) {
  let mainAccount = getMainAccount()
  let gasLimit = options.gasLimit || 100000000
  let args = (options.args && JSON.parse(options.args)) || []
  let key = (options.key && options.key.replace('0x', ''))
  if (!key || mainAccount.pk) {
    key = mainAccount.pk
  } else {
    console.log(colors.warn('please specify a private key or  use a  main account'))
    return
  }
  call.call(options.contractAddress, options.method, args, gasLimit, key)

}


module.exports = {
  handlerConfigCmd: handlerConfigCmd,
  handlerAccountCmd: handlerAccountCmd,
  handlerRpcCmd: handlerRpcCmd,
  handlerCallCmd: handlerCallCmd,
  handlerTemplateCmd: handlerTemplateCmd
}
