#!/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')
const tx = require('./scripts/tx')
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':
      account.utxo()
      break;
    case 'use':
      account.use()
      break;
    case 'del':
      account.del(options.address)
      break;
    default:
      break;

  }

}

function handlerRpcCmd(options) {
  let method = options.method || ''
  let args = []
  try {
    args = (options.args && JSON.parse(options.args))
  } catch (e) {
    args.push(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) {
      try {
        let r = JSON.stringify(res)
        console.log(colors.green(r))
      } catch (e) {
        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
  let page = options.page || 0
  let args = (options.args && JSON.parse(options.args)) || []
  if (!key || mainAccount.pk) {
    key = mainAccount.pk
  } else {
    console.log(colors.warn('please specify a private key or set a default account'))
    return
  }

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

      promise.then(res => {
        let text = ""
        try {
          text = JSON.stringify(res)
        } catch (e) {
          text = res
        }
        console.log(colors.green("tx hash: " + text))
        console.log(colors.yellow("confirming transaction, please wait ..."))
      })

      break;
    case 'list':
      template.listTemplate(category, page)
      break;
    case 'deploy':
      template.deployTemplate(options.id, args, gasLimit, key).then(res => {
        let text = ""
        try {
          text = JSON.stringify(res)
        } catch (e) {
          text = res
        }

        console.log(colors.green("tx hash: " + text))
        console.log(colors.yellow("confirming transaction, please wait ..."))
      })
    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', ''))
  let amount = options.assetValue
  let asset = options.assetType || '000000000000000000000000'
  if (!key || mainAccount.pk) {
    key = mainAccount.pk
  } else {
    console.log(colors.warn('please specify a private key or set a default account'))
    return
  }
  call.call(options.contractAddress, options.method, args, gasLimit, amount, asset, key)

}

function handlerTxCmd(cmd, options) {
  let mainAccount = getMainAccount()
  let gasLimit = options.gasLimit || 100000000
  let data = options.data || ''
  let address = options.address || ''

  let amount = options.assetValue
  let asset = options.assetType || '000000000000000000000000'
  let callType = options.callType

  switch (cmd) {
    case 'send':
      tx.send(options.address, amount, asset, data, callType, gasLimit).then(res => {
        console.log(colors.green('success!'))
        console.log(colors.green("tx hash: " + res))
        console.log(colors.yellow("confirming transaction, please wait ..."))
      }).catch(e => {
        console.log(colors.red(e))
      })
      break;
    case 'detail':
      //8b0dcfd88f6e89866d05c6fef435545365a3574c87f86e5f3878f9123cb260a1
      tx.detail(options.id)
      break;
    default:
      break;
  }

}



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