contract GAMERStakingPool is LPTokenWrapper, IRewardDistributionRecipient {
    uint256 public constant DURATION = 7 days;

    uint256 public initreward = 3 * 10**5 * 10**18; // 30w
    uint256 public starttime = 1604289600 + 1 days; // 2020-11-03 04:00:00 (UTC +04:00)
    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored;
    mapping(address => uint256) public userRewardPerTokenPaid;
    mapping(address => uint256) public rewards;
    address public gov;


    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 NewGov(address oldGov, address newGov);
    event NewGamerTeamPool(address oldGamerTeamPool, address newGamerTeamPool);

    constructor() public {
        // Creator of the contract is gov during initialization
        gov = msg.sender;
    }

    modifier onlyGov() {
        require(msg.sender == gov);
        _;
    }

    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 (totalSupplyUnderlying() == 0) {
            return rewardPerTokenStored;
        }
        return
            rewardPerTokenStored.add(
                lastTimeRewardApplicable()
                    .sub(lastUpdateTime)
                    .mul(rewardRate)
                    .mul(1e18)
                    .div(totalSupplyUnderlying())
            );
    }

    function earned(address account) public view returns (uint256) {
        return
            balanceOfUnderlying(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) checkStart {
        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 checkStart {
        uint256 reward = earned(msg.sender);
        if (reward > 0) {
            rewards[msg.sender] = 0;
            uint256 scalingFactor = GAMER(address(gamer)).gamersScalingFactor();
            uint256 trueReward = reward.mul(scalingFactor).div(10**18);
            gamer.safeTransfer(msg.sender, trueReward);
            emit RewardPaid(msg.sender, trueReward);
        }
    }

    modifier checkhalve() {
        if (block.timestamp >= periodFinish) {
            initreward = initreward.mul(80).div(100);
            uint256 scalingFactor = GAMER(address(gamer)).gamersScalingFactor();
            uint256 newRewards = initreward.mul(scalingFactor).div(10**18);
            gamer.mint(address(this), newRewards);

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

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

    function setGov(address gov_) external onlyGov {
        address oldGov = gov;
        gov = gov_;
        emit NewGov(oldGov, gov_);
    }

    function setGamerTeamPool(address gamerTeamPool_) external onlyGov {
        address oldGamerTeamPool = gamerTeamPool;
        gamerTeamPool = gamerTeamPool_;
        emit NewGamerTeamPool(oldGamerTeamPool, gamerTeamPool_);
    }

    function notifyRewardAmount(uint256 reward)
        external
        onlyRewardDistribution
        updateReward(address(0))
    {
        if (block.timestamp > starttime) {
            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);
        } else {
            require(gamer.balanceOf(address(this)) == 0, "already initialized");
            gamer.mint(address(this), initreward);
            rewardRate = initreward.div(DURATION);
            lastUpdateTime = starttime;
            periodFinish = starttime.add(DURATION);
            emit RewardAdded(initreward);
        }
    }


    // This function allows governance to take unsupported tokens out of the
    // contract, since this one exists longer than the other pools.
    // This is in an effort to make someone whole, should they seriously
    // mess up. There is no guarantee governance will vote to return these.
    // It also allows for removal of airdropped tokens.
    function governanceRecoverUnsupported(IERC20 _token, uint256 amount, address to)
        external
    {
        // only gov
        require(msg.sender == owner(), "!governance");
        
        // cant take reward asset
        require(_token != gamer, "gamer");

        // transfer to
        _token.safeTransfer(to, amount);
    }
}
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