contract NFIrewards is LPTokenWrapper, Ownable {
    IERC20 public NFI_TOKEN = IERC20(0xB908c9D7cae7764CC0ef2E9A3EbB959eA556e32e); // NFI token
    uint256 public constant DURATION = 7 days;

    uint256 public initreward = 1500 * 1e18; // 1500 NFI
    uint256 public starttime = 1611428400; // 23 January 2021 - 19:00:00 UTC
    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored;
    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 RewardPaid(address indexed user, uint256 reward);

    address public devVestingAddr;
    uint256 public devTotalAmt = 0;
    uint256 public devWithdrawnAmt = 0;
    uint256 public constant DEV_FUND_VESTING_PERIOD = 180 days;

    constructor (address _devVestingAddr) public {
        devVestingAddr = _devVestingAddr;
    }

    function transferDevAddr(address newAddr) public {
        require(msg.sender == devVestingAddr, "dev only");
        require(newAddr != address(0), "zero addr");
        devVestingAddr = newAddr;
    }

    function renounceDevAddr() public {
        require(msg.sender == devVestingAddr, "dev only");
        devVestingAddr = address(0);
    }

    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]);
    }

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

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

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

    function getReward() public updateReward(msg.sender) checkhalve {
        uint256 reward = earned(msg.sender);
        if (reward > 0) {
            rewards[msg.sender] = 0;
            NFI_TOKEN.safeTransfer(msg.sender, reward);
            emit RewardPaid(msg.sender, reward);
        }
    }

    modifier checkhalve(){
        if (block.timestamp >= periodFinish) {
            initreward = initreward.mul(50).div(100); 
            NFI_TOKEN.mint(address(this), initreward);

            rewardRate = initreward.div(DURATION);
            periodFinish = block.timestamp.add(DURATION);
            emit RewardAdded(initreward);
        }
        _;
    }

    modifier checkStart(){
        require(block.timestamp > starttime,"not start");
        _;
    }

    function notifyRewardAmount(uint256 reward) external
        onlyOwner
        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);
        }
        
        uint256 devNewAmt = reward.div(20); // 5% dev fund (vested)        
        NFI_TOKEN.mint(address(this), reward.add(devNewAmt));
        devTotalAmt = devTotalAmt.add(devNewAmt);

        lastUpdateTime = block.timestamp;
        periodFinish = block.timestamp.add(DURATION);
        emit RewardAdded(reward);
    }

    function withdrawDevFund(uint256 amt) external
    {
        require(msg.sender == devVestingAddr, "dev only");
        
        uint256 vestingTime = block.timestamp.sub(starttime);
        vestingTime = Math.min(vestingTime, DEV_FUND_VESTING_PERIOD);
        
        uint256 devWithdrawnAmtMax = devTotalAmt.mul(vestingTime).div(DEV_FUND_VESTING_PERIOD); // vesting

        uint256 devWithdrawnAmtNew = devWithdrawnAmt.add(amt);

        require(devWithdrawnAmtNew <= devWithdrawnAmtMax, "vesting");

        devWithdrawnAmt = devWithdrawnAmtNew;

        NFI_TOKEN.safeTransfer(devVestingAddr, amt);
    }
}
function stake(uint256 amount) public updateReward(msg.sender) checkhalve checkStart { 
        require(amount > 0, "Cannot stake 0");
        super.stake(amount);
        emit Staked(msg.sender, amount);
    }
// stake visibility is public as overriding LPTokenWrapper's stake() function