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

    Counters.Counter private supply;
    string public uriPrefix = "";
    string public uriSuffix = ".json";
    string public hiddenMetadataUri;
  
    uint256 public cost = 0.07 ether;
    uint256 public maxCapsules = 5000;
    uint256 public maxMintAmount = 3;

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

    constructor() ERC721("CapsuleCorp.", "CPSL") {
        setHiddenMetadataUri("https://gateway.pinata.cloud/ipfs/Qmbw4CDKs5exCFrXsmQZX4DrkwEQ6QEjke3unXt5ibC5Ro/hidden.json");
    }
    
    // Minting requirements
    modifier mintCompliance(uint256 _mintAmount) {
        require(_mintAmount > 0 && _mintAmount <= maxMintAmount, "Invalid amount!");
        require(supply.current() + _mintAmount <= maxCapsules, "Exceeded total Supply!");
        _;
    }

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

    // Minting process
    function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
        require(!paused, "The contract is paused!");
        require(msg.value >= cost * _mintAmount, "Insufficient funds!");

        _mintLoop(msg.sender, _mintAmount);
    }
    
    // Used to reserve capsules for owner once called
    function _mintSingleCapsule() private {
        uint newTokenID = supply.current();
        _safeMint(msg.sender, newTokenID);
        supply.increment();
    }

    // Mint for a provided address
    function mintForAddress(uint256 _mintAmount, address _receiver) public mintCompliance(_mintAmount) onlyOwner {
        _mintLoop(_receiver, _mintAmount);
    }
    
    // Mints up to 3 capsules
    function _mintLoop(address _receiver, uint256 _mintAmount) internal {
        for (uint256 i = 0; i < _mintAmount; i++) {
            supply.increment();
            _safeMint(_receiver, supply.current());
        }
    }
    
    // Reserves 10 capsules for owner
    function reserveCapsules() public onlyOwner {
        uint totalMinted = supply.current();
        require(
            totalMinted.add(10) < maxCapsules, "Not enough Capsules"
        );
        for (uint i = 0; i < 10; i++) {
            _mintSingleCapsule();
        }
    }

    function _baseURI() internal view virtual override returns (string memory) {
        return uriPrefix;
    }

    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 <= maxCapsules) {
            address currentTokenOwner = ownerOf(currentTokenId);

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

            ownedTokenIndex++;
        } 
        currentTokenId++;
    }
        return ownedTokenIds;
    }

    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))
        : "";
    }

    // Reveals metadata after minting if set to true
    function setRevealed(bool _state) public onlyOwner {
        revealed = _state;
    }

    // Cost to mint without gas fees
    function setCost(uint256 _cost) public onlyOwner {
        cost = _cost;
    }

    // Sets the max mint amount per address
    function setMaxMintAmount(uint256 _maxMintAmount) public onlyOwner {
        maxMintAmount = _maxMintAmount;
    }

    // MetadataURI setup
    function setHiddenMetadataUri(string memory _hiddenMetadataUri) public onlyOwner {
        hiddenMetadataUri = _hiddenMetadataUri;
    }

    function setUriPrefix(string memory _uriPrefix) public onlyOwner {
        uriPrefix = _uriPrefix;
    }

    function setUriSuffix(string memory _uriSuffix) public onlyOwner {
        uriSuffix = _uriSuffix;
    }

    // Set sale to pause
    function setPaused(bool _state) public onlyOwner {
        paused = _state;    
    }

    // This will transfer the remaining contract balance to the owner.
    function withdraw() public onlyOwner {
        (bool os, ) = payable(owner()).call{value: address(this).balance}("");
        require(os);
    }
}
//////////////////////////////////////////////////
function setMaxMintAmount(uint256 _maxMintAmount) public onlyOwner {
    maxMintAmount = _maxMintAmount;
}

// Sets the max mint amount per address