contract StonkSocietyNFT is ERC721, ERC721Enumerable, Pausable, Ownable {
    using Counters for Counters.Counter;
    using Strings for uint256;

    Counters.Counter private _tokenIdCounter;

    uint256 public maxSupply = 6969;
    uint256 public mintPrice = 0.169 ether;

    uint256 public maxOGMintPerTX = 10;
    uint256 public maxWLMintPerTX = 5;
    uint256 public maxPublicMintPerTX = 3;
    uint256 public maxTokensPerWallet = 20;

    bool public isOGStage = true;
    bool public isWLStage = false;
    bool public isPublicStage = false;

    bool public revealed = false;
    string private baseURI;
    string public unrevealedURL;

    mapping(address => bool) public isOG;
    mapping(address => bool) public isWL;

    constructor(
        address _owner,
        string memory _initBaseURI,
        string memory _initUnrevealedURL
    ) ERC721("Stonk Society", "SS") {
        setBaseURI(_initBaseURI);
        setUnrevealedURL(_initUnrevealedURL);
        transferOwnership(_owner);
    }

    // Public
    function mint(uint256 nTokens)
        public
        payable
        whenNotPaused
        notOverMaxSupply(nTokens)
        maxPerAddress(nTokens)
        stageChecks
        mintTXlimit(nTokens)
    {
        require(
            msg.value >= nTokens * mintPrice,
            "The sent value is less than the required price for minting this number of NFT(s)"
        );
        _multiSafeMint(_msgSender(), nTokens);
    }

    // Ownables
    function safeMint(address to) public notOverMaxSupply(1) onlyOwner {
        _iSafeMint(to);
    }

    function multiSafeMint(address to, uint256 nTokens)
        public
        notOverMaxSupply(nTokens)
        onlyOwner
    {
        _multiSafeMint(to, nTokens);
    }

    function pause() public onlyOwner {
        _pause();
    }

    function unpause() public onlyOwner {
        _unpause();
    }

    function addMultiOG(address[] memory addresses) public onlyOwner {
        for (uint256 idx = 0; idx < addresses.length; idx++) {
            _addOG(addresses[idx]);
        }
    }

    function removeMultiOG(address[] memory addresses) public onlyOwner {
        for (uint256 idx = 0; idx < addresses.length; idx++) {
            _removeOG(addresses[idx]);
        }
    }

    function addMultiWL(address[] memory addresses) public onlyOwner {
        for (uint256 idx = 0; idx < addresses.length; idx++) {
            _addWL(addresses[idx]);
        }
    }

    function removeMultiWL(address[] memory addresses) public onlyOwner {
        for (uint256 idx = 0; idx < addresses.length; idx++) {
            _removeWL(addresses[idx]);
        }
    }

    function toggleOGStage() public onlyOwner {
        isOGStage = !isOGStage;
    }

    function toggleWLStage() public onlyOwner {
        isWLStage = !isWLStage;
    }

    function togglePublicStage() public onlyOwner {
        isPublicStage = !isPublicStage;
    }

    function setMintPrice(uint256 newMintPrice) public onlyOwner {
        mintPrice = newMintPrice;
    }

    function setBaseURI(string memory newBaseURI) public onlyOwner {
        baseURI = newBaseURI;
    }

    function setUnrevealedURL(string memory newUnrevealedURL) public onlyOwner {
        unrevealedURL = newUnrevealedURL;
    }

    function setMaxOGMintPerTX(uint256 maxTokens) public onlyOwner {
        maxOGMintPerTX = maxTokens;
    }

    function setMaxWLMintPerTX(uint256 maxTokens) public onlyOwner {
        maxWLMintPerTX = maxTokens;
    }

    function setMaxPublicMintPerTX(uint256 maxTokens) public onlyOwner {
        maxPublicMintPerTX = maxTokens;
    }

    function setMaxTokensPerWallet(uint256 maxTokens) public onlyOwner {
        maxTokensPerWallet = maxTokens;
    }

    function reveal() public onlyOwner {
        revealed = true;
    }

    function withdraw() public onlyOwner {
        payable(owner()).transfer(address(this).balance);
    }

    // Internals
    function _iSafeMint(address to) internal {
        _tokenIdCounter.increment();
        uint256 tokenId = _tokenIdCounter.current();
        _safeMint(to, tokenId);
    }

    function _multiSafeMint(address to, uint256 _count) internal {
        require(_count > 0, "Mint count can not be zero");
        for (uint256 idx = 1; idx <= _count; idx++) {
            _iSafeMint(to);
        }
    }

    function _addOG(address _address) internal {
        isOG[_address] = true;
    }

    function _removeOG(address _address) internal {
        isOG[_address] = false;
    }

    function _addWL(address _address) internal {
        isWL[_address] = true;
    }

    function _removeWL(address _address) internal {
        isWL[_address] = false;
    }

    // Modefiers
    modifier notOverMaxSupply(uint256 _count) {
        require(
            _count + totalSupply() <= maxSupply,
            "Can not mint over the max supply of the tokens"
        );
        _;
    }
    modifier maxPerAddress(uint256 _count) {
        require(
            _count + balanceOf(_msgSender()) <= maxTokensPerWallet,
            "Can not mint over the max allowed tokens per address"
        );
        _;
    }
    modifier stageChecks() {
        if (isPublicStage) {} else if (isWLStage) {
            require(
                (isWL[_msgSender()] || isOG[_msgSender()]),
                "Only whitelisted and OG addresses are allowed to mint at this stage"
            );
        } else if (isOGStage) {
            require(
                isOG[_msgSender()],
                "Only OG addresses are allowed to mint at this stage"
            );
        }
        _;
    }

    modifier mintTXlimit(uint256 _nTokens) {
        if (isOG[_msgSender()]) {
            require(
                _nTokens <= maxOGMintPerTX,
                "Unable to mint over the limit per transaction for OG"
            );
        } else if (isWL[_msgSender()]) {
            require(
                _nTokens <= maxWLMintPerTX,
                "Unable to mint over the limit per transaction for WL"
            );
        } else {
            require(
                _nTokens <= maxPublicMintPerTX,
                "Unable to mint over the limit per transaction for non-OG & non-WL"
            );
        }
        _;
    }

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

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

        return
            bytes(baseURI).length > 0
                ? string(abi.encodePacked(baseURI, tokenId.toString(), ".json"))
                : "";
    }

    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal override(ERC721, ERC721Enumerable) whenNotPaused {
        super._beforeTokenTransfer(from, to, tokenId);
    }

    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721, ERC721Enumerable)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
}
function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721)
        returns (string memory)
    {
        require(
            _exists(tokenId),
            "ERC721Metadata: URI query for nonexistent token"
        );

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

        return
            bytes(baseURI).length > 0
                ? string(abi.encodePacked(baseURI, tokenId.toString(), ".json"))
                : "";
    }
// Overrides