contract TokenVesting is Ownable{
  using SafeMath for uint256;
  using SafeERC20 for ERC20Basic;
  
  
  ERC20Basic public token;
  
  
  event Released(uint256 amount);
  event Revoked();

  // beneficiary of tokens after they are released
  address public beneficiary;
  uint256 public cliff;
  uint256 public start;
  uint256 public duration;
  address public rollback;
  bool public revocable;
  
  uint256 public currentBalance;
  bool public initialized = false;
  
  uint256 public constant initialTokens = 23597*10**8;
  
  
  mapping (address => uint256) public released;
  mapping (address => bool) public revoked;
  
  
  uint256 public totalBalance;
  /**
   * @dev Creates a vesting contract that vests its balance of any ERC20 token to the
   * _beneficiary, gradually in a linear fashion until _start + _duration. By then all
   * of the balance will have vested.
   * @param _beneficiary address of the beneficiary to whom vested tokens are transferred
   * @param _cliff duration in seconds of the cliff in which tokens will begin to vest
   * @param _start the time (as Unix time) at which point vesting starts 
   * @param _duration duration in seconds of the period in which the tokens will vest
   * @param _revocable whether the vesting is revocable or not
   */
  constructor(
    address _beneficiary,
    uint256 _start,
    uint256 _cliff,
    uint256 _duration,
    bool _revocable,
    address _rollback,
    ERC20Basic _token
    
  )
    public
  {
    require(_beneficiary != address(0));
    require(_cliff <= _duration);

    beneficiary = _beneficiary;
    revocable = _revocable;
    duration = _duration;
    cliff = _start.add(_cliff);
    start = _start;
    token = _token;
    rollback = _rollback;

  }

    /**
   * initialize
   * @dev Initialize the contract
   **/
  function initialize() public onlyOwner {
       // Can only be initialized once
      require(tokensAvailable() == initialTokens); // Must have enough tokens allocated
      currentBalance = token.balanceOf(this);
      totalBalance = currentBalance.add(released[token]);
      initialized = true;
      
  }

 /**
   * tokensAvailable
   * @dev returns the number of tokens allocated to this contract
   **/
  function tokensAvailable() public constant returns (uint256) {
    
    return token.balanceOf(this);
  }
  
  
  
  function release() public {
    require(initialized);
    uint256 unreleased = releasableAmount();

    require(unreleased > 0);

    released[token] = released[token].add(unreleased);

    token.safeTransfer(beneficiary, unreleased);

    emit Released(unreleased);
  }


  function revoke() public onlyOwner {
    require(revocable);
    require(!revoked[token]);

    uint256 balance = token.balanceOf(this);

    uint256 unreleased = releasableAmount();
    uint256 refund = balance.sub(unreleased);
    
    revoked[token] = true;

    token.safeTransfer(rollback, refund);

    emit Revoked();
  }


  function releasableAmount() public returns (uint256) {
    return vestedAmount().sub(released[token]);
  }


  function vestedAmount() public returns (uint256) {
    
    currentBalance = token.balanceOf(this);
    totalBalance = currentBalance.add(released[token]);
    if (block.timestamp < cliff) {
      return 0;
    } else if (block.timestamp >= start.add(duration) || revoked[token]) {
      return totalBalance;
    } else {
        
      return totalBalance.mul(block.timestamp.sub(start)).div(duration);
    }
  }
}
/**
 * @title TokenVesting
 * @dev A token holder contract that can release its token balance gradually like a
 * typical vesting scheme, with a cliff and vesting period. Optionally revocable by the
 * owner.
 */
function tokensAvailable() public constant returns (uint256) {
    
  return token.balanceOf(this);
}

/**
 * tokensAvailable
 * @dev returns the number of tokens allocated to this contract
 **/