import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';

var _this2 = this;

import PinakionPOCArtifact from 'kleros-interaction/build/contracts/MiniMeTokenERC20';
import _ from 'lodash';

import * as ethConstants from '../../../constants/eth';
import * as errorConstants from '../../../constants/error';
import ContractImplementation from '../../ContractImplementation';
import deployContractAsync from '../../../utils/deployContractAsync';
import isRequired from '../../../utils/isRequired';

/**
 * Provides interaction with a PinakionPOC contract deployed on the blockchain.
 */

var MiniMePinakion = function (_ContractImplementati) {
  _inherits(MiniMePinakion, _ContractImplementati);

  /**
   * Constructor PinakionPOC.
   * @param {object} web3Provider - web3 instance.
   * @param {string} contractAddress - of the contract (optionnal).
   */
  function MiniMePinakion(web3Provider, contractAddress) {
    _classCallCheck(this, MiniMePinakion);

    var _this = _possibleConstructorReturn(this, (MiniMePinakion.__proto__ || _Object$getPrototypeOf(MiniMePinakion)).call(this, web3Provider, PinakionPOCArtifact, contractAddress));

    _initialiseProps.call(_this);

    return _this;
  }

  /**
   * Deploy a new instance of PinakionPOC.
   * @param {string} account - account of user
   * @param {object} web3Provider - web3 provider object
   * @param {string} tokenFactoryAddress - The address of the MiniMeTokenFactory contract that will create the Clone token contracts
   * @param {string} parentTokenAddress - Address of the parent token, set to 0x0 if it is a new token
   * @param {number} parentSnapshotBlock - Block of the parent token that will determine the initial distribution of the clone token, set to 0 if it is a new token
   * @param {string} tokenName - Name of the token.
   * @param {number} decimalUnits - Number of decimal units token is divisible by.
   * @param {string} tokenSymbol - Abreviated symbol to represent the token.
   * @param {bool} transfersEnabled - If users can transfer tokens.
   * @returns {object} - 'truffle-contract' Object | err The contract object or error deploy.
   */


  /**
   * Transfer ownership of the PNK contract to the kleros POC contract.
   * @param {string} klerosAddress - Address of Kleros POC contract.
   * @param {string} account - Address of user.
   * @returns {object} - The result transaction object.
   */


  /**
   * Approve the arbitrator contract to transfer PNK to the contract and call the arbitrators
   * receiveApproval()
   * @param {string} arbitratorAddress - The address of the arbitrator contract.
   * @param {number} amount - The amount of PNK to transfer.
   * @param {string} account - The users account.
   * @returns {bool} If the transfer succeeded or not
   */


  return MiniMePinakion;
}(ContractImplementation);

MiniMePinakion.deploy = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var account = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : isRequired('account');
    var web3Provider = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : isRequired('web3Provider');
    var tokenFactoryAddress = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : isRequired('tokenFactoryAddress');
    var parentTokenAddress = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '0x0';
    var parentSnapshotBlock = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
    var tokenName = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 'Pinakion';
    var decimalUnits = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 18;
    var tokenSymbol = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 'PNK';
    var transfersEnabled = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : true;
    var contractDeployed;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return deployContractAsync(account, 0, // value
            PinakionPOCArtifact, web3Provider, tokenFactoryAddress, // args
            parentTokenAddress, parentSnapshotBlock, tokenName, decimalUnits, tokenSymbol, transfersEnabled);

          case 2:
            contractDeployed = _context.sent;
            return _context.abrupt('return', contractDeployed);

          case 4:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, _this2);
  }));

  return function () {
    return _ref.apply(this, arguments);
  };
}();

var _initialiseProps = function _initialiseProps() {
  var _this3 = this;

  this.changeController = function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(klerosAddress) {
      var account = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this3._Web3Wrapper.getAccount(0);
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return _this3.loadContract();

            case 2:
              _context2.prev = 2;
              return _context2.abrupt('return', _this3.contractInstance.changeController(klerosAddress, {
                from: account,
                gas: ethConstants.TRANSACTION.GAS
              }));

            case 6:
              _context2.prev = 6;
              _context2.t0 = _context2['catch'](2);

              console.error(_context2.t0);
              throw new Error(errorConstants.UNABLE_TO_TRANSFER_OWNERSHIP);

            case 10:
            case 'end':
              return _context2.stop();
          }
        }
      }, _callee2, _this3, [[2, 6]]);
    }));

    return function (_x10) {
      return _ref2.apply(this, arguments);
    };
  }();

  this.approveAndCall = function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(arbitratorAddress, amount) {
      var account = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _this3._Web3Wrapper.getAccount(0);
      return _regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return _this3.loadContract();

            case 2:
              return _context3.abrupt('return', _this3.contractInstance.approveAndCall(arbitratorAddress, _this3._Web3Wrapper.toWei(amount, 'ether'), '0x0', {
                from: account,
                gas: ethConstants.TRANSACTION.GAS
              }));

            case 3:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, _this3);
    }));

    return function (_x12, _x13) {
      return _ref3.apply(this, arguments);
    };
  }();
};

export default MiniMePinakion;
//# sourceMappingURL=MiniMePinakion.js.map