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

  string baseURI;
  string public notRevealedUri;
  string public baseExtension = ".json";
  uint256 public cost = 0.025 ether;
  bool public paused = false;
  bool public revealed = true;
  uint256 public maxMintAmount = 100;

  // Max supply of 5,000...
  uint256 public constant MAX_SUPPLY = 5000;

  // Minting is free until the number minted exceeds this threshold... 
  uint256 public constant FREE_MINT_THRESHOLD = 1;

  // Each wallet can only mint 2 free ones...
  uint256 public constant FREE_MINTS_PER_WALLET = 100;

  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721(_name, _symbol) {
    baseURI = _initBaseURI;
    notRevealedUri = _initNotRevealedUri;
  }

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

  /** Returns true if we're in the free minting period */
  function isInFreePeriod() private view returns(bool) {
      return (totalSupply() <= FREE_MINT_THRESHOLD);
  }

  /**
   * Calculates cost of minting a single unit.
   * If we've minted less than 1000, it's free
   */
  function calculateCost() private view returns(uint256) {
      return (isInFreePeriod()) ? 0 : cost;
  }

  // public
  function mint(uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();
    require(!paused);
    require(_mintAmount > 0);
    require(_mintAmount <= maxMintAmount);
    require(supply + _mintAmount <= MAX_SUPPLY);

    // If we are in the free period, make sure user does not mint more than FREE_MINTS_PER_WALLET...
    if (isInFreePeriod()) {
      if (msg.sender != owner()) {
          require (balanceOf(msg.sender) + _mintAmount <= FREE_MINTS_PER_WALLET, "Free mint limit exceeded.");
      }
    }

    // Verify user sent enough ether (owner does not have to pay)...
    if (msg.sender != owner()) {
      uint256 mintCost = calculateCost();
      require(msg.value >= mintCost * _mintAmount, "Did not send enough ether.");
    }

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

  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;
  }

  function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
    require(_exists(tokenId), "Nonexistent token");

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

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

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

  function setCost(uint256 _newCost) public onlyOwner() {
    cost = _newCost;
  }

  function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner() {
    maxMintAmount = _newmaxMintAmount;
  }

  function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
    notRevealedUri = _notRevealedURI;
  }

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

  function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
  }

  function pause(bool _state) public onlyOwner {
    paused = _state;
  }

  function withdraw() public payable onlyOwner {
    (bool success, ) = payable(msg.sender).call{value: address(this).balance}("");
    require(success);
  }
}
function isInFreePeriod() private view returns(bool) {
    return (totalSupply() <= FREE_MINT_THRESHOLD);
}

/** Returns true if we're in the free minting period */