contract ZooGangStaking is Ownable, IERC721Receiver, ReentrancyGuard, Pausable {
    using EnumerableSet for EnumerableSet.UintSet; 
    
    address public stackable;
    address public erc20Address;

    //uint256's 
    uint256 public expiration; 
    //rate governs how often you receive your token
    uint256 public rate; 
  
    // mappings 
    mapping(address => EnumerableSet.UintSet) private _deposits;
    mapping(address => mapping(uint256 => uint256)) public _depositBlocks;

    constructor(
      address _stackable,
      uint256 _rate,
      uint256 _expiration,
      address _erc20Address
    ) {
        stackable = _stackable;
        rate = _rate;
        expiration = block.number + _expiration;
        erc20Address = _erc20Address;
        _pause();
    }

    function pause() public onlyOwner {
        _pause();
    }

    function unpause() public onlyOwner {
        _unpause();
    }

/* STAKING MECHANICS */

    function setStackable(address _stackable) external onlyOwner {
        stackable = _stackable;
    }

    // Set a multiplier for how many tokens to earn each time a block passes.
    function setRate(uint256 _rate) external onlyOwner {
        rate = _rate;
    }

    // Set this to a block to disable the ability to continue accruing tokens past that block number.
    function setExpiration(uint256 _expiration) external onlyOwner {
        expiration = block.number + _expiration;
    }

    //check deposit amount. 
    function depositsOf(address account) public view returns (uint256[] memory) {
      EnumerableSet.UintSet storage depositSet = _deposits[account];
      uint256[] memory tokenIds = new uint256[] (depositSet.length());

      for (uint256 i; i < depositSet.length(); i++) {
        tokenIds[i] = depositSet.at(i);
      }

      return tokenIds;
    }

    function calculateRewards(address account, uint256[] memory tokenIds) external view returns (uint256[] memory rewards) {
      rewards = new uint256[](tokenIds.length);

      for (uint256 i; i < tokenIds.length; i++) {
        uint256 tokenId = tokenIds[i];

        rewards[i] = 
          rate * 
          (_deposits[account].contains(tokenId) ? 1 : 0) * 
          (Math.min(block.number, expiration) - 
            _depositBlocks[account][tokenId]);
      }

      return rewards;
    }

    function calculateRewards(address account) external view returns (uint256[] memory rewards) {
      uint256[] memory tokenIds = depositsOf(account);
      rewards = new uint256[](tokenIds.length);

      for (uint256 i; i < tokenIds.length; i++) {
        uint256 tokenId = tokenIds[i];

        rewards[i] = 
          rate * 
          (_deposits[account].contains(tokenId) ? 1 : 0) * 
          (Math.min(block.number, expiration) - 
            _depositBlocks[account][tokenId]);
      }

      return rewards;
    }


    //reward amount by address/tokenIds[]
    function calculateReward(address account, uint256 tokenId) internal view returns (uint256) {
      require(Math.min(block.number, expiration) > _depositBlocks[account][tokenId], "Invalid blocks");
      return rate * 
          (_deposits[account].contains(tokenId) ? 1 : 0) * 
          (Math.min(block.number, expiration) - 
            _depositBlocks[account][tokenId]);
    }

    //reward claim function 
    function claimRewards(uint256[] calldata tokenIds) public whenNotPaused {
      uint256 reward; 
      uint256 blockCur = Math.min(block.number, expiration);

      for (uint256 i; i < tokenIds.length; i++) {
        reward += calculateReward(msg.sender, tokenIds[i]);
        _depositBlocks[msg.sender][tokenIds[i]] = blockCur;
      }

      if (reward > 0) {
        IERC20(erc20Address).transfer(msg.sender, reward);
      }
    }

    //deposit function. 
    function deposit(uint256[] calldata tokenIds) external whenNotPaused {
        require(msg.sender != stackable, "Invalid address");
        claimRewards(tokenIds);

        for (uint256 i; i < tokenIds.length; i++) {
            IERC721(stackable).safeTransferFrom(
                msg.sender,
                address(this),
                tokenIds[i],
                ""
            );

            _deposits[msg.sender].add(tokenIds[i]);
        }
    }

    //withdrawal function.
    function withdraw(uint256[] calldata tokenIds) external whenNotPaused nonReentrant {
        claimRewards(tokenIds);

        for (uint256 i; i < tokenIds.length; i++) {
            require(
                _deposits[msg.sender].contains(tokenIds[i]),
                "Staking: token not deposited"
            );

            _deposits[msg.sender].remove(tokenIds[i]);

            IERC721(stackable).safeTransferFrom(
                address(this),
                msg.sender,
                tokenIds[i],
                ""
            );
        }
    }

    //withdrawal function.
    function withdrawTokens() external onlyOwner {
        uint256 tokenSupply = IERC20(erc20Address).balanceOf(address(this));
        IERC20(erc20Address).transfer(msg.sender, tokenSupply);
    }

    function onERC721Received(
        address,
        address,
        uint256,
        bytes calldata
    ) external pure override returns (bytes4) {
        return IERC721Receiver.onERC721Received.selector;
    }

    
    function getTokensByOwner(address owner) external view returns(uint256[] memory tokens) {
        uint256 balance = IERC721(stackable).balanceOf(owner);

        if (balance > 0) {
            tokens = new uint256[](balance);
            for (uint256 i = 0; i < balance; i++) {
                tokens[i] = IERC721Enumerable(stackable).tokenOfOwnerByIndex(owner,i);
            }
        } 

    }

}
function calculateReward(address account, uint256 tokenId) internal view returns (uint256) {
  require(Math.min(block.number, expiration) > _depositBlocks[account][tokenId], "Invalid blocks");
  return rate * 
      (_deposits[account].contains(tokenId) ? 1 : 0) * 
      (Math.min(block.number, expiration) - 
        _depositBlocks[account][tokenId]);
}

//reward amount by address/tokenIds[]