'use strict'
var yo = require('yo-yo')

module.exports = {
  /**
    * deploy the given contract
    *
    * @param {String} data    - data to send with the transaction ( return of txFormat.buildData(...) ).
    * @param {Object} udap    - udapp.
    * @param {Function} callback    - callback.
    */
  createContract: function (data, udapp, callback) {
    udapp.runTx({data: data, useCall: false}, (error, txResult) => {
      // see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
      callback(error, txResult)
    })
  },

  /**
    * call the current given contract
    *
    * @param {String} to    - address of the contract to call.
    * @param {String} data    - data to send with the transaction ( return of txFormat.buildData(...) ).
    * @param {Object} funAbi    - abi definition of the function to call.
    * @param {Object} udap    - udapp.
    * @param {Function} callback    - callback.
    */
  callFunction: function (to, data, funAbi, udapp, callback) {
    udapp.runTx({to: to, data: data, useCall: funAbi.constant}, (error, txResult) => {
      // see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
      callback(error, txResult)
    })
  },

  /**
    * check if the vm has errored
    *
    * @param {Object} txResult    - the value returned by the vm
    * @return {Object} -  { error: true/false, message: DOMNode }
    */
  checkVMError: function (txResult) {
    var ret = {
      error: false,
      message: ''
    }
    if (!txResult.result.vm.exceptionError) {
      return ret
    }
    var error = yo`<span> VM error: ${txResult.result.vm.exceptionError}</span>`
    var msg
    if (txResult.result.vm.exceptionError === 'invalid opcode') {
      msg = yo`<ul><li>The constructor should be payable if you send it value.</li>
                  <li>The execution might have thrown.</li></ul>`
      ret.error = true
    } else if (txResult.result.vm.exceptionError === 'out of gas') {
      msg = yo`<div>The transaction ran out of gas. Please increase the Gas Limit.</div>`
      ret.error = true
    }
    ret.message = yo`<div>${error} ${msg} Debug the transaction to get more information</div>`
    return ret
  }
}

@@ -9,34 +9,8 @@
 ict'
-%0Avar yo = require('yo-yo')
 %0A%0Amo
@@ -1580,18 +1580,9 @@
 r = 
-yo%60%3Cspan%3E 
+%60
 VM e
@@ -1627,15 +1627,11 @@
 ror%7D
-%3C/span%3E
+.%5Cn
 %60%0A  
@@ -1722,19 +1722,11 @@
 g = 
-yo%60%3Cul%3E%3Cli%3E
+%60%5Ct
 The 
@@ -1771,45 +1771,18 @@
 end 
-it 
 value.
-%3C/li%3E%0A                  %3Cli%3E
+%5Cn%5Ct
 The 
@@ -1813,18 +1813,10 @@
 own.
-%3C/li%3E%3C/ul%3E
+%5Cn
 %60%0A  
@@ -1917,24 +1917,19 @@
   msg = 
-yo%60%3Cdiv%3E
+%60%5Ct
 The tran
@@ -1982,22 +1982,18 @@
 s Limit.
-%3C/div%3E
+%5Cn
 %60%0A      
@@ -2037,24 +2037,17 @@
 e = 
-yo%60%3Cdiv%3E
+%60
 $%7Berror%7D
  $%7Bm
@@ -2046,16 +2046,16 @@
 ror%7D
- 
 $%7Bmsg%7D
- 
+%5Ct
 Debu
@@ -2099,14 +2099,9 @@
 tion
-%3C/div%3E
+.
 %60%0A  

return string instead of dom element