import _Object$assign from 'babel-runtime/core-js/object/assign';
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';
import AbstractContract from '../AbstractContract';

/**
 * Arbitrable Abstract Contarct API. This wraps an arbitrable contract. It provides
 * interaction with both the off chain store as well as the arbitrable instance. All
 * arbitrable methods from the supplied contract implementation can be called from this
 * object.
 */

var ArbitrableContract = function (_AbstractContract) {
  _inherits(ArbitrableContract, _AbstractContract);

  function ArbitrableContract() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, ArbitrableContract);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ArbitrableContract.__proto__ || _Object$getPrototypeOf(ArbitrableContract)).call.apply(_ref, [this].concat(args))), _this), _initialiseProps.call(_this), _temp), _possibleConstructorReturn(_this, _ret);
  }
  /**
   * Deploy a contract and add to the Store.
   * @param {string} account - Ethereum address.
   * @param {int} value - funds to be placed in contract.
   * @param {string} hashContract - Keccak hash of the plain English contract.
   * @param {string} arbitratorAddress - The address of the arbitrator contract.
   * @param {int} timeout - Time after which a party automatically loose a dispute.
   * @param {string} partyB - Ethereum address of the other party in the contract.
   * @param {bytes} arbitratorExtraData - Extra data for the arbitrator.
   * @param {string} email - Email address of the contract creator (default empty string).
   * @param {string} title - Title of the contract (default empty string).
   * @param {string} description - Description of what the contract is about (default empty string).
   * @param {...any} args - Extra arguments for the contract.
   * @returns {object | Error} - The contract object or an error.
   */


  /**
   * Submit evidence.
   * @param {string} account - ETH address of user.
   * @param {string} name - Name of evidence.
   * @param {string} description - Description of evidence.
   * @param {string} url - A link to an evidence using its URI.
   * @returns {string} - txHash Hash transaction.
   */


  /**
   * Get all contracts TODO do we need to get contract data from blockchain?
   * @param {string} account - Address of user.
   * @returns {object[]} - Contract data from store.
   */


  /**
   * Get evidence for contract.
   * @param {string} contractAddress - Address of arbitrable contract.
   * @returns {object[]} - Array of evidence objects.
   */


  /**
   * Fetch all data from the store on the current contract.
   * @returns {object} - Store data for contract.
   */


  /**
   * Get data from the store and contract for Arbitrable Contract.
   * @param {string} account - ETH address of user.
   * @returns {object} - Contract data.
   */


  return ArbitrableContract;
}(AbstractContract);

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

  this.deploy = function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(account, value, hashContract, arbitratorAddress, timeout, partyB) {
      for (var _len2 = arguments.length, args = Array(_len2 > 10 ? _len2 - 10 : 0), _key2 = 10; _key2 < _len2; _key2++) {
        args[_key2 - 10] = arguments[_key2];
      }

      var arbitratorExtraData = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : '';
      var email = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : '';

      var _contractImplementati;

      var title = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : '';
      var description = arguments.length > 9 && arguments[9] !== undefined ? arguments[9] : '';
      var contractInstance, newContract;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return (_contractImplementati = _this2._contractImplementation.constructor).deploy.apply(_contractImplementati, [account, value, hashContract, arbitratorAddress, timeout, partyB, arbitratorExtraData, _this2._contractImplementation.getWeb3Provider()].concat(args));

            case 2:
              contractInstance = _context.sent;
              _context.next = 5;
              return _this2._StoreProvider.updateContract(account, contractInstance.address, {
                hashContract: hashContract,
                partyA: account,
                partyB: partyB,
                arbitrator: arbitratorAddress,
                timeout: timeout,
                email: email,
                title: title,
                description: description
              });

            case 5:
              newContract = _context.sent;
              return _context.abrupt('return', newContract);

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

    return function (_x, _x2, _x3, _x4, _x5, _x6) {
      return _ref2.apply(this, arguments);
    };
  }();

  this.submitEvidence = function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(account, name) {
      var description = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
      var url = arguments[3];
      var txHash;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return _this2._contractImplementation.submitEvidence(account, name, description, url);

            case 2:
              txHash = _context2.sent;
              _context2.next = 5;
              return _this2._StoreProvider.addEvidenceContract(_this2._contractImplementation.contractAddress, account, name, description, url);

            case 5:
              return _context2.abrupt('return', txHash);

            case 6:
            case 'end':
              return _context2.stop();
          }
        }
      }, _callee2, _this2);
    }));

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

  this.getContractsForUser = function () {
    var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(account) {
      var userProfile;
      return _regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return _this2._StoreProvider.setUpUserProfile(account);

            case 2:
              userProfile = _context3.sent;
              return _context3.abrupt('return', userProfile.contracts);

            case 4:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, _this2);
    }));

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

  this.getEvidenceForArbitrableContract = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
    var arbitrableContractData, partyAContractData, partyBContractData, partyAEvidence, partyBEvidence;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            _context4.next = 2;
            return _this2._contractImplementation.getData();

          case 2:
            arbitrableContractData = _context4.sent;
            _context4.next = 5;
            return _this2._StoreProvider.getContractByAddress(arbitrableContractData.partyA, _this2._contractImplementation.contractAddress);

          case 5:
            partyAContractData = _context4.sent;
            _context4.next = 8;
            return _this2._StoreProvider.getContractByAddress(arbitrableContractData.partyB, _this2._contractImplementation.contractAddress);

          case 8:
            partyBContractData = _context4.sent;
            partyAEvidence = (partyAContractData ? partyAContractData.evidences : []).map(function (evidence) {
              evidence.submitter = arbitrableContractData.partyA;
              return evidence;
            });
            partyBEvidence = (partyBContractData ? partyBContractData.evidences : []).map(function (evidence) {
              evidence.submitter = arbitrableContractData.partyB;
              return evidence;
            });
            return _context4.abrupt('return', partyAEvidence.concat(partyBEvidence));

          case 12:
          case 'end':
            return _context4.stop();
        }
      }
    }, _callee4, _this2);
  }));
  this.getDataFromStore = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
    var contractInstance, partyA;
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            _context5.next = 2;
            return _this2._contractImplementation.loadContract();

          case 2:
            contractInstance = _context5.sent;
            _context5.next = 5;
            return contractInstance.partyA();

          case 5:
            partyA = _context5.sent;
            return _context5.abrupt('return', _this2._StoreProvider.getContractByAddress(partyA, _this2._contractImplementation.contractAddress));

          case 7:
          case 'end':
            return _context5.stop();
        }
      }
    }, _callee5, _this2);
  }));

  this.getData = function () {
    var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(account) {
      var contractData, storeData;
      return _regeneratorRuntime.wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              _context6.next = 2;
              return _this2._contractImplementation.getData();

            case 2:
              contractData = _context6.sent;
              storeData = {};

              if (!account) {
                _context6.next = 8;
                break;
              }

              _context6.next = 7;
              return _this2._StoreProvider.getContractByAddress(account, _this2._contractImplementation.contractAddress);

            case 7:
              storeData = _context6.sent;

            case 8:
              return _context6.abrupt('return', _Object$assign({}, storeData, contractData));

            case 9:
            case 'end':
              return _context6.stop();
          }
        }
      }, _callee6, _this2);
    }));

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

export default ArbitrableContract;
//# sourceMappingURL=Arbitrable.js.map