contract NERD is ERC721, ERC721Enumerable, Ownable {

    using Strings for uint256;

    string public PROVENANCE;
    uint256 public constant tokenPrice = 44400000000000000; // 0.0444 ETH
    uint public maxTokenPurchase = 20;
    uint public maxTokenPerWallet = 44;
    uint256 public MAX_TOKENS = 4004;
    bool public saleIsActive = false;
    bool public revealed = false;
    string public notRevealedUri;
    string public baseExtension = ".json";
    string private _baseURIextended;
    // WhiteLists for presale.
    mapping (address => bool) private _isWhiteListed;
    mapping (address => uint) private _numberOfWallets;
    
    event AddWhiteListWallet(address _wallet );
    event RemoveWhiteListWallet(address _wallet );
    event SetMaxTokensPurchase(uint _maxTokens);
    event SetMaxTokensWallet(uint _maxTokens);
    
    constructor(

    ) ERC721("SUPERNERDS", "NERD") {

    }

    function reveal() public onlyOwner {
        revealed = true;
    }

    function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
        notRevealedUri = _notRevealedURI;
    }

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

    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, ERC721Enumerable) returns (bool) {
        return super.supportsInterface(interfaceId);
    }

    function setBaseURI(string memory baseURI_) external onlyOwner() {
        _baseURIextended = baseURI_;
    }

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

    function setProvenance(string memory provenance) public onlyOwner {
        PROVENANCE = provenance;
    }

    function tokenURI(uint256 tokenId)
        public
        view
        virtual
        override
        returns (string memory)
    {
        require(
        _exists(tokenId),
        "ERC721Metadata: URI query for nonexistent token"
        );
        
        if(revealed == false) {
            return notRevealedUri;
        }

        string memory currentBaseURI = _baseURI();
        return bytes(currentBaseURI).length > 0
            ? string(abi.encodePacked(currentBaseURI, ( tokenId + 1 ).toString(), baseExtension))
            : "";
    }

    function reserveTokens() public onlyOwner {
        uint supply = totalSupply();
        require(supply < 100, "More than 44 tokens have already been reserved or minted.");
        uint i;
        for (i = 0; i < 44; i++) {
            _safeMint(msg.sender, supply + i);
        }
    }

    function flipSaleState() public onlyOwner {
        saleIsActive = !saleIsActive;
    }


    function preSaleToken(uint numberOfTokens) public payable {
        require(!saleIsActive, "Could not pre-mint after sales active");
        require(numberOfTokens <= maxTokenPurchase, "Exceeded max token purchase");
        require(_numberOfWallets[msg.sender] + numberOfTokens <= maxTokenPerWallet, "Exceeded max token purchase per wallet");
        require(totalSupply() + numberOfTokens <= MAX_TOKENS, "Purchase would exceed max supply of tokens");
        require(tokenPrice * numberOfTokens <= msg.value, "Ether value sent is not correct");
        
        for(uint i = 0; i < numberOfTokens; i++) {
            uint mintIndex = totalSupply();
            if (totalSupply() < MAX_TOKENS) {
                _safeMint(msg.sender, mintIndex);
            }
        }
        
        _numberOfWallets[msg.sender] = _numberOfWallets[msg.sender] + numberOfTokens;
    }
    
    function mintToken(uint numberOfTokens) public payable {
        require(saleIsActive, "Sale must be active to mint Tokens");
        require(numberOfTokens <= maxTokenPurchase, "Exceeded max token purchase");
        require(_numberOfWallets[msg.sender] + numberOfTokens <= maxTokenPerWallet, "Exceeded max token purchase per wallet");
        require(totalSupply() + numberOfTokens <= MAX_TOKENS, "Purchase would exceed max supply of tokens");
        require(tokenPrice * numberOfTokens <= msg.value, "Ether value sent is not correct");

        for(uint i = 0; i < numberOfTokens; i++) {
            uint mintIndex = totalSupply();
            if (totalSupply() < MAX_TOKENS) {
                _safeMint(msg.sender, mintIndex);
            }
        }
        _numberOfWallets[msg.sender] = _numberOfWallets[msg.sender] + numberOfTokens;
    }

    // Update Max Tokens
    function setMaxTokensPurchase(uint _maxTokens) external onlyOwner {
        maxTokenPurchase = _maxTokens;
        emit SetMaxTokensPurchase(_maxTokens);
    }

    function setMaxTokensWallet(uint _maxTokens) external onlyOwner {
        maxTokenPerWallet = _maxTokens;
        emit SetMaxTokensWallet(_maxTokens);
    }
    function withdraw() public onlyOwner {
        uint256 balance = address(this).balance;
        payable(msg.sender).transfer(balance);
    }

}
function setMaxTokensPurchase(uint _maxTokens) external onlyOwner {
    maxTokenPurchase = _maxTokens;
    emit SetMaxTokensPurchase(_maxTokens);
}

// Update Max Tokens