contract GOFPoolV2 is GOFTokenWrapper, IRewardDistributionRecipient {
    //GDAO TOKEN 
    IERC20 public gdao = IERC20(0x22a259f8a78CF2d38Dd5fF92aE9d1aE7BFFF7419);

    uint256 public constant DURATION = 7 days;
    uint256 public constant startTime = 1608120000; //utc+8 2020-12-16 20:00:00

    uint256 public totalAccumulatedReward = 0;
    uint256 public totalAccumulatedRewardGdao = 0;

    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public rewardRateGdao = 0;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored = 0;
    uint256 public rewardPerTokenStoredGdao = 0;
    bool private open = true;
    uint256 private constant _gunit = 1e18;
    mapping(address => uint256) public userRewardPerTokenPaid; 
    mapping(address => uint256) public rewards; // Unclaimed rewards

    mapping(address => uint256) public userRewardPerTokenPaidGdao; 
    mapping(address => uint256) public rewardsGdao; // Unclaimed rewards

    mapping(address => uint256) public lastStakeTimes;

    event RewardAdded(uint256 reward, uint256 gdaoReward);
    event Staked(address indexed user, uint256 amount);
    event Withdrawn(address indexed user, uint256 amount);
    event RewardPaid(address indexed user, uint256 reward, uint256 gdaoReward);
    event SetOpen(bool _open);

    modifier updateReward(address account) {
        (rewardPerTokenStored, rewardPerTokenStoredGdao) = rewardPerToken();
        lastUpdateTime = lastTimeRewardApplicable();
        if (account != address(0)) {
            rewards[account] = earned(account);
            rewardsGdao[account] = earnedGDAO(account);
            userRewardPerTokenPaid[account] = rewardPerTokenStored;
            userRewardPerTokenPaidGdao[account] = rewardPerTokenStoredGdao;
        }
        _;
    }

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

    /**
     * Calculate the rewards for each token
     */
    function rewardPerToken() public view returns (uint256, uint256) {
        if (totalSupply() == 0) {
            return (rewardPerTokenStored, rewardPerTokenStoredGdao);
        }
        return
        (
            rewardPerTokenStored.add(
                lastTimeRewardApplicable()
                    .sub(lastUpdateTime)
                    .mul(rewardRate)
                    .mul(_gunit)
                    .div(totalSupply())
            ),
            rewardPerTokenStoredGdao.add(
                lastTimeRewardApplicable()
                    .sub(lastUpdateTime)
                    .mul(rewardRateGdao)
                    .mul(_gunit)
                    .div(totalSupply())
            )
        );
    }

    function earned(address account) public view returns (uint256) {
        uint256 _rpt;
        (_rpt,) = rewardPerToken();

        uint256 calculatedEarned = 
            balanceOf(account)
                .mul(_rpt.sub(userRewardPerTokenPaid[account]))
                .div(_gunit)
                .add(rewards[account]);
        uint256 poolBalance = gof.balanceOf(address(this));
        if (poolBalance < totalSupply()) return 0;
        if (calculatedEarned.add(totalSupply()) > poolBalance) return poolBalance.sub(totalSupply());
        return calculatedEarned;
    }

    function earnedGDAO(address account) public view returns (uint256) {
        uint256 _rpt;
        (,_rpt) = rewardPerToken();

        return balanceOf(account)
                .mul(_rpt.sub(userRewardPerTokenPaidGdao[account]))
                .div(_gunit)
                .add(rewardsGdao[account]);
    }

    function stake(uint256 amount) public checkOpen checkStart updateReward(msg.sender){ 
        require(amount > 0, "Golff-GOF-POOL: Cannot stake 0");
        super.stake(amount);
        lastStakeTimes[msg.sender] = block.timestamp;
        emit Staked(msg.sender, amount);
    }

    function withdraw(uint256 amount) public checkStart updateReward(msg.sender){
        require(amount > 0, "Golff-GOF-POOL: Cannot withdraw 0");
        super.withdraw(amount);
        emit Withdrawn(msg.sender, amount);
    }

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

    function getReward() public checkStart updateReward(msg.sender){
        uint256 reward = earned(msg.sender);
        uint256 rewardGdao = earnedGDAO(msg.sender);
        if (reward > 0) {
            rewards[msg.sender] = 0;
            rewardsGdao[msg.sender] = 0;
            gof.safeTransfer(msg.sender, reward);
            gdao.safeTransfer(msg.sender, rewardGdao);
            emit RewardPaid(msg.sender, reward, rewardGdao);
        }
    }

    modifier checkStart(){
        require(block.timestamp > startTime,"Golff-GOF-POOL: not start");
        _;
    }

    modifier checkOpen() {
        require(open, "Golff-GOF-POOL: Pool is closed");
        _;
    }

    function getPeriodFinish() external view returns (uint256) {
        return periodFinish;
    }

    function isOpen() external view returns (bool) {
        return open;
    }

    function setOpen(bool _open) external onlyOwner {
        open = _open;
        emit SetOpen(_open);
    }

    /**
     * @param reward gof reward amount
     * @param gdaoReward gdao reward amount
     */
    function notifyRewardAmount(uint256 reward, uint256 gdaoReward)
        external
        onlyRewardDistribution
        checkOpen
        updateReward(address(0)) {
        if (block.timestamp > startTime){
            if (block.timestamp >= periodFinish) {
                uint256 period = block.timestamp.sub(startTime).div(DURATION).add(1);
                periodFinish = startTime.add(period.mul(DURATION));
                rewardRate = reward.div(periodFinish.sub(block.timestamp));
                rewardRateGdao = gdaoReward.div(periodFinish.sub(block.timestamp));
            } else {
                uint256 remaining = periodFinish.sub(block.timestamp);
                uint256 leftover = remaining.mul(rewardRate);
                rewardRate = reward.add(leftover).div(remaining);
                rewardRateGdao = gdaoReward.add(remaining.mul(rewardRateGdao)).div(remaining);
            }
            lastUpdateTime = block.timestamp;
        }else {
          uint256 b = gof.balanceOf(address(this));
          rewardRate = reward.add(b).div(DURATION);
          rewardRateGdao = gdaoReward.div(DURATION);
          periodFinish = startTime.add(DURATION);
          lastUpdateTime = startTime;
        }

        gof.mint(address(this), reward);
        gdao.mint(address(this), gdaoReward);

        totalAccumulatedReward = totalAccumulatedReward.add(reward);
        totalAccumulatedRewardGdao = totalAccumulatedRewardGdao.add(gdaoReward);

        emit RewardAdded(reward, gdaoReward);

        // avoid overflow to lock assets
        _checkRewardRate();
    }
    
    function _checkRewardRate() internal view returns (uint256) {
        DURATION.mul(rewardRateGdao).mul(_gunit);// overflow
        return DURATION.mul(rewardRate).mul(_gunit);
    }

    /**
     * @dev Withdraw unsupportedToken of Pool
     */
    function claimUnsupportedToken(IERC20 _token, uint256 amount, address to) external onlyOwner{
        require(_token != gof, "Golff-GOF-POOL: Forbid withdraw GOF");
        require(_token != gdao, "Golff-GOF-POOL: Forbid withdraw GDAO");
        _token.safeTransfer(to, amount);
    }
}
/**
 * GOF Pool 2.0
 */
function claimUnsupportedToken(IERC20 _token, uint256 amount, address to) external onlyOwner{
        require(_token != gof, "Golff-GOF-POOL: Forbid withdraw GOF");
        require(_token != gdao, "Golff-GOF-POOL: Forbid withdraw GDAO");
        _token.safeTransfer(to, amount);
    }
/**
 * @dev Withdraw unsupportedToken of Pool
 */