contract StakeBUND is LPTokenWrapper {
    IERC20 public bundNFT = IERC20(0x92B3367515a7D2dF838c2ccD9F5e1Fc07D977C20); // Must be changed with Presale
    uint256 public constant duration = 30 days;
    
    uint256 public starttime = 1614749400;              //-----| Starts immediately after deploy |-----                           
    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored;
    bool firstNotify;
  
    uint256 rewardAmount = 0;

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

    event RewardAdded(uint256 reward);
    event Staked(address indexed user, uint256 amount);
    event Withdrawn(address indexed user, uint256 amount);
    event Rewarded(address indexed from, address indexed to, uint256 value);

    modifier checkStart() {
        require(
            block.timestamp >= starttime,
            "BUND_BUNDNFT staking pool not started yet."
        );
        _;
    }

    modifier updateReward(address account) {
        rewardPerTokenStored = rewardPerToken();
        lastUpdateTime = lastTimeRewardApplicable();
        if (account != address(0)) {
            rewards[account] = earned(account);
            userRewardPerTokenPaid[account] = rewardPerTokenStored;
        }
        _;
    }
    
    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]))
                .div(1e18)
                .add(rewards[account]);
    }

    function stake(uint256 amount) public updateReward(msg.sender) checkStart {
        require(amount > 0, "Cannot stake 0");
        super.stake(amount);
    }

    function withdraw(uint256 amount)
        public
        updateReward(msg.sender)
    {
        require(amount > 0, "Cannot withdraw 0");
        super.withdraw(amount);
    }

    // withdraw stake and get rewards at once
    function exit() external {
        withdraw(balanceOf(msg.sender));
        getReward();
    }

    function getReward() public updateReward(msg.sender){
        uint256 reward = earned(msg.sender);

        if (reward > 0) {
            rewards[msg.sender] = 0;
        
            bundNFT.safeTransfer(msg.sender, reward);
        }
        
    }

    function permitNotifyReward() public view returns (bool) {    //-----| If current reward session has completed |---------
        
        if(block.timestamp > starttime.add(duration)){
           return true;
        }
         
    }
 
    function notifyRewardRate(uint256 _reward) public updateReward(address(0)) onlyOwner{
       
        require(permitNotifyReward() == true || firstNotify == false, "Cannot notify until previous reward session is completed");
        
        rewardRate = _reward.div(duration);
       
        firstNotify = true;
        lastUpdateTime = block.timestamp;
        starttime = block.timestamp; 
        periodFinish = block.timestamp.add(duration);

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

// withdraw stake and get rewards at once