contract PoolReward is LPTokenWrapper {
    IERC20 public rewardtoken = IERC20(0); 
    uint256 public constant oneday = 15 days;

    uint256 public initreward = 0;
    uint256 public starttime = 0; //09/11/2020 @ 10:00 (UTC)
    uint256 public endtime = 0;
    uint256 public rewardRate = 0;
	uint256 public lastUpdateTime = 0;
    uint256 public rewardPerTokenStored = 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 RewardPaid(address indexed user, uint256 reward);

	constructor(address _lptoken,address _rewardtoken,uint256 _initreward,uint256 _starttime,uint256 _duration) LPTokenWrapper(IERC20(_lptoken)) public{
	    rewardtoken = IERC20(rewardtoken);
	    starttime = _starttime;
	    lastUpdateTime = starttime;
	    initreward = _initreward;
	    endtime = starttime.add(oneday.mul(_duration));
	    rewardRate = initreward.div(oneday.mul(_duration));
	}
	
    modifier updateReward(address account) {
		if(lastTimeRewardApplicable() > starttime && lastUpdateTime < endtime){
			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, endtime);
    }

    function rewardPerToken() public view returns (uint256) {
        if ((block.timestamp < starttime) || (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) { 
        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)  {
        uint256 reward = earned(msg.sender);
        if (reward > 0) {
            rewards[msg.sender] = 0;
            rewardtoken.safeTransfer(msg.sender, reward);
            emit RewardPaid(msg.sender, reward);
        }
    }
}
function stake(uint256 amount) public updateReward(msg.sender) { 
    require(amount > 0, "Cannot stake 0");
    super.stake(amount);
    emit Staked(msg.sender, amount);
}

// stake visibility is public as overriding LPTokenWrapper's stake() function