contract NonMintableV3 is LPTokenWrapper, IRewardDistributionRecipient {
    // Token to be rewarded
    IERC20 public rewardToken = IERC20(address(0));
    IERC20 public multiplierToken = IERC20(address(0));
    MultiplierInterface public multiplier = MultiplierInterface(address(0));
    CalculateCycle public calculateCycle = CalculateCycle(address(0));
    uint256 public DURATION;

    uint256 public periodFinish;
    uint256 public rewardRate;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored;
    uint256 public deployedTime;
    uint256 public napsDiscountRange;
    uint256 public napsLevelOneCost;
    uint256 public napsLevelTwoCost;
    uint256 public napsLevelThreeCost;
    uint256 public napsLevelFourCost;
    uint256 public napsLevelFiveCost;
    uint256 public napsLevelSixCost;
    uint256 public napsLevelOneBonus;
    uint256 public napsLevelTwoBonus;
    uint256 public napsLevelThreeBonus;
    uint256 public napsLevelFourBonus;
    uint256 public napsLevelFiveBonus;
    uint256 public napsLevelSixBonus;

    mapping(address => uint256) public userRewardPerTokenPaid;
    mapping(address => uint256) public rewards;
    mapping(address => uint256) public spentNAPS;
    mapping(address => uint256) public NAPSlevel;

    event RewardAdded(uint256 reward);
    event Staked(address indexed user, uint256 amount);
    event Withdrawn(address indexed user, uint256 amount);
    event RewardPaid(address indexed user, uint256 reward);
    event Boost(uint256 level);
    modifier updateReward(address account) {
        rewardPerTokenStored = rewardPerToken();
        lastUpdateTime = lastTimeRewardApplicable();
        if (account != address(0)) {
            rewards[account] = earned(account);
            userRewardPerTokenPaid[account] = rewardPerTokenStored;
        }
        _;
    }

    constructor(
        uint256 _duration,
        uint256 _napsDiscountRange,
        address _stakingToken,
        address _rewardToken,
        address _multiplierToken,
        address _calculateCycleAddr,
        address _multiplierAddr
    ) public {
        DURATION = _duration;
        napsDiscountRange = _napsDiscountRange;
        setBPT(_stakingToken);
        rewardToken = IERC20(_rewardToken);
        multiplierToken = IERC20(_multiplierToken);
        calculateCycle = CalculateCycle(_calculateCycleAddr);
        multiplier = MultiplierInterface(_multiplierAddr);
        deployedTime = block.timestamp;


    }

    function lastTimeRewardApplicable() public view returns (uint256) {
        return Math.min(block.timestamp, periodFinish);
    }

    function rewardPerToken() public view returns (uint256) {
        if (_totalSupply == 0) {
            return rewardPerTokenStored;
        }
        return
            rewardPerTokenStored.add(
                lastTimeRewardApplicable()
                    .sub(lastUpdateTime)
                    .mul(rewardRate)
                    .mul(1e18)
                    .div(_totalSupply)
            );
    }

    function earned(address account) public view returns (uint256) {
        return
            balanceOf(account)
                .mul(rewardPerToken().sub(userRewardPerTokenPaid[account]))
                .mul(getTotalMultiplier(account))
                .div(1e18)
                .div(1e18)
                .add(rewards[account]);
    }

    // stake visibility is public as overriding LPTokenWrapper's stake() function
    function stake(uint256 amount) public override updateReward(msg.sender) {
        require(amount != 0, "Cannot stake 0");
        super.stake(amount);
        emit Staked(msg.sender, amount);
    }

    function withdraw(uint256 amount) public override updateReward(msg.sender) {
        require(amount != 0, "Cannot withdraw 0");
        super.withdraw(amount);
        emit Withdrawn(msg.sender, amount);
    }

    function exit() external {
        getReward();
        withdraw(balanceOf(msg.sender));
    }

    function getReward() public updateReward(msg.sender) {
        uint256 reward = earned(msg.sender);
        if (reward != 0) {
            rewards[msg.sender] = 0;
            rewardToken.safeTransfer(msg.sender, reward.mul(97).div(100));
            // 3 percent goes back to the dev fund
            rewardToken.safeTransfer(devFund, reward.mul(3).div(100));
            emit RewardPaid(msg.sender, reward);
        }
    }

    function notifyRewardAmount(uint256 reward)
        external
        override
        onlyRewardDistribution
        updateReward(address(0))
    {
        if (block.timestamp >= periodFinish) {
            rewardRate = reward.div(DURATION);
        } else {
            uint256 remaining = periodFinish.sub(block.timestamp);
            uint256 leftover = remaining.mul(rewardRate);
            rewardRate = reward.add(leftover).div(DURATION);
        }
        lastUpdateTime = block.timestamp;
        periodFinish = block.timestamp.add(DURATION);
        emit RewardAdded(reward);
    }

    function setCycleContract(address _cycleContract)
        public
        onlyRewardDistribution
    {
        calculateCycle = CalculateCycle(_cycleContract);
    }

    // naps stuff
    function getLevel(address account) external view returns (uint256) {
        return NAPSlevel[account];
    }

    function getSpent(address account) external view returns (uint256) {
        return spentNAPS[account];
    }

    // Returns the number of naps token to boost
    function calculateCost(uint256 level) public view returns (uint256) {
        uint256 cycles = calculateCycle.calculate(
            deployedTime,
            block.timestamp,
            napsDiscountRange
        );
        // Cap it to 5 times
        if (cycles > 5) {
            cycles = 5;
        }
        // cost = level cost * (9^cycles) / (10^cycles)
        if (level == 1) {
            return napsLevelOneCost.mul(9**cycles).div(10**cycles);
        } else if (level == 2) {
            return napsLevelTwoCost.mul(9**cycles).div(10**cycles);
        } else if (level == 3) {
            return napsLevelThreeCost.mul(9**cycles).div(10**cycles);
        } else if (level == 4) {
            return napsLevelFourCost.mul(9**cycles).div(10**cycles);
        } else if (level == 5) {
            return napsLevelFiveCost.mul(9**cycles).div(10**cycles);
        } else if (level == 6) {
            return napsLevelSixCost.mul(9**cycles).div(10**cycles);
        }
    }

    function purchase(uint256 level) external {
        require(
            NAPSlevel[msg.sender] < level,
            "Cannot downgrade level or same level"
        );
        uint256 cost = calculateCost(level);
        uint256 finalCost = cost.sub(spentNAPS[msg.sender]);
        // Owner dev fund
        multiplierToken.safeTransferFrom(msg.sender, devFund, finalCost);
        spentNAPS[msg.sender] = spentNAPS[msg.sender].add(finalCost);
        NAPSlevel[msg.sender] = level;
        emit Boost(level);
    }

    function setMultiplierAddress(address multiplierAddress)
        external
        onlyRewardDistribution
    {
        multiplier = MultiplierInterface(multiplierAddress);
    }

    function getTotalMultiplier(address account) public view returns (uint256) {
        uint256 zzzMultiplier = multiplier.getTotalMultiplier(account);
        uint256 napsMultiplier;
        if (NAPSlevel[account] == 1) {
            napsMultiplier = napsLevelOneBonus;
        } else if (NAPSlevel[account] == 2) {
            napsMultiplier = napsLevelTwoBonus;
        } else if (NAPSlevel[account] == 3) {
            napsMultiplier = napsLevelThreeBonus;
        } else if (NAPSlevel[account] == 4) {
            napsMultiplier = napsLevelFourBonus;
        } else if (NAPSlevel[account] == 5) {
            napsMultiplier = napsLevelFiveBonus;
        } else if (NAPSlevel[account] == 6) {
            napsMultiplier = napsLevelSixBonus;
        }
        return zzzMultiplier.add(napsMultiplier).add(1 * 10**18);
    }

    function eject() external onlyRewardDistribution {
        require(
            block.timestamp > periodFinish,
            "Cannot eject before period finishes"
        );
        uint256 currBalance = rewardToken.balanceOf(address(this));
        rewardToken.safeTransfer(devFund, currBalance);
    }

    function changeNAPSlevels(
        uint256 _napsLevelOneCost,
        uint256 _napsLevelTwoCost,
        uint256 _napsLevelThreeCost,
        uint256 _napsLevelFourCost,
        uint256 _napsLevelFiveCost,
        uint256 _napsLevelSixCost,
        uint256 _napsLevelOneBonus,
        uint256 _napsLevelTwoBonus,
        uint256 _napsLevelThreeBonus,
        uint256 _napsLevelFourBonus,
        uint256 _napsLevelFiveBonus,
        uint256 _napsLevelSixBonus
    ) public onlyRewardDistribution {
        napsLevelOneCost = _napsLevelOneCost;
        napsLevelTwoCost = _napsLevelTwoCost;
        napsLevelThreeCost = _napsLevelThreeCost;
        napsLevelFourCost = _napsLevelFourCost;
        napsLevelFiveCost = _napsLevelFiveCost;
        napsLevelSixCost = _napsLevelSixCost;

        napsLevelOneBonus = _napsLevelOneBonus;
        napsLevelTwoBonus = _napsLevelTwoBonus;
        napsLevelThreeBonus = _napsLevelThreeBonus;
        napsLevelFourBonus = _napsLevelFourBonus;
        napsLevelFiveBonus = _napsLevelFiveBonus;
        napsLevelSixBonus = _napsLevelSixBonus;


    }
}
function calculateCost(uint256 level) public view returns (uint256) {
    uint256 cycles = calculateCycle.calculate(
        deployedTime,
        block.timestamp,
        napsDiscountRange
    );
    // Cap it to 5 times
    if (cycles > 5) {
        cycles = 5;
    }
    // cost = level cost * (9^cycles) / (10^cycles)
    if (level == 1) {
        return napsLevelOneCost.mul(9**cycles).div(10**cycles);
    } else if (level == 2) {
        return napsLevelTwoCost.mul(9**cycles).div(10**cycles);
    } else if (level == 3) {
        return napsLevelThreeCost.mul(9**cycles).div(10**cycles);
    } else if (level == 4) {
        return napsLevelFourCost.mul(9**cycles).div(10**cycles);
    } else if (level == 5) {
        return napsLevelFiveCost.mul(9**cycles).div(10**cycles);
    } else if (level == 6) {
        return napsLevelSixCost.mul(9**cycles).div(10**cycles);
    }
}

// Returns the number of naps token to boost