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

  string private baseURI;
  string public baseExtension = ".json";
  uint256 public cost = 0.134 ether;
  uint256 public maxSupply = 10428;
  uint256 public maxMintAmount = 20;
  uint256 public maxMintWalletAmount = 20;
  bool public paused = true;
  bool public onlyWhitelisted = false;
  address[] public whitelistedAddresses;
  mapping(address => uint256) public addressMintedBalance;
  string public notRevealedUri;
  bool public revealed = false;

  constructor() ERC721("The Official LoopTroop Lunatics", "LTL") {
    setBaseURI("https://ipfs.io/ipfs/QmY5dpRJGZkAZeJbtWh78YPH4Zk3rJaBRiQZDPot2X7WHc/");
    setNotRevealedURI("https://ipfs.io/ipfs/QmQ4HQQMcedYJqnzqsH5VNwUwGCs3Nhyc6QYAXSpCM9Ky3/hidden.json");
    mint(10);
  }

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

  // public
  function mint(uint256 _mintAmount) public payable {
       uint256 supply = totalSupply();
    if (msg.sender != owner()) {
        require(_mintAmount > 0, "Need to mint at least 1 NFT");
        require(!paused, "The contract is paused or the sale is not active");
        require(_mintAmount <= maxMintAmount, "Max mint amount per session exceeded");
        require(supply + _mintAmount <= maxSupply, "Max NFT limit exceeded");
        uint256 mintedAmount = addressMintedBalance[msg.sender];
        require(mintedAmount + _mintAmount <= maxMintWalletAmount, "Max NFT per wallet limit exceeded");
        if(onlyWhitelisted == true) {
            require(isWhitelisted(msg.sender), "User is not whitelisted");
        }
        require(msg.value >= cost * _mintAmount, "insufficient funds");
    }
    
    for (uint256 i = 1; i <= _mintAmount; i++) {
        addressMintedBalance[msg.sender]++;
        _safeMint(msg.sender, supply + i);
    }
  }
  
  function isWhitelisted(address _user) public view returns (bool) {
    for (uint i = 0; i < whitelistedAddresses.length; i++) {
      if (whitelistedAddresses[i] == _user) {
          return true;
      }
    }
    return false;
  }

  function reveal(bool _reveal) public onlyOwner() {
      revealed = _reveal;
  }

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

  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),
      "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))
        : "";
  }
  
  function setCost(uint256 _newCost) public onlyOwner {
    cost = _newCost;
  }

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

  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 setOnlyWhitelisted(bool _state) public onlyOwner {
    onlyWhitelisted = _state;
  }
  
  function whitelistUsers(address[] calldata _users) public onlyOwner {
    delete whitelistedAddresses;
    whitelistedAddresses = _users;
  }
 
  function withdraw() public payable onlyOwner {
    require(address(this).balance > 0, "Balance is 0");
    payable(owner()).transfer(address(this).balance);
  }
}
function _baseURI() internal view virtual override returns (string memory) {
  return baseURI;
}

// internal