contract MetaDM_LiquidityFarming is Ownable {
    using SafeMath for uint;
    using EnumerableSet for EnumerableSet.AddressSet;
    
    event Deposited(address farmer, uint amount);
    event Withdrawn(address farmer, uint amount);
    event RewardsTransferred(address farmer, uint amount);
    
    // META token contract address
    address public constant tokenAddress = 0xEc068b286D09E1650175caB9B93bFbb733eaC335;
    // UNI-V2 token contract address
    address public constant LPTokenAddress = 0x3d7E4674b3A78d7Aa5892fB43D380292F6910b1D;
    
    // amount disbursed per week
    uint public amountToDisburse = 25000000000000000000;   // 25 META PER DISBURSE
    
    // unfarming possible after ...
    uint public constant unstakeTime = 30 days;
    // claiming possible after ...
    uint public claimTime = 10 days;
    
    
    uint public totalClaimedRewards = 0;
    uint public totalDeposited = 0;
    uint public totalDisbursed = 0;
    uint public maxFarmers = 500;
    bool public ended = false;
    
    EnumerableSet.AddressSet private holders;
    
    mapping (address => uint) public depositedTokens;
    mapping (address => uint) public pending; 
    mapping (address => uint) public totalEarnedTokens;
    mapping (address => uint) public lastDepositTime;
    mapping (address => uint) public lastClaimTime;
    
    function disburse () public onlyOwner returns (bool){
        require(!ended, "Staking already ended");

        address _hold;
        uint _add;
        for(uint i = 0; i < holders.length(); i = i.add(1)){
            _hold = holders.at(i);
            _add = depositedTokens[_hold].mul(amountToDisburse).div(totalDeposited);
            pending[_hold] = pending[_hold].add(_add);
        }
        totalDisbursed = totalDisbursed.add(amountToDisburse);
        return true;
        
    }
    
    //End the farming pool
    function end() public onlyOwner returns (bool){
        require(!ended, "Staking already ended");
        ended = true;
        return true;
    }
    
    function getPendingDivs(address _holder) public view returns (uint) {
        uint pendingDivs = pending[_holder];
       
        return pendingDivs;
    }
    
    function getNumberOfHolders() public view returns (uint) {
        return holders.length();
    }
    
    function deposit(uint amountToStake) public returns (bool) {
        require(!ended, "Staking has ended");
        require(amountToStake > 0, "Cannot deposit 0 Tokens");
        require(holders.length() < maxFarmers, "Max Farmers reached");
    
        require(Token(LPTokenAddress).transferFrom(msg.sender, address(this), amountToStake), "Insufficient Token Allowance");
        
        depositedTokens[msg.sender] = depositedTokens[msg.sender].add(amountToStake);
        totalDeposited = totalDeposited.add(amountToStake);
        
        lastDepositTime[msg.sender] = block.timestamp;
        lastClaimTime[msg.sender] = block.timestamp;

        if (!holders.contains(msg.sender)) {
            holders.add(msg.sender);
        }

        emit Deposited(msg.sender, amountToStake);
        return true;
        
    }

    function claim() public returns(bool){
        require(block.timestamp.sub(lastClaimTime[msg.sender]) > claimTime, "Not yet.");
        require(pending[msg.sender] > 0);
        
        
        uint _reward = pending[msg.sender];
        pending[msg.sender] = 0;
        require(Token(tokenAddress).transfer(msg.sender, _reward), "Could not transfer tokens.");
        totalClaimedRewards = totalClaimedRewards.add(_reward);
        totalEarnedTokens[msg.sender] = totalEarnedTokens[msg.sender].add(_reward);
        
        lastClaimTime[msg.sender] = block.timestamp;

        return true;
    }

    function withdraw(uint _amount) public returns (bool){
        require(block.timestamp.sub(lastDepositTime[msg.sender]) > unstakeTime || ended, "Not yet.");
        require(depositedTokens[msg.sender] >= _amount);
        require(_amount > 0);
        
        depositedTokens[msg.sender] = depositedTokens[msg.sender].sub(_amount);
        totalDeposited = totalDeposited.sub(_amount);
        require(Token(LPTokenAddress).transfer(msg.sender, _amount), "Could not transfer tokens.");
        
        if(depositedTokens[msg.sender] == 0 && pending[msg.sender] == 0){
            holders.remove(msg.sender);    
        }

        emit Withdrawn(msg.sender, _amount);
        return true;
    }
    
    function getStakersList(uint startIndex, uint endIndex) 
        public 
        view 
        returns (address[] memory stakers, 
            uint[] memory stakedTokens) {
        require (startIndex < endIndex);
        
        uint length = endIndex.sub(startIndex);
        address[] memory _stakers = new address[](length);
        uint[] memory _stakedTokens = new uint[](length);
        
        for (uint i = startIndex; i < endIndex; i = i.add(1)) {
            address staker = holders.at(i);
            uint listIndex = i.sub(startIndex);
            _stakers[listIndex] = staker;
            _stakedTokens[listIndex] = depositedTokens[staker];
        }
        
        return (_stakers, _stakedTokens);
    }
    
    // function to allow admin to claim *other* ERC20 tokens sent to this contract (by mistake)
    function transferAnyERC20Tokens(address _tokenAddr, address _to, uint _amount) public onlyOwner returns (bool){
        require (_tokenAddr != LPTokenAddress , "Cannot Transfer Out this token");
        Token(_tokenAddr).transfer(_to, _amount);
        return true;
    }

    function setDisburseAmount(uint _new) public onlyOwner returns (bool){
        amountToDisburse = _new;
        return true;
    }

    function setClaimTime(uint _new) public onlyOwner returns (bool){
        claimTime = _new;
        return true;
    }

    function setMaxFarmers(uint _new) public onlyOwner returns (bool){
        maxFarmers = _new;
        return true;
    }
}
function end() public onlyOwner returns (bool){
    require(!ended, "Staking already ended");
    ended = true;
    return true;
}

//End the farming pool