contract ThePoolLP3 is LPTokenWrapper, IRewardDistributionRecipient {
    string public name;
    
    // 18 decimals -> 18 decimals, reward multipliers @ 10/5/2
    // Token to be rewarded
    IERC20 public rewardToken = IERC20(address(0));
    IERC20 public vaccToken = IERC20(address(0));
    uint256 public DURATION = 3 weeks;

    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored;
    uint256 public deployedTime;
    bool public ejected;
    mapping(address => bool) public locked;
    mapping(address => uint256) public whenLocked;
    mapping(address => uint256) public lockMultiplier;
    mapping(address => uint256) public vaccMultiplier;

    mapping(address => uint256) public userRewardPerTokenPaid;
    mapping(address => uint256) public rewards;

    event RewardAdded(uint256 reward);
    event Staked(address indexed user, uint256 amount);
    event Locked(address indexed user);
    event Unlocked(address indexed user);
    event Withdrawn(address indexed user, uint256 amount);
    event RewardPaid(address indexed user, uint256 reward);
    event Boost(uint256 level);
    modifier updateReward(address account) {
        rewardPerTokenStored = rewardPerToken();
        lastUpdateTime = lastTimeRewardApplicable();
        if (account != address(0)) {
            rewards[account] = earned(account);
            userRewardPerTokenPaid[account] = rewardPerTokenStored;
        }
        _;
    }

    constructor(
        address _stakingToken,
        address _rewardToken,
        address _vaccToken
    ) public {
        setBPT(_stakingToken);
        rewardToken = IERC20(_rewardToken);
        vaccToken = IERC20(_vaccToken);
        deployedTime = block.timestamp;
        ejected=true;
        name="ThePoolLP3";
    }
    
    function isLocked(address account) public view returns (bool) {
     return(locked[account]);
    }

    function blockTimestamp() public view returns (uint256) {
     return(block.timestamp);
    }
    
    function lockStake() public updateReward(msg.sender) {
     require(balanceOf(msg.sender) > 0, "No stake to lock.");
     require(locked[msg.sender]==false,"Stake already locked.");
     require(block.timestamp < periodFinish,"Cannot lock stake until pool active."); // omit = to be safe
     locked[msg.sender]=true;
     whenLocked[msg.sender]=block.timestamp;
     uint256 remaining = periodFinish.sub(block.timestamp);
     if(remaining>2 weeks)
      lockMultiplier[msg.sender]=50;
     else if(remaining>1 weeks)
      lockMultiplier[msg.sender]=25;
     else
      lockMultiplier[msg.sender]=10;
     uint256 vaccBalance=vaccToken.balanceOf(msg.sender);
     if(vaccBalance>50000*10**18)
      vaccBalance=50000*10**18;
     vaccMultiplier[msg.sender]=vaccBalance.div(1000*10**18);
     emit Locked(msg.sender);
    }
    
    function unlockStake() public updateReward(msg.sender) {
     require(block.timestamp > periodFinish,"Cannot unlock stake while pool is running.");
     require(locked[msg.sender]==true,"Stake already unlocked.");
     locked[msg.sender]=false;
     whenLocked[msg.sender]=0;
     lockMultiplier[msg.sender]=0;
     vaccMultiplier[msg.sender]=0;
     emit Unlocked(msg.sender);
    }
    
    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) {
       if (rewardPerToken()<userRewardPerTokenPaid[account])
        return 0;
       return
            balanceOf(account)
                .mul(rewardPerToken().sub(userRewardPerTokenPaid[account]))
                .div(1e18)
                .mul(getTotalMultiplier(account))
                .div(250) // to ensure if everyone has 150% bonus there are enough rewards
                .add(rewards[account]);
    }
    
    // stake visibility is public as overriding LPTokenWrapper's stake() function
    function stake(uint256 amount) public override updateReward(msg.sender) {
        require(amount > 0, "Cannot stake 0");
        super.stake(amount);
        if(locked[msg.sender]==true) {
         whenLocked[msg.sender]=block.timestamp;
         if(block.timestamp>periodFinish)
          lockMultiplier[msg.sender]=50;
         else {
          uint256 remaining = periodFinish.sub(block.timestamp);
          if(remaining>2 weeks)
           lockMultiplier[msg.sender]=50;
          else if(remaining>1 weeks)
           lockMultiplier[msg.sender]=25;
          else
           lockMultiplier[msg.sender]=10;
          }
         }
        emit Staked(msg.sender, amount);
    }

    function withdraw(uint256 amount) public override updateReward(msg.sender) {
        require(amount > 0, "Cannot withdraw 0");
        require(locked[msg.sender]==false,"Stake locked.");
        super.withdraw(amount);
        emit Withdrawn(msg.sender, amount);
    }

    function exit() external {
        require(balanceOf(msg.sender) > 0, "Cannot withdraw 0");
        require(locked[msg.sender]==false,"Stake locked.");
        withdraw(balanceOf(msg.sender));
        if(ejected==false)
         getReward();
    }

    function getReward() public updateReward(msg.sender) {
        require(ejected==false,"Rewards have expired.");
        uint256 reward = earned(msg.sender);
        if (reward > 0) {
            rewards[msg.sender] = 0;
            rewardToken.safeTransfer(msg.sender, reward.mul(95).div(100));
            // 5 percent goes back to the dev fund
            rewardToken.safeTransfer(devFund, reward.mul(5).div(100));
            emit RewardPaid(msg.sender, reward);
        }
    }

    function notifyRewardAmount(uint256 reward)
        external
        override
        onlyRewardDistribution
        updateReward(address(0))
    {
        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);
        ejected=false;
        emit RewardAdded(reward);
    }

    function getTotalMultiplier(address account) public view returns (uint256) {
        uint256 cordMultiplier = 100;
        if(balanceOf(account)>=10*10**18)
         cordMultiplier=150;
        else if(balanceOf(account)>=5*10**18)
         cordMultiplier=125;
        else if(balanceOf(account)>=2*10**18)
         cordMultiplier=110;
        uint256 newlockMultiplier=lockMultiplier[account];
        if(locked[account]==true && newlockMultiplier<50) {
         if(whenLocked[account].add(DURATION)<periodFinish)
          newlockMultiplier=50;
        }
        cordMultiplier=cordMultiplier.add(newlockMultiplier);
        uint256 vaccBalance=vaccToken.balanceOf(account);
        if(vaccToken.whenSent(account)>whenLocked[account])
         vaccBalance=0;
        if(vaccBalance>50000*10**18)
         vaccBalance=50000*10**18;
        uint256 newvaccMultiplier=vaccBalance.div(1000*10**18);
        if(newvaccMultiplier>vaccMultiplier[account])
         newvaccMultiplier=vaccMultiplier[account];
        cordMultiplier=cordMultiplier.add(newvaccMultiplier);
        return cordMultiplier;
    }

    function eject() external onlyRewardDistribution {
        require(block.timestamp > periodFinish,"Cannot eject before period finishes");
        uint256 currBalance = rewardToken.balanceOf(address(this));
        rewardToken.safeTransfer(devFund, currBalance);
        ejected=true;
    }
}
function stake(uint256 amount) public override updateReward(msg.sender) {
    require(amount > 0, "Cannot stake 0");
    super.stake(amount);
    if(locked[msg.sender]==true) {
     whenLocked[msg.sender]=block.timestamp;
     if(block.timestamp>periodFinish)
      lockMultiplier[msg.sender]=50;
     else {
      uint256 remaining = periodFinish.sub(block.timestamp);
      if(remaining>2 weeks)
       lockMultiplier[msg.sender]=50;
      else if(remaining>1 weeks)
       lockMultiplier[msg.sender]=25;
      else
       lockMultiplier[msg.sender]=10;
      }
     }
    emit Staked(msg.sender, amount);
}

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