pragma solidity ^0.4.24;

/**
 * @title ERC20Basic
 * @dev Simpler version of ERC20 interface
 * See https://github.com/ethereum/EIPs/issues/179
 */
contract ERC20Basic {
  function totalSupply() public view returns (uint256);
  function balanceOf(address _who) public view returns (uint256);
  function transfer(address _to, uint256 _value) public returns (bool);
  event Transfer(address indexed from, address indexed to, uint256 value);
}

/**
 * @title ERC20 interface
 * @dev see https://github.com/ethereum/EIPs/issues/20
 */
contract ERC20 is ERC20Basic {
  function allowance(address _owner, address _spender)
    public view returns (uint256);

  function transferFrom(address _from, address _to, uint256 _value)
    public returns (bool);

  function approve(address _spender, uint256 _value) public returns (bool);
  event Approval(
    address indexed owner,
    address indexed spender,
    uint256 value
  );
}

contract Alice {
  enum DealState {
    Uninitialized,
    Initialized,
    PaymentSentToBob,
    PaymentSentToAlice
  }

  struct Deal {
    bytes20 dealHash;
    DealState state;
  }

  mapping (bytes32 => Deal) public deals;

  constructor() public { }

  function initEthDeal(
    bytes32 _dealId,
    address _bob,
    bytes20 _aliceHash,
    bytes20 _bobHash
  ) external payable {
    require(_bob != 0x0 && msg.value > 0 && deals[_dealId].state == DealState.Uninitialized);
    bytes20 dealHash = ripemd160(abi.encodePacked(
      msg.sender,
      _aliceHash,
      _bob,
      _bobHash,
      msg.value,
      address(0)
    ));
    deals[_dealId] = Deal(
      dealHash,
      DealState.Initialized
    );
  }

  function initErc20Deal(
    bytes32 _dealId,
    uint _amount,
    address _bob,
    bytes20 _aliceHash,
    bytes20 _bobHash,
    address _tokenAddress
  ) external {
    require(_bob != 0x0 && _tokenAddress != 0x0 && _amount > 0 && deals[_dealId].state == DealState.Uninitialized);
    bytes20 dealHash = ripemd160(abi.encodePacked(
      msg.sender,
      _aliceHash,
      _bob,
      _bobHash,
      _amount,
      _tokenAddress
    ));
    deals[_dealId] = Deal(
      dealHash,
      DealState.Initialized
    );
    ERC20 token = ERC20(_tokenAddress);
    assert(token.transferFrom(msg.sender, address(this), _amount));
  }

  function aliceClaimsPayment(
    bytes32 _dealId,
    uint _amount,
    address _tokenAddress,
    address _bob,
    bytes20 _aliceHash,
    bytes _bobSecret
  ) external {
    require(deals[_dealId].state == DealState.Initialized);
    bytes20 dealHash = ripemd160(abi.encodePacked(
      msg.sender,
      _aliceHash,
      _bob,
      ripemd160(abi.encodePacked(sha256(abi.encodePacked(_bobSecret)))),
      _amount,
      _tokenAddress
    ));
    require(dealHash == deals[_dealId].dealHash);

    deals[_dealId].state = DealState.PaymentSentToAlice;
    if (_tokenAddress == 0x0) {
      msg.sender.transfer(_amount);
    } else {
      ERC20 token = ERC20(_tokenAddress);
      assert(token.transfer(msg.sender, _amount));
    }
  }

  function bobClaimsPayment(
    bytes32 _dealId,
    uint _amount,
    address _tokenAddress,
    address _alice,
    bytes20 _bobHash,
    bytes _aliceSecret
  ) external {
    require(deals[_dealId].state == DealState.Initialized);
    bytes20 dealHash = ripemd160(abi.encodePacked(
      _alice,
      ripemd160(abi.encodePacked(sha256(abi.encodePacked(_aliceSecret)))),
      msg.sender,
      _bobHash,
      _amount,
      _tokenAddress
    ));
    require(dealHash == deals[_dealId].dealHash);
    deals[_dealId].state = DealState.PaymentSentToBob;
    if (_tokenAddress == 0x0) {
      msg.sender.transfer(_amount);
    } else {
      ERC20 token = ERC20(_tokenAddress);
      assert(token.transfer(msg.sender, _amount));
    }
  }
}