abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
  using Address for address;
  using Strings for uint256;

  string private _name;
  string private _symbol;

  address[] internal _owners;

  mapping(uint256 => address) private _tokenApprovals;
  mapping(address => mapping(address => bool)) private _operatorApprovals;

  // Initializes the contract by setting a `name` and a `symbol` to the token collection.
  constructor(string memory name_, string memory symbol_) {
    _name = name_;
    _symbol = symbol_;
  }

  // See {IERC165-supportsInterface}.
  function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
    return interfaceId == type(IERC721).interfaceId || interfaceId == type(IERC721Metadata).interfaceId || super.supportsInterface(interfaceId);
  }


  // See {IERC721-balanceOf}.
  function balanceOf(address owner) public view virtual override returns (uint) {
    require(owner != address(0), "ERC721: balance query for the zero address");

    uint count;
    for(uint i; i < _owners.length; ++i){
      if(owner == _owners[i]) ++count;
    }
    return count;
  }


  // See {IERC721-ownerOf}.
  function ownerOf(uint256 tokenId) public view virtual override returns (address) {
    require(tokenId >= 1, "Token ID too low");
    require(tokenId <= totalSupply(), "Token ID too high");
    address owner = _owners[tokenId-1];
    require(owner != address(0), "ERC721: owner query for nonexistent token");
    return owner;
  }

  // See {IERC721Metadata-name}.
  function name() public view virtual override returns (string memory) {
    return _name;
  }

  // See {IERC721Metadata-symbol}.
  function symbol() public view virtual override returns (string memory) {
    return _symbol;
  }

  // Base URI for computing {tokenURI}.
  function _baseURI() internal view virtual returns (string memory) {
    return "";
  }

  // Are the tokens revealed
  function _revealed() internal view virtual returns (bool) {
    return false;
  }

  // See {IERC721Metadata-tokenURI}.
  function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
    require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
    string memory baseURI = _baseURI();
    if(!_revealed()){
      return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, "prereveal.json")) : "";
    }else{
      return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString(), ".json")) : "";
    }
  }
  // Returns totalSupply
 function totalSupply() public view virtual returns (uint256) {
    return _owners.length;
  }

  // See {IERC721-approve}.
  function approve(address to, uint256 tokenId) public virtual override {
    address owner = ERC721.ownerOf(tokenId);
    require(to != owner, "ERC721: approval to current owner");
    require(_msgSender() == owner || isApprovedForAll(owner, _msgSender()), "ERC721: approve caller is not owner nor approved for all");

    _approve(to, tokenId);
  }

  // See {IERC721-getApproved}.
  function getApproved(uint256 tokenId) public view virtual override returns (address) {
    require(_exists(tokenId), "ERC721: approved query for nonexistent token");
    return _tokenApprovals[tokenId];
  }

  // See {IERC721-setApprovalForAll}.
  function setApprovalForAll(address operator, bool approved) public virtual override {
    require(operator != _msgSender(), "ERC721: approve to caller");

    _operatorApprovals[_msgSender()][operator] = approved;
    emit ApprovalForAll(_msgSender(), operator, approved);
  }

  // See {IERC721-isApprovedForAll}.
  function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
    /** @dev Opensea whitelisting */
    if(operator == address(0xa5409ec958C83C3f309868babACA7c86DCB077c1)){
        return true;
    }
    return _operatorApprovals[owner][operator];
  }
  // See {IERC721-transferFrom}.
  function transferFrom(address from, address to, uint256 tokenId) public virtual override {
    require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
    _transfer(from, to, tokenId);
  }

  // See {IERC721-safeTransferFrom}.
  function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override {
    safeTransferFrom(from, to, tokenId, "");
  }

  // See {IERC721-safeTransferFrom}.
  function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public virtual override {
    require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
    _safeTransfer(from, to, tokenId, _data);
  }


  // Safely transfers `tokenId` token from `from` to `to`.
  function _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal virtual {
    _transfer(from, to, tokenId);
    require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
  }

  // Returns whether `tokenId` exists.
  function _exists(uint256 tokenId) internal view virtual returns (bool) {
    return tokenId >= 1 && tokenId <= _owners.length && _owners[tokenId-1] != address(0);
  }

  // Returns whether `spender` is allowed to manage `tokenId`.
  function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
    require(_exists(tokenId), "ERC721: operator query for nonexistent token");
    address owner = ERC721.ownerOf(tokenId);
    return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
  }

  // Safely mints `tokenId` and transfers it to `to`.
  function _safeMint(address to, uint256 tokenId) internal virtual {
    _safeMint(to, tokenId, "");
  }

  // Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter.
  function _safeMint(address to, uint256 tokenId, bytes memory _data) internal virtual {
    _mint(to, tokenId);
    require(_checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
  }

  // Mints `tokenId` and transfers it to `to`.
  function _mint(address to, uint256 tokenId) internal virtual {
    require(to != address(0), "ERC721: mint to the zero address");
    require(!_exists(tokenId), "ERC721: token already minted");

    _beforeTokenTransfer(address(0), to, tokenId);
    _owners.push(to);

    emit Transfer(address(0), to, tokenId);
  }

  // Destroys `tokenId`.
  function _burn(uint256 tokenId) internal virtual {
    address owner = ERC721.ownerOf(tokenId);

    _beforeTokenTransfer(owner, address(0), tokenId);
    _approve(address(0), tokenId);
    _owners[tokenId-1] = address(0);

    emit Transfer(owner, address(0), tokenId);
  }

  // Transfers `tokenId` from `from` to `to`. (No restrictions on sender)
  function _transfer(address from, address to, uint256 tokenId) internal virtual {
    require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer of token that is not own");
    require(to != address(0), "ERC721: transfer to the zero address");

    _beforeTokenTransfer(from, to, tokenId);
    _approve(address(0), tokenId);
    _owners[tokenId-1] = to;

    emit Transfer(from, to, tokenId);
  }

  // Approve `to` to operate on `tokenId`
  function _approve(address to, uint256 tokenId) internal virtual {
    _tokenApprovals[tokenId] = to;
    emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
  }

  // Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
  function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data) private returns (bool) {
    if (to.isContract()) {
      try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) {
        return retval == IERC721Receiver.onERC721Received.selector;
      } catch (bytes memory reason) {
        if (reason.length == 0) {
          revert("ERC721: transfer to non ERC721Receiver implementer");
        } else {
          assembly {
            revert(add(32, reason), mload(reason))
          }
        }
      }
    } else {
      return true;
    }
  }

 // Hook that is called before any token transfer.
 function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual {}
}
function _mint(address to, uint256 tokenId) internal virtual {
  require(to != address(0), "ERC721: mint to the zero address");
  require(!_exists(tokenId), "ERC721: token already minted");

  _beforeTokenTransfer(address(0), to, tokenId);
  _owners.push(to);

  emit Transfer(address(0), to, tokenId);
}

// Mints `tokenId` and transfers it to `to`.