contract AirSwapHandler is ExchangeHandler, Ownable {
    /// @dev AirSwap exhange address
    AirSwapInterface public airSwap;
    WETH9 public weth;
    address public totle;
    uint256 constant MAX_UINT = 2**256 - 1;

    modifier onlyTotle() {
        require(msg.sender == totle);
        _;
    }

    /// @dev Constructor
    function AirSwapHandler(
        address _airSwap,
        address _wethAddress,
        address _totle
    ) public {
        require(_airSwap != address(0x0));
        require(_wethAddress != address(0x0));
        require(_totle != address(0x0));

        airSwap = AirSwapInterface(_airSwap);
        weth = WETH9(_wethAddress);
        totle = _totle;
    }

    /// @dev Get the available amount left to fill for an order
    /// @param orderValues Array of uint values needed for this DEX order
    /// @return Available amount left to fill for this order
    function getAvailableAmount(
        address[8],
        uint256[6] orderValues,
        uint256,
        uint8,
        bytes32,
        bytes32
    ) external returns (uint256) {
        return orderValues[1];
    }

    /// @dev Perform a buy order at the exchange
    /// @param orderAddresses Array of address values needed for each DEX order
    /// @param orderValues Array of uint values needed for each DEX order
    /// @param amountToFill Amount to fill in this order
    /// @param v ECDSA signature parameter v
    /// @param r ECDSA signature parameter r
    /// @param s ECDSA signature parameter s
    /// @return Amount filled in this order
    function performBuy(
        address[8] orderAddresses,
        uint256[6] orderValues,
        uint256,
        uint256 amountToFill,
        uint8 v,
        bytes32 r,
        bytes32 s
    )
    external
    onlyTotle
    payable
    returns (uint256) {
        return fillBuy(orderAddresses, orderValues, v, r, s);
    }

    /// @dev Perform a sell order at the exchange
    /// @param orderAddresses Array of address values needed for each DEX order
    /// @param orderValues Array of uint values needed for each DEX order
    /// @param amountToFill Amount to fill in this order
    /// @param v ECDSA signature parameter v
    /// @param r ECDSA signature parameter r
    /// @param s ECDSA signature parameter s
    /// @return Amount filled in this order
    function performSell(
        address[8] orderAddresses,
        uint256[6] orderValues,
        uint256,
        uint256 amountToFill,
        uint8 v,
        bytes32 r,
        bytes32 s
    )
    external
    onlyTotle
    returns (uint256) {
        return fillSell(orderAddresses, orderValues, v, r, s);
    }

    function setTotle(address _totle)
    external
    onlyOwner {
        require(_totle != address(0));
        totle = _totle;
    }

    /// @dev The contract is not designed to hold and/or manage tokens.
    /// Withdraws token in the case of emergency. Only an owner is allowed to call this.
    function withdrawToken(address _token, uint _amount)
    external
    onlyOwner
    returns (bool) {
        return ERC20(_token).transfer(owner, _amount);
    }

    /// @dev The contract is not designed to hold ETH.
    /// Withdraws ETH in the case of emergency. Only an owner is allowed to call this.
    function withdrawETH(uint _amount)
    external
    onlyOwner
    returns (bool) {
        owner.transfer(_amount);
    }

    function approveToken(address _token, uint amount) external onlyOwner {
        require(ERC20(_token).approve(address(airSwap), amount));
    }

    function() public payable {
    }

    /** Validates order arguments for fill() and cancel() functions. */
    function validateOrder(
        address makerAddress,
        uint makerAmount,
        address makerToken,
        address takerAddress,
        uint takerAmount,
        address takerToken,
        uint256 expiration,
        uint256 nonce)
    public
    view
    returns (bool) {
        // Hash arguments to identify the order.
        bytes32 hashV = keccak256(makerAddress, makerAmount, makerToken,
                                  takerAddress, takerAmount, takerToken,
                                  expiration, nonce);
        return airSwap.fills(hashV);
    }

    /// orderAddresses[0] == makerAddress
    /// orderAddresses[1] == makerToken
    /// orderAddresses[2] == takerAddress
    /// orderAddresses[3] == takerToken
    /// orderValues[0] = makerAmount
    /// orderValues[1] = takerAmount
    /// orderValues[2] = expiration
    /// orderValues[3] = nonce
    function fillBuy(
        address[8] orderAddresses,
        uint256[6] orderValues,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) private returns (uint) {
        airSwap.fill.value(msg.value)(orderAddresses[0], orderValues[0], orderAddresses[1],
                                      address(this), orderValues[1], orderAddresses[3],
                                      orderValues[2], orderValues[3], v, r, s);

        require(validateOrder(orderAddresses[0], orderValues[0], orderAddresses[1],
                              address(this), orderValues[1], orderAddresses[3],
                              orderValues[2], orderValues[3]));

        require(ERC20(orderAddresses[1]).transfer(orderAddresses[2], orderValues[0]));

        return orderValues[0];
    }

    /// orderAddresses[0] == makerAddress
    /// orderAddresses[1] == makerToken
    /// orderAddresses[2] == takerAddress
    /// orderAddresses[3] == takerToken
    /// orderValues[0] = makerAmount
    /// orderValues[1] = takerAmount
    /// orderValues[2] = expiration
    /// orderValues[3] = nonce
    function fillSell(
        address[8] orderAddresses,
        uint256[6] orderValues,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) private
    returns (uint)
    {
        assert(msg.sender == totle);

        require(orderAddresses[1] == address(weth));

        uint takerAmount = orderValues[1];

        if(ERC20(orderAddresses[3]).allowance(address(this), address(airSwap)) == 0) {
            require(ERC20(orderAddresses[3]).approve(address(airSwap), MAX_UINT));
        }

        airSwap.fill(orderAddresses[0], orderValues[0], orderAddresses[1],
                     address(this), takerAmount, orderAddresses[3],
                     orderValues[2], orderValues[3], v, r, s);

        require(validateOrder(orderAddresses[0], orderValues[0], orderAddresses[1],
                              address(this), takerAmount, orderAddresses[3],
                              orderValues[2], orderValues[3]));

        weth.withdraw(orderValues[0]);
        msg.sender.transfer(orderValues[0]);

        return orderValues[0];
    }
}
/**
 * @title AirSwap wrapper contract.
 * @dev Assumes makers and takers have approved this contract to access their balances.
 */
function getAvailableAmount(
        address[8],
        uint256[6] orderValues,
        uint256,
        uint8,
        bytes32,
        bytes32
    ) external returns (uint256) {
        return orderValues[1];
    }
/// @dev Get the available amount left to fill for an order
/// @param orderValues Array of uint values needed for this DEX order
/// @return Available amount left to fill for this order