contract TokenContinuousDistribution is Ownable {
    using SafeMath for uint256;
    using SafeERC20 for ERC20Basic;

    event Released(ERC20Basic token, uint256 amount);

    // beneficiary of tokens after they are released
    address public beneficiary;

    uint256 public cliff;
    uint256 public start;
    uint256 public endTime;
    // 1 day = 86400 seconds
    uint256 public secondsIn1Unit = 86400;
    // 365 days * 5 = 1825 time units
    uint256 public numberOfUnits = 1825;
    // 86400 * 1825
    uint256 public duration = 157680000;

    //1st interval gets 5/15*total balance allowed, 2nd gets 4/15*TBA, 3rd gets 3*TBA, 4th gets 2*TBA, 5th gets 1*TBA
    uint256 numberOfPhases = 5;
    // 15=5+4+3+2+1
    uint256 slice = 15;

    mapping(address => uint256) public released;

    /**
     * @dev Creates a continuous distribution contract that distributes its balance of any ERC20 token to the
     * _beneficiary, gradually in a linear fashion until _start + _duration,
     * where _duration is the result of secondsIn1Unit*numberOfUnits
     * By then all of the balance will have distributed.
     * @param _beneficiary address of the beneficiary to whom distributed tokens are transferred
     * @param _start the time (as Unix time) at which point continuous distribution starts
     * @param _cliff duration in seconds of the cliff in which tokens will begin to continuous-distribute
     */
    constructor(
        address _beneficiary,
        uint256 _start,
        uint256 _cliff
    )
    public
    {
        require(_beneficiary != address(0), "Beneficiary address should NOT be null.");
        require(_cliff <= duration, "Cliff should be less than or equal to duration (i.e. secondsIn1Unit.mul(numberOfUnits)).");
        require((numberOfUnits % 5) == 0, "numberOfUnits should be a multiple of 5");


        beneficiary = _beneficiary;
        cliff = _start.add(_cliff);
        start = _start;
        endTime = _start.add(duration);
    }

    /**
     * @notice Transfers distributed tokens to beneficiary.
     * @param token ERC20 token which is being distributed
     */
    function release(ERC20Basic token) public {
        uint256 unreleased = releasableAmount(token);

        require(unreleased > 0, "Unreleased amount should be larger than 0.");

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

        token.safeTransfer(beneficiary, unreleased);

        emit Released(token, unreleased);
    }

    /**
     * @dev Calculates the amount that has already distributed but hasn't been released yet.
     * @param token ERC20 token which is being distributed
     */
    function releasableAmount(ERC20Basic token) public view returns (uint256) {
        return distributedAmount(token).sub(released[token]);
    }

    /**
     * @dev Calculates the amount that has already distributed.
     * @param token ERC20 token which is being distributed
     */
    function distributedAmount(ERC20Basic token) public view returns (uint256) {
        uint256 blockTimestamp = block.timestamp;
        return distributedAmountWithBlockTimestamp(token, blockTimestamp);
    }


    function distributedAmountWithBlockTimestamp(ERC20Basic token, uint256 blockTimestamp) public view returns (uint256) {
        uint256 currentBalance = token.balanceOf(this);
        uint256 totalBalance = currentBalance.add(released[token]);

        if (blockTimestamp < cliff) {
            return 0;
        } else if (blockTimestamp >= endTime) {
            return totalBalance;
        } else {
            uint256 unitsPassed = blockTimestamp.sub(start).div(secondsIn1Unit); // number of time unit passed, remember unit is usually 'day'
            uint256 unitsIn1Phase = numberOfUnits.div(numberOfPhases); // remember unit is usually 'day'
            uint256 unitsInThisPhase;
            uint256 weight;

            if (unitsPassed < unitsIn1Phase) {
                weight = 5;
                unitsInThisPhase = unitsPassed;
                // delay division to last step to keep precision
                return unitsInThisPhase.mul(totalBalance).mul(weight).div(slice).div(unitsIn1Phase);
            } else if (unitsPassed < unitsIn1Phase.mul(2)) {
                weight = 4;
                unitsInThisPhase = unitsPassed.sub(unitsIn1Phase);
                // "5" because we have everything in the previous phase 
                // and note div(slice) is moved to the end, (x+y).div(slice) => x.div(slice).add(y.div(slice))
                return totalBalance.mul(5).add(unitsInThisPhase.mul(totalBalance).mul(weight).div(unitsIn1Phase)).div(slice);
            } else if (unitsPassed < unitsIn1Phase.mul(3)) {
                weight = 3;
                unitsInThisPhase = unitsPassed.sub(unitsIn1Phase.mul(2));
                // "9" because we have everything in the previous phase = 5+4
                // and note div(slice) is moved to the end, (x+y).div(slice) => x.div(slice).add(y.div(slice))
                return totalBalance.mul(9).add(unitsInThisPhase.mul(totalBalance).mul(weight).div(unitsIn1Phase)).div(slice);
            } else if (unitsPassed < unitsIn1Phase.mul(4)) {
                weight = 2;
                unitsInThisPhase = unitsPassed.sub(unitsIn1Phase.mul(3));
                // "12" because we have everything in the previous phase = 5+4+3
                // and note div(slice) is moved to the end, (x+y).div(slice) => x.div(slice).add(y.div(slice))
                return totalBalance.mul(12).add(unitsInThisPhase.mul(totalBalance).mul(weight).div(unitsIn1Phase)).div(slice);
            } else if (unitsPassed < unitsIn1Phase.mul(5)) {
                weight = 1;
                unitsInThisPhase = unitsPassed.sub(unitsIn1Phase.mul(4));
                // "14" because we have everything in the previous phase = 5+4+3+2
                // and note div(slice) is moved to the end, (x+y).div(slice) => x.div(slice).add(y.div(slice))
                return totalBalance.mul(14).add(unitsInThisPhase.mul(totalBalance).mul(weight).div(unitsIn1Phase)).div(slice);
            }
            require(blockTimestamp < endTime, "Block timestamp is expected to have not reached distribution endTime if the code even falls in here.");
        }
    }
}
function distributedAmount(ERC20Basic token) public view returns (uint256) {
        uint256 blockTimestamp = block.timestamp;
        return distributedAmountWithBlockTimestamp(token, blockTimestamp);
    }
/**
 * @dev Calculates the amount that has already distributed.
 * @param token ERC20 token which is being distributed
 */