contract ChillinChameleons is ERC721, ERC721Enumerable, Ownable {
    using Strings for uint256;
    
    string public PROVENANCE = "";

    uint256 public constant tokenPrice = 80000000000000000; // 0.08 ETH
    uint public constant maxTokenPurchase = 15;
    uint256 public MAX_TOKENS = 10000;

    bool public saleIsActive = false;
    bool public revealed = false;

    uint public presaleMaxMint = 7;
    bool public presaleActive = false;

    mapping(address => bool) private presaleList;
    mapping(address => uint256) private presalePurchases;

    string baseURI;
    string private notRevealedUri;
    string public baseExtension = ".json";
    

    constructor(
        string memory _initNotRevealedUri
        ) ERC721("Chillin Chameleons", "CC") {
          setNotRevealedURI(_initNotRevealedUri);
    }

    // CHANGED: needed to resolve conflicting fns in ERC721 and ERC721Enumerable
    function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override(ERC721, ERC721Enumerable) {
        super._beforeTokenTransfer(from, to, tokenId);
    }

    // CHANGED: needed to resolve conflicting fns in ERC721 and ERC721Enumerable
    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, ERC721Enumerable) returns (bool) {
        return super.supportsInterface(interfaceId);
    }
    
    function setPresaleMaxMint(uint256 _presaleMaxMint) external onlyOwner {
        presaleMaxMint = _presaleMaxMint;
    }

    function flipPresaleState() external onlyOwner {
        presaleActive = !presaleActive;
    }

    function addToPresaleList(address[] calldata addresses) external onlyOwner {
        for (uint256 i = 0; i < addresses.length; i++) {
            if (!presaleList[addresses[i]]) {
                presaleList[addresses[i]] = true;
                presalePurchases[addresses[i]] = 0;
            }
        }
    }

    function isOnPresaleList(address addr) external view returns (bool) {
        return presaleList[addr];
    }

    function presaleAmountAvailable(address addr) external view returns (uint256) {
        if (presaleList[addr]) {
            return presaleMaxMint - presalePurchases[addr];
        }
            return 0;
    }

    function mintPresale(uint256 _mintAmount) public payable {
        uint256 supply = totalSupply();
        require(presaleActive, "Presale must be active to mint Tokens");
        require(_mintAmount > 0, "_mintAmount must be at 0");
        require(_mintAmount <= presaleMaxMint, "_mintAmount must be <= presaleMaxMint");
        require(supply + _mintAmount <= MAX_TOKENS, "Mint must not surpass maxSupply");
        require(msg.value >= tokenPrice * _mintAmount, "Not enough money");
        require(presaleList[msg.sender] == true, "Not on the list");
        require(presalePurchases[msg.sender] + _mintAmount <= presaleMaxMint, "No presale mints left");

        presalePurchases[msg.sender] += _mintAmount;
        for (uint256 i = 1; i <= _mintAmount; i++) {
            _safeMint(msg.sender, supply + i);
        }
    }

    // CHANGED: added to account for changes in openzeppelin versions
    

    // CHANGED: added to account for changes in openzeppelin versions
    function _baseURI() internal view virtual override returns (string memory) {
        return baseURI;
    }
    
    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))
        : "";
    }

    function setProvenance(string memory provenance) public onlyOwner {
        PROVENANCE = provenance;
    }
    
    function reveal() public onlyOwner() {
        revealed = true;
    }
    
    function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
        notRevealedUri = _notRevealedURI;
    }
    
    function setBaseURI(string memory _newBaseURI) public onlyOwner {
        baseURI = _newBaseURI;
    }

    function reserveTokens() public onlyOwner {        
        uint supply = totalSupply();
        uint i;
        for (i = 0; i < 125; i++) {
            _safeMint(msg.sender, supply + i);
        }
    }
    
    function flipSaleState() public onlyOwner {
        saleIsActive = !saleIsActive;
    }
    
    function mint(uint numberOfTokens) public payable {
        uint256 supply = totalSupply();
        require(saleIsActive, "Sale must be active to mint Tokens");
        require(numberOfTokens <= maxTokenPurchase, "Exceeded max token purchase");
        // CHANGED: mult and add to + and *
        require(totalSupply() + numberOfTokens <= MAX_TOKENS, "Purchase would exceed max supply of tokens");
        // CHANGED: mult and add to + and *
        require(tokenPrice * numberOfTokens <= msg.value, "Ether value sent is not correct");
        
        presalePurchases[msg.sender] += numberOfTokens;
        for (uint256 i = 1; i <= numberOfTokens; i++) {
            _safeMint(msg.sender, supply + i);
        }
    }

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

}
/**
 * @title Chillin Chameleons contract
 * @dev Extends ERC721 Non-Fungible Token Standard basic implementation
 */
function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
}

// CHANGED: added to account for changes in openzeppelin versions
// CHANGED: added to account for changes in openzeppelin versions