contract TopPlayerTestToken is PausableToken {
  using SafeMath
  for uint256;

  // ERC20 constants
  string public name = "Top Players Mother Token Test";
  string public symbol = "TPMT Test";
  string public standard = "ERC20";

  uint8 public constant decimals = 18; // solium-disable-line uppercase

  uint256 public constant INITIAL_SUPPLY = 20 * (10 ** 8) * (10 ** 18);

  event ReleaseTarget(address target);

  mapping(address => TimeLock[]) public allocations;

  address[] public receiptors;

  address[] public froms;
  address[] public tos;
  uint[] public timess;
  uint256[] public balancess;
  uint[] public createTimes;

  struct TimeLock {
    uint time;
    uint256 balance;
    uint createTime;
  }

  /*Here is the constructor function that is executed when the instance is created*/
  constructor() public {
    totalSupply_ = INITIAL_SUPPLY;
    balances[msg.sender] = INITIAL_SUPPLY;
    emit Transfer(address(0), msg.sender, INITIAL_SUPPLY);
  }

  function getAllocations() public view returns(address[], address[],  uint[], uint256[], uint[]){
    getInfos();
    return (froms, tos, timess, balancess, createTimes); 
  }

  /**
   * @dev transfer token for a specified address if transfer is open
   * @param _to The address to transfer to.
   * @param _value The amount to be transferred.
   */
  function transfer(address _to, uint256 _value) public returns(bool) {
    require(canSubAllocation(msg.sender, _value));

    subAllocation(msg.sender);

    return super.transfer(_to, _value);
  }

  function canSubAllocation(address sender, uint256 sub_value) private constant returns(bool) {
    if (sub_value == 0) {
      return false;
    }

    if (balances[sender] < sub_value) {
      return false;
    }

    uint256 alllock_sum = 0;
    for (uint j = 0; j < allocations[sender].length; j++) {
      if (allocations[sender][j].time >= block.timestamp) {
        alllock_sum = alllock_sum.add(allocations[sender][j].balance);
      }
    }

    uint256 can_unlock = balances[sender].sub(alllock_sum);

    return can_unlock >= sub_value;
  }

  function subAllocation(address sender) private {
    for (uint j = 0; j < allocations[sender].length; j++) {
      if (allocations[sender][j].time < block.timestamp) {
        allocations[sender][j].balance = 0;
      }
    }
  }

  function setAllocation(address _address, uint256 total_value, uint time, uint256 balanceRequire) public onlyOwner returns(bool) {
    uint256 sum = 0;
    sum = sum.add(balanceRequire);

    require(total_value >= sum);

    require(balances[msg.sender] >= sum);

    uint256 createTime;

    if(allocations[_address].length == 0){
      receiptors.push(_address);
    }

    bool find = false;

    for (uint j = 0; j < allocations[_address].length; j++) {
      if (allocations[_address][j].time == time) {
        allocations[_address][j].balance = allocations[_address][j].balance.add(balanceRequire);
        find = true;
        break;
      }
    }

    if (!find) {
      createTime = now;
      allocations[_address].push(TimeLock(time, balanceRequire, createTime));
    }

    bool result = super.transfer(_address, total_value);

    emit Transferred(msg.sender, _address, createTime, total_value, time);

    return result;
  }

  function releaseAllocation(address target) public onlyOwner {
    require(balances[target] > 0);

    for (uint j = 0; j < allocations[target].length; j++) {
      allocations[target][j].balance = 0;
    }

    emit ReleaseTarget(target);
  }

  event Transferred(address from, address to, uint256 createAt, uint256 total_value, uint time);

  function getInfos() public {
    if (msg.sender == owner){
      for (uint i=0; i<receiptors.length; i++){
        for (uint j=0; j<allocations[receiptors[i]].length; j++){
          froms.push(owner);
          tos.push(receiptors[i]);
          timess.push(allocations[receiptors[i]][j].time);
          balancess.push(allocations[receiptors[i]][j].balance);
          createTimes.push(allocations[receiptors[i]][j].createTime);
        }
      }
    }else{
      for (uint k=0; k<allocations[msg.sender].length; k++){
        froms.push(owner);
        tos.push(msg.sender);
        timess.push(allocations[msg.sender][k].time);
        balancess.push(allocations[msg.sender][k].balance);
        createTimes.push(allocations[msg.sender][k].createTime);
      }
    }
  }
}
/*****************************************************************************
 *
 *Copyright 2018 TopPlayer
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
 *
 *****************************************************************************/
function transfer(address _to, uint256 _value) public returns(bool) {
    require(canSubAllocation(msg.sender, _value));

    subAllocation(msg.sender);

    return super.transfer(_to, _value);
  }
/**
 * @dev transfer token for a specified address if transfer is open
 * @param _to The address to transfer to.
 * @param _value The amount to be transferred.
 */