contract COOLGHOULS is ERC721Enumerable, Ownable {
    using Strings for uint256;

    string public baseURI = "ipfs://QmSbchkJWTzzhFAnX1AdqKoiXPcoJLBPFJHUvaW7Hyq6nn";
    uint256 public maxSupply = 5555;
    uint256 public maxMintPresale = 4;
    uint256 public maxMint = 10;
    uint256 public price = 0.05 ether;
    bool public active = true;
    bool public presale = true;
    address[] public whitelist;

    constructor() ERC721("Cool Ghouls", "CG") {
        mint(5);
    }

    // Internal Methods
    // Override
    function _baseURI() internal view virtual override returns (string memory) {
        return baseURI;
    }

    // Public Methods
    function mint(uint256 _mintAmount) public payable {
        require(active, "The contract is not active");
        uint256 supply = totalSupply();
        require(_mintAmount > 0, "Mint amount must be at least 1 NFT");
        require(supply + _mintAmount <= maxSupply, "Exceeded NFT supply");

        if (msg.sender != owner()) {
            if (presale == true) {
                require(isWhitelisted(msg.sender), "User is not whitelisted");
                require(_mintAmount <= maxMintPresale, "Exceeded max mint amount");
            } else {
                require(_mintAmount <= maxMint, "Exceeded max mint amount");
            }
            require(msg.value >= price * _mintAmount, "Insufficient funds");
        }

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

    // Override
    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
        string memory newBaseURI = _baseURI();
        return bytes(newBaseURI).length > 0 ? string(abi.encodePacked(newBaseURI, "/", tokenId.toString(), ".json")) : "";
    }

    function isWhitelisted(address _user) public view returns (bool) {
        for (uint i = 0; i < whitelist.length; i++) {
            if (whitelist[i] == _user) {
                return true;
            }
        }
        return false;
    }

    function getTokenIds(address _address) public view returns (uint256[] memory) {
        uint256 tokenCount = balanceOf(_address);
        uint256[] memory tokenIds = new uint256[](tokenCount);
        for (uint256 i; i < tokenCount; i++) {
            tokenIds[i] = tokenOfOwnerByIndex(_address, i);
        }
        return tokenIds;
    }

    // Owner methods
    function setBaseURI(string memory _newBaseURI) public onlyOwner {
        baseURI = _newBaseURI;
    }

    function setMaxSupply(uint256 _newMaxSupply) public onlyOwner {
        maxSupply = _newMaxSupply;
    }

    function setMaxMintPresale(uint256 _newMaxMintPresale) public onlyOwner {
        maxMintPresale = _newMaxMintPresale;
    }

    function setMaxMint(uint256 _newMaxMint) public onlyOwner {
        maxMint = _newMaxMint;
    }

    function setPrice(uint256 _newPrice) public onlyOwner {
        price = _newPrice;
    }

    function startMinting() public onlyOwner {
        active = true;
    }

    function stopMinting() public onlyOwner {
        active = false;
    }

    function startPresale() public onlyOwner {
        presale = true;
    }

    function stopPresale() public onlyOwner {
        presale = false;
    }

    function setWhitelist(address[] calldata _whitelist) public onlyOwner {
        delete whitelist;
        whitelist = _whitelist;
    }

    function withdraw() public payable onlyOwner {
        (bool success, ) = payable(owner()).call{value: address(this).balance}("");
        require(success);
    }
}
function setBaseURI(string memory _newBaseURI) public onlyOwner {
    baseURI = _newBaseURI;
}

// Owner methods