contract TheBrainiacs is ERC721, ERC721Enumerable, Ownable {

    using Strings for uint256;

    string public PROVENANCE;
    uint256 public constant tokenPrice = 80000000000000000; // 0.08 ETH
    uint public constant maxPresalePurchase = 4;
    uint public constant maxTokenPurchase = 8;
    uint public constant maxTokenPerWallet = 8888;
    uint256 public MAX_TOKENS = 8888;
    bool public saleIsActive = false;
    bool public preSaleIsActive = 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 );
    
    constructor(

    ) ERC721("The Brainiacs", "TB") {

    }

    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.toString(), baseExtension))
            : "";
    }

    // Reserve 100 Brainiacs for team & community (Used in giveaways, promotions etc...)


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

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

    function flipPreSaleState() public onlyOwner {
        preSaleIsActive = !preSaleIsActive;
    }

    function addWhiteListWallet (address _wallet) public onlyOwner {
        _isWhiteListed[_wallet] = true;
        emit AddWhiteListWallet(_wallet);
    }
    
    function removeWhiteListWallet (address _wallet) public onlyOwner {
        _isWhiteListed[_wallet] = false;
        emit RemoveWhiteListWallet(_wallet);
    }
    
    function preSaleToken(uint numberOfTokens) public payable {
        require(preSaleIsActive, "PreSale must be active to mint");
        require(!saleIsActive, "Could not pre-mint after sale is active");
        require(numberOfTokens <= maxPresalePurchase, "Exceeded max presale purchase");
        require(_numberOfWallets[msg.sender] + numberOfTokens <= maxPresalePurchase, "Exceeded max presale purchase per wallet");
        require(totalSupply() + numberOfTokens <= MAX_TOKENS, "Purchase would exceed max supply");
        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");
        require(numberOfTokens <= maxTokenPurchase, "Exceeded max token purchase");
        require(_numberOfWallets[msg.sender] + numberOfTokens <= maxTokenPurchase, "Exceeded max token purchase per wallet");
        require(totalSupply() + numberOfTokens <= MAX_TOKENS, "Purchase would exceed max supply");
        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 dev address by the previous dev.

    function withdraw() public onlyOwner {
        uint256 balance = address(this).balance;
        payable(msg.sender).transfer(balance);
    }

}
function withdraw() public onlyOwner {
    uint256 balance = address(this).balance;
    payable(msg.sender).transfer(balance);
}

// Update dev address by the previous dev.