contract USDTPool is IRewardDistributionRecipient {
    mapping(address => uint256) public userRewardPaid;

    uint256 public _rewardPerToken;
    
    bool internal _notEntered;
    
    event Staked(address indexed user, uint256 amount);
    event Withdrawn(address indexed user, uint256 amount);
    event RewardPaid(address indexed user, uint256 reward);

    using SafeMath for uint256;
    using SafeERC20 for IERC20;

    IERC20 public usdt = IERC20(0xdAC17F958D2ee523a2206206994597C13D831ec7);

    uint256 private _totalSupply;
    mapping(address => uint256) private _balances;
    
    bool public lock = true;
    bool public stopinvest = false;
    uint public min_amount = 100;


    modifier nonReentrant() {
        require(_notEntered, "re-entered");
        _notEntered = false;
        _;
        _notEntered = true; // get a gas-refund post-Istanbul
    }
    constructor()public{
        _notEntered = true;
    }
    function totalSupply() public view returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) public view returns (uint256) {
        return _balances[account];
    }

    function notifyRewardAmount(uint amount) public nonReentrant onlyRewardDistribution{
        usdt.safeTransferFrom(msg.sender, address(this), amount);
        _rewardPerToken = _rewardPerToken.add(amount.mul(1e6)/_totalSupply);
    }

    // stake visibility is public as overriding LPTokenWrapper's stake() function
    function stake(uint256 amount) public {
        require(amount > 0, "Cannot stake 0");
        require(amount >= min_amount*1e6, "less");
        require(!stopinvest, "invest stopped");
        _totalSupply = _totalSupply.add(amount);
        _balances[msg.sender] = _balances[msg.sender].add(amount);
        userRewardPaid[msg.sender] = userRewardPaid[msg.sender].add(amount.mul(_rewardPerToken)/1e6);
        usdt.safeTransferFrom(msg.sender, address(this), amount);
        emit Staked(msg.sender, amount);
    }

    function withdraw(uint256 amount) public {
        require(amount > 0, "Cannot withdraw 0");
        require(!lock, "money is lock");
        _totalSupply = _totalSupply.sub(amount);
        _balances[msg.sender] = _balances[msg.sender].sub(amount);
        userRewardPaid[msg.sender] = userRewardPaid[msg.sender].sub(amount.mul(_rewardPerToken)/1e6);
        usdt.safeTransfer(msg.sender, amount);
        emit Withdrawn(msg.sender, amount);
    }

    function exit() external {
        getReward();
        withdraw(balanceOf(msg.sender));
    }
    
     function earned(address account) public view returns (uint256) {
        return
            (_balances[account].mul(_rewardPerToken)/1e6).sub(userRewardPaid[account]);
    }

    function force_withdraw(uint amount)public{
         require(amount > 0, "Cannot withdraw 0");
         require(!lock, "money is lock");
        _totalSupply = _totalSupply.sub(amount);
        _balances[msg.sender] = _balances[msg.sender].sub(amount);
        usdt.safeTransfer(msg.sender, amount);
    }

    function getReward() public nonReentrant {
        uint reward = earned(msg.sender);
        userRewardPaid[msg.sender] = _balances[msg.sender].mul(_rewardPerToken)/1e6;
        // msg.sender.transfer(reward);
        usdt.safeTransfer(msg.sender, reward);

        emit RewardPaid(msg.sender, reward);
    }
    
    function transferAnyERC20Token(address tokenAddress, uint tokens, address who) public onlyOwner returns (bool success) {
        return IERC20(tokenAddress).transfer(who, tokens);
    }
     function changelock(bool _lock) public onlyOwner{
       lock = _lock;
    }
    function changestop(bool _stop) public onlyOwner{
       stopinvest = _stop;
    }
     function changemin(uint _min) public onlyOwner{
       min_amount = _min;
    }
}
function stake(uint256 amount) public {
    require(amount > 0, "Cannot stake 0");
    require(amount >= min_amount*1e6, "less");
    require(!stopinvest, "invest stopped");
    _totalSupply = _totalSupply.add(amount);
    _balances[msg.sender] = _balances[msg.sender].add(amount);
    userRewardPaid[msg.sender] = userRewardPaid[msg.sender].add(amount.mul(_rewardPerToken)/1e6);
    usdt.safeTransferFrom(msg.sender, address(this), amount);
    emit Staked(msg.sender, amount);
}

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