contract BEELINKPool is LPTokenWrapper, IRewardDistributionRecipient {
    IERC20 public bee = IERC20(0xcD4292701995F4707AE63fb1a48d80dB2c5F04D4);
    IERC20 public honey = IERC20(0x932441a0F11482CB5f8B8900aB981a39806F88fa);
    uint256 public constant DURATION = 625000; // ~7 1/4 days
    uint256 public constant HONEYWAIT = 2 days; // 2 days
    uint256 public constant EPOCH_REWARD = 500000;

    uint256 public starttime = 1599642000; // 09/09/2020 @ 9:00am (UTC +00: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;
    mapping(address => uint256) public paidReward;
    mapping(address => uint256) public paidHoney;

    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 HoneyPaid(address indexed user, uint256 reward);

    modifier checkStart() {
        require(block.timestamp >= starttime,"not start");
        _;
    }
    modifier checkHoneyWait() {
        require(block.timestamp >= starttime.add(HONEYWAIT), "need more bees");
        require(maxSupply() >= EPOCH_REWARD.mul(DECIMALS), "need more pollen");
        _;
    }

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

    function earned(address account) public view returns (uint256) {
        return
            sqrtBalance(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) {
            paidReward[msg.sender] = paidReward[msg.sender].add(reward);
            rewards[msg.sender] = 0;
            bee.safeTransfer(msg.sender, reward);
            emit RewardPaid(msg.sender, reward);
        }
    }
    function allEarned(address account) public view returns (uint256) {
        return earned(account).add(paidReward[account]);
    }
    function formatSqrt(uint256 balance) internal pure returns (uint256) {
        return Math.sqrt(balance.div(1e18)).mul(1e18);
    }
    // allHoney - paidHoney, see getHoney(), if should wait, need return 0
    function earnedHoney(address account) public view returns (uint256) {
        if (block.timestamp >= starttime.add(HONEYWAIT) && maxSupply() >= EPOCH_REWARD.mul(DECIMALS)) {
            return formatSqrt(allEarned(account)).sub(paidHoney[account]);
        } else {
            return 0;
        }
    } 
    function getHoney() public checkHoneyWait {
        uint256 beeAmount = allEarned(msg.sender);
        require(beeAmount > 1e18, "bee amount need > 1");
        uint256 allHoney = formatSqrt(beeAmount);
        uint256 needPayHoney = allHoney.sub(paidHoney[msg.sender]);
        if (needPayHoney > 0) {
            paidHoney[msg.sender] = allHoney;
            honey.safeTransfer(msg.sender, needPayHoney);
            emit HoneyPaid(msg.sender, needPayHoney);
        }
    }

    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 burnHoney(uint256 amount) external onlyRewardDistribution {
        require(block.timestamp > starttime.add(DURATION), "not finish");
        honey.safeTransfer(address(0), amount);
    }
}
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