contract FUTC1 is StandardToken {
  using SafeMath for uint;
  using ERC20AsmFn for ERC20;

  string public constant name = "Futereum Centurian 1";
  string public constant symbol = "FUTC1";
  uint8 public constant decimals = 0;

  address public admin;
  uint public totalEthReleased = 0;

  mapping(address => uint) public ethReleased;
  address[] public trackedTokens;
  mapping(address => bool) public isTokenTracked;
  mapping(address => uint) public totalTokensReleased;
  mapping(address => mapping(address => uint)) public tokensReleased;

  constructor() public {
    admin = msg.sender;
    totalSupply_ = 100000;
    balances[admin] = totalSupply_;
    emit Transfer(address(0), admin, totalSupply_);
  }

  function () public payable {}

  modifier onlyAdmin() {
    require(msg.sender == admin);
    _;
  }

  function changeAdmin(address _receiver) onlyAdmin external {
    admin = _receiver;
  }

  /**
   * Claim your eth.
   */
  function claimEth() public {
    claimEthFor(msg.sender);
  }

  // Claim eth for address
  function claimEthFor(address payee) public {
    require(balances[payee] > 0);

    uint totalReceived = address(this).balance.add(totalEthReleased);
    uint payment = totalReceived.mul(
      balances[payee]).div(
        totalSupply_).sub(
          ethReleased[payee]
    );

    require(payment != 0);
    require(address(this).balance >= payment);

    ethReleased[payee] = ethReleased[payee].add(payment);
    totalEthReleased = totalEthReleased.add(payment);

    payee.transfer(payment);
  }

  // Claim your tokens
  function claimMyTokens() external {
    claimTokensFor(msg.sender);
  }

  // Claim on behalf of payee address
  function claimTokensFor(address payee) public {
    require(balances[payee] > 0);

    for (uint16 i = 0; i < trackedTokens.length; i++) {
      claimToken(trackedTokens[i], payee);
    }
  }

  /**
   * Transfers the unclaimed token amount for the given token and address
   * @param _tokenAddr The address of the ERC20 token
   * @param _payee The address of the payee (FUTC holder)
   */
  function claimToken(address _tokenAddr, address _payee) public {
    require(balances[_payee] > 0);
    require(isTokenTracked[_tokenAddr]);

    uint payment = getUnclaimedTokenAmount(_tokenAddr, _payee);
    if (payment == 0) {
      return;
    }

    ERC20 Token = ERC20(_tokenAddr);
    require(Token.balanceOf(address(this)) >= payment);
    tokensReleased[address(Token)][_payee] = tokensReleased[address(Token)][_payee].add(payment);
    totalTokensReleased[address(Token)] = totalTokensReleased[address(Token)].add(payment);
    Token.asmTransfer(_payee, payment);
  }

  /**
   * Returns the amount of a token (tokenAddr) that payee can claim
   * @param tokenAddr The address of the ERC20 token
   * @param payee The address of the payee
   */
  function getUnclaimedTokenAmount(address tokenAddr, address payee) public view returns (uint) {
    ERC20 Token = ERC20(tokenAddr);
    uint totalReceived = Token.balanceOf(address(this)).add(totalTokensReleased[address(Token)]);
    uint payment = totalReceived.mul(
      balances[payee]).div(
        totalSupply_).sub(
          tokensReleased[address(Token)][payee]
    );
    return payment;
  }

  function transfer(address _to, uint256 _value) public returns (bool) {
    require(msg.sender != _to);
    uint startingBalance = balances[msg.sender];
    require(super.transfer(_to, _value));

    transferChecks(msg.sender, _to, _value, startingBalance);
    return true;
  }

  function transferFrom(address _from, address _to, uint256 _value) public returns (bool){
    require(_from != _to);
    uint startingBalance = balances[_from];
    require(super.transferFrom(_from, _to, _value));

    transferChecks(_from, _to, _value, startingBalance);
    return true;
  }

  function transferChecks(address from, address to, uint checks, uint startingBalance) internal {

    // proportional amount of eth released already
    uint claimedEth = ethReleased[from].mul(
      checks).div(
        startingBalance
    );

    // increment to's released eth
    ethReleased[to] = ethReleased[to].add(claimedEth);

    // decrement from's released eth
    ethReleased[from] = ethReleased[from].sub(claimedEth);

    for (uint16 i = 0; i < trackedTokens.length; i++) {
      address tokenAddr = trackedTokens[i];

      // proportional amount of token released already
      uint claimed = tokensReleased[tokenAddr][from].mul(
        checks).div(
          startingBalance
      );

      // increment to's released token
      tokensReleased[tokenAddr][to] = tokensReleased[tokenAddr][to].add(claimed);

      // decrement from's released token
      tokensReleased[tokenAddr][from] = tokensReleased[tokenAddr][from].sub(claimed);
    }
  }

  function trackToken(address _addr) onlyAdmin external {
    require(_addr != address(0));
    require(!isTokenTracked[_addr]);
    trackedTokens.push(_addr);
    isTokenTracked[_addr] = true;
  }

  /*
   * However unlikely, it is possible that the number of tracked tokens
   * reaches the point that would make the gas cost of transferring FUTC
   * exceed the block gas limit. This function allows the admin to remove
   * a token from the tracked token list thus reducing the number of loops
   * required in transferChecks, lowering the gas cost of transfer. The
   * remaining balance of this token is sent back to the token's contract.
   *
   * Removal is irreversible.
   *
   * @param _addr The address of the ERC token to untrack
   * @param _position The index of the _addr in the trackedTokens array.
   * Use web3 to cycle through and find the index position.
   */
  function unTrackToken(address _addr, uint16 _position) onlyAdmin external {
    require(isTokenTracked[_addr]);
    require(trackedTokens[_position] == _addr);

    ERC20(_addr).asmTransfer(_addr, ERC20(_addr).balanceOf(address(this)));
    trackedTokens[_position] = trackedTokens[trackedTokens.length-1];
    delete trackedTokens[trackedTokens.length-1];
    trackedTokens.length--;
  }
}
/**
 * Holders of FUTC can claim tokens fed to it using the claimTokens()
 * function. This contract will be fed several tokens automatically by other Futereum
 * contracts.
 */
function claimEthFor(address payee) public {
  require(balances[payee] > 0);

  uint totalReceived = address(this).balance.add(totalEthReleased);
  uint payment = totalReceived.mul(
    balances[payee]).div(
      totalSupply_).sub(
        ethReleased[payee]
  );

  require(payment != 0);
  require(address(this).balance >= payment);

  ethReleased[payee] = ethReleased[payee].add(payment);
  totalEthReleased = totalEthReleased.add(payment);

  payee.transfer(payment);
}

// Claim eth for address