import _slicedToArray from 'babel-runtime/helpers/slicedToArray';
import _extends from 'babel-runtime/helpers/extends';
import _Promise from 'babel-runtime/core-js/promise';
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 _ from 'lodash';

import ContractImplementation from '../../ContractImplementation';
import EventListener from '../../../utils/EventListener';
import httpRequest from '../../../utils/httpRequest';

/**
 * Provides interaction with standard Arbitrable contracts
 */

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

  /**
   * Constructor ArbitrableTransaction.
   * @param {object} web3Provider instance
   * @param {string} contractAddress of the contract
   */
  function Arbitrable(web3Provider, contractArtifact, contractAddress) {
    var _this2 = this;

    _classCallCheck(this, Arbitrable);

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

    _this.getMetaEvidence = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var metaEvidenceLog, metaEvidenceUri, metaEvidenceResponse;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return EventListener.getEventLogs(_this, 'MetaEvidence', 0, 'latest', { _metaEvidenceID: 0 });

            case 2:
              metaEvidenceLog = _context.sent;

              if (metaEvidenceLog[0]) {
                _context.next = 5;
                break;
              }

              return _context.abrupt('return', {});

            case 5:
              // NOTE better to throw errors for missing meta-evidence?

              metaEvidenceUri = metaEvidenceLog[0].args._evidence;
              // FIXME caching issue need a query param to fetch from AWS

              _context.next = 8;
              return httpRequest('GET', metaEvidenceUri + '?nocache');

            case 8:
              metaEvidenceResponse = _context.sent;

              if (!(metaEvidenceResponse.status >= 400)) {
                _context.next = 11;
                break;
              }

              throw new Error('Unable to fetch meta-evidence at ' + metaEvidenceUri);

            case 11:
              return _context.abrupt('return', metaEvidenceResponse.body || metaEvidenceResponse);

            case 12:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, _this2);
    }));
    _this.getEvidence = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
      var arbitratorAddress, disputeId, evidenceLogs;
      return _regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return _this.loadContract();

            case 2:
              _context3.next = 4;
              return _this.contractInstance.arbitrator();

            case 4:
              arbitratorAddress = _context3.sent;
              _context3.next = 7;
              return _this.loadContract();

            case 7:
              _context3.next = 9;
              return _this.contractInstance.disputeID();

            case 9:
              disputeId = _context3.sent.toNumber();

              if (!_.isNull(disputeId)) {
                _context3.next = 12;
                break;
              }

              return _context3.abrupt('return', []);

            case 12:
              _context3.next = 14;
              return EventListener.getEventLogs(_this, 'Evidence', 0, 'latest', { _disputeID: disputeId, _arbitrator: arbitratorAddress });

            case 14:
              evidenceLogs = _context3.sent;
              return _context3.abrupt('return', _Promise.all(evidenceLogs.map(function () {
                var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(evidenceLog) {
                  var evidenceURI, evidence, submittedAt;
                  return _regeneratorRuntime.wrap(function _callee2$(_context2) {
                    while (1) {
                      switch (_context2.prev = _context2.next) {
                        case 0:
                          evidenceURI = evidenceLog.args._evidence;
                          _context2.next = 3;
                          return httpRequest('GET', evidenceURI);

                        case 3:
                          evidence = _context2.sent;
                          _context2.next = 6;
                          return _this._Web3Wrapper.getBlock(evidenceLog.blockNumber);

                        case 6:
                          submittedAt = _context2.sent.timestamp;
                          return _context2.abrupt('return', _extends({}, evidence.body, { submittedBy: evidenceLog.args._party, submittedAt: submittedAt }));

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

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

            case 16:
            case 'end':
              return _context3.stop();
          }
        }
      }, _callee3, _this2);
    }));
    _this.getContractData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
      var _ref5, _ref6, metaEvidence, partyA, partyB;

      return _regeneratorRuntime.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              _context4.next = 2;
              return _this.loadContract();

            case 2:
              _context4.next = 4;
              return _Promise.all([_this.getMetaEvidence(), _this.contractInstance.partyA(), _this.contractInstance.partyB()]);

            case 4:
              _ref5 = _context4.sent;
              _ref6 = _slicedToArray(_ref5, 3);
              metaEvidence = _ref6[0];
              partyA = _ref6[1];
              partyB = _ref6[2];
              return _context4.abrupt('return', {
                partyA: partyA,
                partyB: partyB,
                metaEvidence: metaEvidence
              });

            case 10:
            case 'end':
              return _context4.stop();
          }
        }
      }, _callee4, _this2);
    }));
    return _this;
  }

  /**
   * Get the meta evidence for the contract. Arbitrable Transaction can only have
   * one meta-evidence that is submitted on contract creation. Look up meta-evidence event
   * and make an http request to the resource.
   */


  /**
   * Get the evidence submitted in a dispute.
   */


  /**
   * Fetch all standard contract data.
   */


  return Arbitrable;
}(ContractImplementation);

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