contract GOKUKASHYCRVPool is LPTokenWrapper, IRewardDistributionRecipient {
    IERC20 public goku = IERC20(0x4E44FE174e934ff78A77BEe3C2b752fD1BEeFDEf);
    GOKUAirdrop public airdrop = GOKUAirdrop(0x98f0E1576e8f36643f961073441dE00c93683245);

    uint256 public constant DURATION = 604800; // 7 days

    uint256 public starttime = 1601683200; // 2020-10-03 12:00:00 AM (UTC UTC +00:00)
    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored;

    mapping(address => uint256) public boostStarttime;
    mapping(address => uint8) public currentBoost;
    mapping(address => uint8) public x2Count;
    mapping(address => uint8) public x4Count;
    mapping(address => uint8) public x8Count;
    mapping(address => uint8) public x10Count;

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

    modifier checkBoostStatus() {
        require(block.timestamp >= boostStarttime[msg.sender] + 1 hours, "already boost");
        _;
    }

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

    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) 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) checkStart {
        uint256 reward = earned(msg.sender);
        if (reward > 0) {
            rewards[msg.sender] = 0;
            uint256 scalingFactor = GOKU(address(goku)).gokusScalingFactor();
            uint256 trueReward = reward.mul(scalingFactor).div(10**18);
            uint256 remainAirdrop = airdrop.getRemainAirdrop(address(this));
            if (block.timestamp < boostStarttime[msg.sender] + 1 hours && remainAirdrop > 0) {
                uint256 boostReward = trueReward.mul(currentBoost[msg.sender] - 1);

                if (boostReward < remainAirdrop) {
                    airdrop.airdrop(boostReward);
                    goku.safeTransfer(msg.sender, trueReward.add(boostReward));
                    emit RewardPaid(msg.sender, trueReward.add(boostReward));
                }
                else {
                    airdrop.airdropAll();
                    goku.safeTransfer(msg.sender, trueReward.add(remainAirdrop));
                    emit RewardPaid(msg.sender, trueReward.add(remainAirdrop));
                }
            }
            else {
                goku.safeTransfer(msg.sender, trueReward);
                emit RewardPaid(msg.sender, trueReward);
            }
        }
    }

    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 {
          rewardRate = reward.div(DURATION);
          lastUpdateTime = starttime;
          periodFinish = starttime.add(DURATION);
          emit RewardAdded(reward);
        }
    }

    function boostx2() external payable checkBoostStatus checkStart {
        address payable fundaddress = 0xaC8eFCe22d8529f458b6c9cCb053f12f553Ae200;
        uint256 boostCost = uint256(15).mul(uint256(2) ** x2Count[msg.sender]).mul(10 ** 18).div(1000);
        require(msg.value == boostCost, "Wrong cost");
        if (!fundaddress.send(boostCost)) {
            return fundaddress.transfer(address(this).balance);
        }
        currentBoost[msg.sender] = 2;
        x2Count[msg.sender] = x2Count[msg.sender] + 1;
        boostStarttime[msg.sender] = block.timestamp;
    }

    function boostx4() external payable checkBoostStatus checkStart {
        address payable fundaddress = 0xaC8eFCe22d8529f458b6c9cCb053f12f553Ae200;
        uint256 boostCost = uint256(15).mul(uint256(2) ** x4Count[msg.sender]).mul(10 ** 18).div(1000);
        require(msg.value == boostCost, "Wrong cost");
        if (!fundaddress.send(boostCost)) {
            return fundaddress.transfer(address(this).balance);
        }
        currentBoost[msg.sender] = 4;
        x4Count[msg.sender] = x4Count[msg.sender] + 1;
        boostStarttime[msg.sender] = block.timestamp;
    }

    function boostx8() external payable checkBoostStatus checkStart {
        address payable fundaddress = 0xaC8eFCe22d8529f458b6c9cCb053f12f553Ae200;
        uint256 boostCost = uint256(15).mul(uint256(2) ** x8Count[msg.sender]).mul(10 ** 18).div(1000);
        require(msg.value == boostCost, "Wrong cost");
        if (!fundaddress.send(boostCost)) {
            return fundaddress.transfer(address(this).balance);
        }
        currentBoost[msg.sender] = 8;
        x8Count[msg.sender] = x8Count[msg.sender] + 1;
        boostStarttime[msg.sender] = block.timestamp;
    }

    function boostx10() external payable checkBoostStatus checkStart {
        address payable fundaddress = 0xaC8eFCe22d8529f458b6c9cCb053f12f553Ae200;
        uint256 boostCost = uint256(15).mul(uint256(2) ** x10Count[msg.sender]).mul(10 ** 18).div(1000);
        require(msg.value == boostCost, "Wrong cost");
        if (!fundaddress.send(boostCost)) {
            return fundaddress.transfer(address(this).balance);
        }
        currentBoost[msg.sender] = 10;
        x10Count[msg.sender] = x10Count[msg.sender] + 1;
        boostStarttime[msg.sender] = block.timestamp;
    }
}
function stake(uint256 amount) public updateReward(msg.sender) 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