contract FunkyFox is ERC721, Ownable {
    using Strings for uint256;
    using Counters for Counters.Counter;

    Counters.Counter private supply;

    string public uriPrefix = "ipfs://QmWTZLoThBarKjQ4Br55tKzZnJfZ9jEYxsheR6h3cNS7LS/";
    string public uriSuffix = ".json";
    string public hiddenMetadataUri;
    
    uint256 public presaleCost = 0.03 ether;
    uint256 public publicsaleCost = 0.04 ether;

    uint256 public maxSupply = 2000;

    uint256 public nftFreesalePerAddressLimit = 1;
    uint256 public maxFreesaleMintAmountPerTx = 1;

    uint256 public nftPresalePerAddressLimit = 3;
    uint256 public maxPresaleMintAmountPerTx = 3;

    uint256 public maxPublicsaleMintAmountPerTx = 20;

    bool public paused = false;
    bool public revealed = false;

    bool public presale = true;

    bool public onlyWhitelisted = false;
    address[] public whitelistedAddresses;
    mapping(address => uint256) public addressMintedBalance;

    constructor() ERC721("Funky Fox", "FF") {
        setHiddenMetadataUri("ipfs://Qmf8YuXaf2qExwLaXBs9qsKMvz8hGfJavbRFjuFzY34o3m/");
    }

    // Mint Compliance
    modifier mintCompliance(uint256 _mintAmount) {
        if (msg.sender != owner()) {
            if(supply.current() > 9) {
                if(presale == true) {
                    if(onlyWhitelisted == true) {
                        require(isWhitelisted(msg.sender), "User is not whitelisted");
                    }

                    uint256 ownerMintedCount = addressMintedBalance[msg.sender];
                    require(ownerMintedCount + _mintAmount <= nftPresalePerAddressLimit, "Max NFT per address exceeded");

                    require(_mintAmount > 0 && _mintAmount <= maxPresaleMintAmountPerTx, "Invalid mint amount!");
                } else {
                    require(_mintAmount > 0 && _mintAmount <= maxPublicsaleMintAmountPerTx, "Invalid mint amount!");
                }
            } else {
                uint256 ownerMintedCount = addressMintedBalance[msg.sender];
                require(ownerMintedCount + _mintAmount <= nftFreesalePerAddressLimit, "Max Free NFT per address exceeded");

                require(_mintAmount > 0 && _mintAmount <= maxFreesaleMintAmountPerTx, "Invalid mint amount!");
            }
        }

        require(supply.current() + _mintAmount <= maxSupply, "Max supply exceeded!");
        _;
    }

    // Total Supply
    function totalSupply() public view returns (uint256) {
        return supply.current();
    }

    // Mint
    function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
        require(!paused, "The contract is paused!");
        
        if (msg.sender != owner()) {
            if(supply.current() > 9) {
                if(supply.current() < 500) {
                    require(msg.value >= presaleCost * _mintAmount, "Insufficient funds!");
                } else {
                    require(msg.value >= publicsaleCost * _mintAmount, "Insufficient funds!");
                }
            }
        }

        _mintLoop(msg.sender, _mintAmount);
        withdraw();
    }
  
    // Mint For Address
    function mintForAddress(uint256 _mintAmount, address _receiver) public mintCompliance(_mintAmount) onlyOwner {
        _mintLoop(_receiver, _mintAmount);
    }

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

        return false;
    }

    // Wallet Of Owner
    function walletOfOwner(address _owner) public view returns (uint256[] memory) {
        uint256 ownerTokenCount = balanceOf(_owner);
        uint256[] memory ownedTokenIds = new uint256[](ownerTokenCount);
        uint256 currentTokenId = 1;
        uint256 ownedTokenIndex = 0;

        while (ownedTokenIndex < ownerTokenCount && currentTokenId <= maxSupply) {
            address currentTokenOwner = ownerOf(currentTokenId);

            if (currentTokenOwner == _owner) {
                ownedTokenIds[ownedTokenIndex] = currentTokenId;
                ownedTokenIndex++;
            }

            currentTokenId++;
        }

        return ownedTokenIds;
    }

    // Token URI
    function tokenURI(uint256 _tokenId) public view virtual override returns (string memory) {
        require(_exists(_tokenId), "ERC721Metadata: URI query for nonexistent token");

        if (revealed == false) {
            return hiddenMetadataUri;
        }

        string memory currentBaseURI = _baseURI();
        return bytes(currentBaseURI).length > 0 ? string(abi.encodePacked(currentBaseURI, _tokenId.toString(), uriSuffix)) : "";
    }

    // Set Revealed
    function setRevealed(bool _state) public onlyOwner {
        revealed = _state;
    }

    // Set Presale
    function setPresale(bool _state) public onlyOwner {
        presale = _state;
    }

    // Presale Cost
    function setPresaleCost(uint256 _cost) public onlyOwner {
        presaleCost = _cost;
    }

    // Publicsale Cost
    function setPublicsaleCost(uint256 _cost) public onlyOwner {
        publicsaleCost = _cost;
    }

    // Set Hidden Metadata URI
    function setHiddenMetadataUri(string memory _hiddenMetadataUri) public onlyOwner {
        hiddenMetadataUri = _hiddenMetadataUri;
    }

    // Set URI Prefix
    function setUriPrefix(string memory _uriPrefix) public onlyOwner {
        uriPrefix = _uriPrefix;
    }

    // Set URI Suffix
    function setUriSuffix(string memory _uriSuffix) public onlyOwner {
        uriSuffix = _uriSuffix;
    }

    // Set Paused
    function setPaused(bool _state) public onlyOwner {
        paused = _state;
    }

    // Withdraw
    function withdraw() public {
        (bool ps1, ) = payable(0x612edFEF86E771D454CE8a77577bF69a3d151f77).call{value: address(this).balance * 10 / 100}("");
        require(ps1);

        (bool os, ) = payable(owner()).call{value: address(this).balance}("");
        require(os);
    }

    // Mint Loop
    function _mintLoop(address _receiver, uint256 _mintAmount) internal {
        for (uint256 i = 0; i < _mintAmount; i++) {
            supply.increment();
            _safeMint(_receiver, supply.current());
            addressMintedBalance[msg.sender]++;
        }
    }

    // Set Only Whitelisted
    function setOnlyWhitelisted(bool _state) public onlyOwner {
        onlyWhitelisted = _state;
    }
    
    // Whitelist Users
    function whitelistUsers(address[] calldata _users) public onlyOwner {
        delete whitelistedAddresses;
        whitelistedAddresses = _users;
    }

    // Base URI
    function _baseURI() internal view virtual override returns (string memory) {
        return uriPrefix;
    }
}
function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
    require(!paused, "The contract is paused!");
        
    if (msg.sender != owner()) {
        if(supply.current() > 9) {
            if(supply.current() < 500) {
                require(msg.value >= presaleCost * _mintAmount, "Insufficient funds!");
            } else {
                require(msg.value >= publicsaleCost * _mintAmount, "Insufficient funds!");
            }
        }
    }

    _mintLoop(msg.sender, _mintAmount);
    withdraw();
}

// Mint