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

    string public baseURI;
    string public baseExtension = ".json";
    string public notRevealedUri;
    uint256 public cost = 0.08 ether;
    uint256 public maxSupply = 5000;
    uint256 public maxMintAmount = 20;
    uint256 public nftPerAddressLimit = 3;
    bool public paused = false;
    bool public revealed = false;
    bool public onlyWhitelisted = true;
    address[] public whitelistedAddresses;
    mapping(address => uint256) public addressMintedBalance;

    constructor(
        string memory _name,
        string memory _symbol,
        string memory _initBaseURI,
        string memory _initNotRevealedUri
    ) ERC721(_name, _symbol) {
        setBaseURI(_initBaseURI);
        setNotRevealedURI(_initNotRevealedUri);
        mint(msg.sender, 5);
    }

    /// @notice Function to return the baseURI
    /// @dev Explicitly override function, as we inherit from Open Zeppelin. See docs (https://docs.soliditylang.org/en/v0.6.0/contracts.html#index-17)
    /// @return notRevealedURI for early whitelisted minters
    function _baseURI() internal view virtual override returns (string memory) {
        return baseURI;
    }

    /// @notice Function to mint non-fungible tokens, pause contract, check amount minted is > 0, and doesn't exceed the maxMintAmount or maxSupply
    /// @param _mintAmount tracks number of NFTs minted
    function mint(address _to, uint256 _mintAmount) public payable {
        require(!paused, "the contract is paused");
        uint256 supply = totalSupply();
        require(_mintAmount > 0, "need to mint at least 1 NFT");
        require(
            _mintAmount <= maxMintAmount,
            "max mint amount per session exceeded"
        );
        require(supply + _mintAmount <= maxSupply, "max NFT limit exceeded");

        /// @notice Minter should be whitelisted (allowed to mint), otherwise forbid sender from minting
        if (msg.sender != owner()) {
            if (onlyWhitelisted == true) {
                require(isWhitelisted(msg.sender), "user is not whitelisted");
                uint256 ownerMintedCount = addressMintedBalance[msg.sender];
                require(
                    ownerMintedCount + _mintAmount <= nftPerAddressLimit,
                    "max NFT per address exceeded"
                );
            }
            require(msg.value >= cost * _mintAmount, "insufficient funds");
        }

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

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

    /// @notice we keep track of tokenIds owner possesses
    /// @dev tokenIds are kept in memory for efficiency
    /// @return tokenIds of wallet owner
    function walletOfOwner(address _owner)
        public
        view
        returns (uint256[] memory)
    {
        uint256 ownerTokenCount = balanceOf(_owner);
        uint256[] memory tokenIds = new uint256[](ownerTokenCount);
        for (uint256 i; i < ownerTokenCount; i++) {
            tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
        }
        return tokenIds;
    }

    /// @notice we require a token Id to exist
    /// @dev if contract is not revealed, show temporary uniform resource identifier
    /// @return current base uniform resource identifier, or return nothing
    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
                    )
                )
                : "";
    }

    /// @notice functions permissible to call only by contract owner
    function reveal() public onlyOwner {
        revealed = true;
    }

    /// @notice only owner can set max number of Superkids per address
    function setNftPerAddressLimit(uint256 _limit) public onlyOwner {
        nftPerAddressLimit = _limit;
    }

    /// @notice only owner can set new cost of Superkids
    function setCost(uint256 _newCost) public onlyOwner {
        cost = _newCost;
    }

    /// @notice only owner can set maximum amount wallet owner may mint
    function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
        maxMintAmount = _newmaxMintAmount;
    }

    /// @notice only owner can set a new base uniform resource identifier
    function setBaseURI(string memory _newBaseURI) public onlyOwner {
        baseURI = _newBaseURI;
    }

    /// @notice only owner can set a new base extension for our metadata
    function setBaseExtension(string memory _newBaseExtension)
        public
        onlyOwner
    {
        baseExtension = _newBaseExtension;
    }

    /// @notice only owner can set a new temporary uniform resource identifier
    function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
        notRevealedUri = _notRevealedURI;
    }

    /// @notice only owner can pause our contract
    function pause(bool _state) public onlyOwner {
        paused = _state;
    }

    /// @notice only owner can add whitelisted wallet addresses
    function setOnlyWhitelisted(bool _state) public onlyOwner {
        onlyWhitelisted = _state;
    }

    /// @notice only owner can remove whitelisted wallet addresses
    function whitelistUsers(address[] calldata _users) public onlyOwner {
        delete whitelistedAddresses;
        whitelistedAddresses = _users;
    }

    /// @notice only owner can withdraw ether from contract
    function withdraw() public payable onlyOwner {
        /// @notice Donate 10% to Assemble Pittsburgh
        (bool assemble, ) = payable(0xc1353fE8bd78aE6f5449da47203523D2edEaCc59)
            .call{value: (address(this).balance * 10) / 100}("");
        require(assemble);

        /// @notice Donate 10% to Black Girls Code
        (bool blackGirls, ) = payable(
            0x11708796f919758A0A0Af6B801E8F45382D5E2F3
        ).call{value: (address(this).balance * 10) / 100}("");
        require(blackGirls);

        /// @notice Donate 10% to Hack Club
        (bool hackClub, ) = payable(0xdC425B61f1d3E753AE2a915f2a146B18A34B388a)
            .call{value: (address(this).balance * 10) / 100}("");
        require(hackClub);

        (bool supergirls, ) = payable(
            0x7757aCa28c46526948C631AE1E6170a6C67b16ea
        ).call{value: address(this).balance}("");
        require(supergirls);
    }
}
/// @title Supergirls. 5,000 students from different Houses at Numinous Park work together to solve hard problems in a virtual universe
/// @author DOSWΞLL.ΞTH (David Oliver Doswell, II)
/// @notice Simple contract to mint NFTs to promote STEM in K-12
/// @dev All function calls are currently implemented without side effects
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
                )
            )
            : "";
}

/// @notice we require a token Id to exist
/// @dev if contract is not revealed, show temporary uniform resource identifier
/// @return current base uniform resource identifier, or return nothing