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

  Counters.Counter private supply;

  string public uriPrefix = "ipfs://QmZbtZzmUWkWQNtDFmtYSNbtKUThM38xwyRgkXtsJaPCAv/";
  string public uriSuffix = ".json";
  string public hiddenMetadataUri;
  
  uint256 public cost = 0.111111 ether;
  uint256 public maxSupply = 383;
  uint256 public maxMintAmountPerTx = 1;

  bool public paused = true;
  bool public revealed = true;

//basic Internal functions
  function _mintLoop(address _receiver, uint256 _mintAmount) internal {
    for (uint256 i = 0; i < _mintAmount; i++) {
      supply.increment();
      _safeMint(_receiver, supply.current());
    }
  }

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

//constructor, Set hidden data URL 
  constructor() ERC721("Leyens Vertical Collection", "LYNSV") {
    setHiddenMetadataUri("ipfs://QmQ6Dc8KYUsbFb6Fgxhg5UjPUpc8vQz7eURq6kc2DaJZ1c/hidden.json");
  }

//check is mint is allowed
  modifier mintCompliance(uint256 _mintAmount) {
    require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid Mint Amount! Max of 2 is allowed per Tx.");
    require(supply.current() + _mintAmount <= maxSupply, "Max Leyen supply exceeded!");
    _;
  }

//total supply
  function totalSupply() public view returns (uint256) {
    return supply.current();
  }

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

    _mintLoop(msg.sender, _mintAmount);
  }
  
//mint token4address
  function mintForAddress(uint256 _mintAmount, address _receiver) public mintCompliance(_mintAmount) onlyOwner {
    _mintLoop(_receiver, _mintAmount);
  }

//get token owner
  function walletOfOwner(address _owner)
    public
    view
    returns (uint256[] memory)
  {
    uint256 ownerTokenCount = balanceOf(_owner);
    uint256[] memory ownedTokenIds = new uint256[](ownerTokenCount);
    uint256 currentTokenId = 0;
    uint256 ownedTokenIndex = 0;

    while (ownedTokenIndex < ownerTokenCount && currentTokenId <= maxSupply) {
      address currentTokenOwner = ownerOf(currentTokenId);

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

        ownedTokenIndex++;
      }

      currentTokenId++;
    }

    return ownedTokenIds;
  }

//returns token URI IPFS Data
  function tokenURI(uint256 _tokenId)
    public
    view
    virtual
    override
    returns (string memory)
  {
    require(
      _exists(_tokenId),
      "ERC721Metadata: Token URI query does not exist."
    );

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

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

//set reveal state. true - revealed to public, false- show hidden URI 
  function setRevealed(bool _state) public onlyOwner {
    revealed = _state;
  }

//change cost to mint token
  function setCost(uint256 _cost) public onlyOwner {
    cost = _cost;
  }

//set amount of mints per tx, don't make this too high.
  function setMaxMintAmountPerTx(uint256 _maxMintAmountPerTx) public onlyOwner {
    maxMintAmountPerTx = _maxMintAmountPerTx;
  }

//set hidden uri data
  function setHiddenMetadataUri(string memory _hiddenMetadataUri) public onlyOwner {
    hiddenMetadataUri = _hiddenMetadataUri;
  }

//change full collection IPFS info
  function setUriPrefix(string memory _uriPrefix) public onlyOwner {
    uriPrefix = _uriPrefix;
  }

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

//pause contract, will not allow mints, true == paused, false == ready to mint
  function pauseContract(bool _state) public onlyOwner {
    paused = _state;
  }

//withdrawal sale costs to owner address
  function withdraw() public onlyOwner {
    // Transfer to Collection owner
    (bool os, ) = payable(owner()).call{value: address(this).balance}("");
    require(os);
    // =============================================================================
  }

}
function tokenURI(uint256 _tokenId)
  public
  view
  virtual
  override
  returns (string memory)
{
  require(
    _exists(_tokenId),
    "ERC721Metadata: Token URI query does not exist."
  );

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

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

//returns token URI IPFS Data