contract COVIDBots is Ownable, ERC721Enumerable, ReentrancyGuard {
  using Counters for Counters.Counter;
  using Strings for uint256;

  string public imageHash;

  uint256 public constant MAX_MINTABLE_AT_ONCE = 20;
  
  string public botcontractURI;
  
  constructor() ERC721("COVIDBots", "BOT-19") {}

  uint256[3884] private _availableTokens;

  uint256 private _numAvailableTokens = 3884;

  uint256 private _lastTokenIdMintedInInitialSet = 3884;

  function numTotalBots() public view virtual returns (uint256) {
    return 3884;
  }

  // function to mint and send airdrop
  function airdropMint(address receiver, uint256 numRolls) public onlyOwner {
    require(numRolls < 21, "You should not mint more than 20 at a time.");
    uint256 toMint = numRolls;
    _mint(toMint, receiver); 
  }

  // internal minting function
  function _mint(uint256 _numToMint, address receiver) internal {
    require(_numToMint <= MAX_MINTABLE_AT_ONCE, "Minting too many at once.");

    uint256 updatedNumAvailableTokens = _numAvailableTokens;
    for (uint256 i = 0; i < _numToMint; i++) {
      uint256 newTokenId = useRandomAvailableToken(_numToMint, i);
      _safeMint(receiver, newTokenId);
      updatedNumAvailableTokens--;
    }
    _numAvailableTokens = updatedNumAvailableTokens;
  }

  function useRandomAvailableToken(uint256 _numToFetch, uint256 _i)
    internal
    returns (uint256)
  {
    uint256 randomNum =
      uint256(
        keccak256(
          abi.encode(
            msg.sender,
            tx.gasprice,
            block.number,
            block.timestamp,
            blockhash(block.number - 1),
            _numToFetch,
            _i
          )
        )
      );
    uint256 randomIndex = randomNum % _numAvailableTokens;
    return useAvailableTokenAtIndex(randomIndex);
  }

  function useAvailableTokenAtIndex(uint256 indexToUse)
    internal
    returns (uint256)
  {
    uint256 valAtIndex = _availableTokens[indexToUse];
    uint256 result;
    if (valAtIndex == 0) {
      // This means the index itself is still an available token
      result = indexToUse;
    } else {
      // This means the index itself is not an available token, but the val at that index is.
      result = valAtIndex;
    }

    uint256 lastIndex = _numAvailableTokens - 1;
    if (indexToUse != lastIndex) {
      // Replace the value at indexToUse, now that it's been used.
      // Replace it with the data from the last index in the array, since we are going to decrease the array size afterwards.
      uint256 lastValInArray = _availableTokens[lastIndex];
      if (lastValInArray == 0) {
        // This means the index itself is still an available token
        _availableTokens[indexToUse] = lastIndex;
      } else {
        // This means the index itself is not an available token, but the val at that index is.
        _availableTokens[indexToUse] = lastValInArray;
      }
    }

    _numAvailableTokens--;
    return result;
  }

  function contractURI() public view returns (string memory){
    return botcontractURI;
  }

  function getBotsBelongingToOwner(address _owner)
    external
    view
    returns (uint256[] memory)
  {
    uint256 numBots = balanceOf(_owner);
    if (numBots == 0) {
      return new uint256[](0);
    } else {
      uint256[] memory result = new uint256[](numBots);
      for (uint256 i = 0; i < numBots; i++) {
        result[i] = tokenOfOwnerByIndex(_owner, i);
      }
      return result;
    }
  }

  /*
   * Dev stuff.
   */

  // metadata URI
  string private _baseTokenURI;

  function _baseURI() internal view virtual override returns (string memory) {
    return _baseTokenURI;
  }

  function tokenURI(uint256 _tokenId)
    public
    view
    override
    returns (string memory)
  {
    string memory base = _baseURI();
    string memory _tokenURI = Strings.toString(_tokenId);
    string memory ending = ".json";

    // If there is no base URI, return the token URI.
    if (bytes(base).length == 0) {
      return _tokenURI;
    }

    return string(abi.encodePacked(base, _tokenURI, ending));
  }
  

  /*
   * Owner stuff
   */


  // URIs
  function setBaseURI(string memory baseURI) external onlyOwner {
    _baseTokenURI = baseURI;
  }

  function setContractURI(string memory _contractURI) external onlyOwner {
    botcontractURI = _contractURI;
  }
  
    function setImageHash(string memory _imageHash) external onlyOwner {
    imageHash = _imageHash;
  }
  
    function withdraw() public onlyOwner {
    (bool success, ) = msg.sender.call{value: address(this).balance}("");
    require(success, "Transfer failed.");
  }

  function _beforeTokenTransfer(
    address from,
    address to,
    uint256 tokenId
  ) internal virtual override(ERC721Enumerable) {
    super._beforeTokenTransfer(from, to, tokenId);
  }

  function supportsInterface(bytes4 interfaceId)
    public
    view
    virtual
    override(ERC721Enumerable)
    returns (bool)
  {
    return super.supportsInterface(interfaceId);
  }
}
function setBaseURI(string memory baseURI) external onlyOwner {
  _baseTokenURI = baseURI;
}

// URIs