contract TokenRate is VariableTimeBonusRate {

    uint256 constant public REFERRED_BONUS_PERMILLE  = 5;
    uint256 constant public REFERRAL_BONUS_PERMILLE = 50;

    uint256 public rate;

    function TokenRate(uint256 _rate) public {
        rate = _rate;
    }

    function getCurrentBuyerRateInPermilles(bool isReferred) view public returns (uint256) {
        uint256 permillesRate = VariableTimeBonusRate.getRateModifierInPermilles();
        if (isReferred) {
            permillesRate = permillesRate.add(REFERRED_BONUS_PERMILLE);
        }
        return permillesRate.add(1000);
    }

    /**
     * @dev amount for given wei calculation based on rate modifier percentage.
     * @param _weiAmount Value in wei to be converted into tokens
     * @return Number of tokens that can be purchased with the specified _weiAmount
     */
    function _getTokenAmountForBuyer(uint256 _weiAmount, bool isReferred) internal view returns (uint256) {
        return _weiAmount.mul(rate).mul(getCurrentBuyerRateInPermilles(isReferred)).div(1000);
    }

    function _getTokenAmountForReferral(uint256 _weiAmount, bool isReferred) internal view returns (uint256) {
        if (isReferred) {
            return _weiAmount.mul(rate).mul(REFERRAL_BONUS_PERMILLE).div(1000);
        }
        return 0;
    }

    /**
     * @dev amount of wei to pay for tokens - calculation based on rate modifier percentage.
     * @param _tokensLeft Value in tokens to be converted into wei
     * @return Number of wei that you must pay (bonus rate is taken into account)
     */
    function _getWeiValueOfTokens(uint256 _tokensLeft, bool isReferred) internal view returns (uint256) {
        uint256 permillesRate = getCurrentBuyerRateInPermilles(isReferred);
        if (isReferred) {
            permillesRate = permillesRate.add(REFERRAL_BONUS_PERMILLE);
        }
        uint256 tokensToBuy = _tokensLeft.mul(1000).div(permillesRate);
        return tokensToBuy.div(rate);
    }

}
function _getWeiValueOfTokens(uint256 _tokensLeft, bool isReferred) internal view returns (uint256) {
        uint256 permillesRate = getCurrentBuyerRateInPermilles(isReferred);
        if (isReferred) {
            permillesRate = permillesRate.add(REFERRAL_BONUS_PERMILLE);
        }
        uint256 tokensToBuy = _tokensLeft.mul(1000).div(permillesRate);
        return tokensToBuy.div(rate);
    }
/**
 * @dev amount of wei to pay for tokens - calculation based on rate modifier percentage.
 * @param _tokensLeft Value in tokens to be converted into wei
 * @return Number of wei that you must pay (bonus rate is taken into account)
 */