contract NyanFrndz is ERC721A, Ownable {
  using Strings for uint256;

  string public baseURI = "https://fd89784e59c72499525556f80289b2c7.nyanfrndz.xyz/";
  string public baseExtension = ".json";

  uint256 public cost = .015 ether;
  uint256 public maxSupply = 3333;

  bool public preSales = true;

  uint256 public freeMintMaxSupply = 333;

  uint256 private maxMintAmount = 10;
  
  bool public paused = true;

  uint256 public nftPerAddressLimit = 3;
  mapping(address => uint256) public addressMintedBalance;

  address[] public whitelistedAddresses;
  bool public enableWhitelist = false;

  bool public reveal = false;

  constructor(
    string memory _name,
    string memory _symbol
  ) ERC721A(_name, _symbol, maxMintAmount) {
  }

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

  modifier isSafeMint(uint256 _mintAmount){
    require(_mintAmount > 0, "need to mint at least 1 NFT");
    require(_mintAmount <= maxMintAmount, "max mint amount per session exceeded");

    uint256 supply = totalSupply();

    if(owner() != msg.sender){
      require(!paused, "the contract is paused");
      if(preSales){
        if(enableWhitelist){
          require(isWhitelisted(msg.sender), "user is not whitelisted");
        }
        require(supply + _mintAmount <= freeMintMaxSupply, "max FREE NFT limit exceeded");

        uint256 ownerMintedCount = addressMintedBalance[msg.sender];
        require(ownerMintedCount + _mintAmount <= nftPerAddressLimit, "max NFT per address exceeded");
      }else{
        require(supply + _mintAmount <= maxSupply, "max NFT limit exceeded");
        require(msg.value >= cost * _mintAmount, "insufficient funds");
      }
    }else{
      require(supply + _mintAmount <= maxSupply, "max NFT limit exceeded");
    }
    
    _;
  }

  // public
  function mint(uint256 _mintAmount) public payable isSafeMint(_mintAmount) {
    addressMintedBalance[msg.sender]+=_mintAmount;
    _safeMint(msg.sender, _mintAmount);
  }

  function setPreSales(bool _preSales) public onlyOwner{
    preSales = _preSales;
  }

  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 setMaxSupply(uint256 _maxSupply) public onlyOwner {
    maxSupply = _maxSupply;
  }
    
  function setFreeMintMaxSupply(uint256 _freeMintMaxSupply) public onlyOwner {
    freeMintMaxSupply = _freeMintMaxSupply;
  }

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

  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 setEnableWhitelist(bool _enableWhitelist) public onlyOwner{
    enableWhitelist = _enableWhitelist;
  }

  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 whitelistUsers(address[] calldata _users) public onlyOwner {
    delete whitelistedAddresses;
    whitelistedAddresses = _users;
  }

  function setReveal(bool _reveal) public onlyOwner {
    reveal = _reveal;
  }
 
  function withdraw() public payable onlyOwner {
    (bool os, ) = payable(owner()).call{value: address(this).balance}("");
    require(os);
  }

  /**
   * @dev See {IERC721Metadata-tokenURI}.
   */
  function tokenURI(uint256 tokenId)
    public
    view
    virtual
    override
    returns (string memory)
  {
    require(
      _exists(tokenId),
      "ERC721Metadata: URI query for nonexistent token"
    );

    string memory currentBaseURI = _baseURI();

    if(reveal==false){
      return bytes(currentBaseURI).length > 0
        ? string(abi.encodePacked(currentBaseURI, "hidden", baseExtension))
        : "";
    }
    
    return bytes(currentBaseURI).length > 0
        ? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension))
        : "";
  }
}
function mint(uint256 _mintAmount) public payable isSafeMint(_mintAmount) {
  addressMintedBalance[msg.sender]+=_mintAmount;
  _safeMint(msg.sender, _mintAmount);
}

// public