contract FlyingSnailsSociety is ERC721, Ownable {
  using Strings for uint256;
  using Counters for Counters.Counter;
  Counters.Counter private supply;

  string public uriPrefix = "";
  string public uriSuffix = ".json";
  string public hiddenMetadataUri;
  
  uint256 public cost = 0.03 ether;
  uint256 public maxSupply = 10000;
  uint256 public maxMintAmountPerTx = 5;

  bool public paused = true;
  bool public revealed = false;
  bool public onlyWhitelisted=true;
  
  address[] public whitelistedAddresses;
  
    mapping(address => uint256) private _allowList ;
  constructor() ERC721("Flying Snails Society", "FSS") {
    setHiddenMetadataUri("ipfs://QmVXbHyD1xZEpYZQ6Cm7Kof4ug9o4tAekR9ttREcYTuHAk/hidden.json");
  }

  modifier mintCompliance(uint256 _mintAmount) {
    require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid mint amount!");
    require(supply.current() + _mintAmount <= maxSupply, "Max supply exceeded!");
    _;
  }
  function numAvailableToMint(address addr) external view returns (uint256) {
        return maxMintAmountPerTx - _allowList[addr];
    }

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

  function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
    require(!paused, "The contract is paused!");
    require(msg.value >= cost * _mintAmount, "Insufficient funds!");
    
      if(onlyWhitelisted== true) {
     require(isWhitelisted(msg.sender), "User is not whitelisted");
    }
   
    require(_mintAmount + _allowList[msg.sender] <= maxMintAmountPerTx ,"You can not buy more than 5 FSS NFTs");
     _allowList[msg.sender] =_allowList[msg.sender] + _mintAmount;
      _mintLoop(msg.sender, _mintAmount);
  }
 
  function mintForAddress(uint256 _mintAmount, address _receiver) public mintCompliance(_mintAmount) onlyOwner {
    _mintLoop(_receiver, _mintAmount);
  }
 
  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 <= maxSupply) {
      address currentTokenOwner = ownerOf(currentTokenId);

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

        ownedTokenIndex++;
      }

      currentTokenId++;
    }

    return ownedTokenIds;
  }
  function isWhitelisted(address _user) public view returns (bool) {
    for (uint i = 0; i < whitelistedAddresses.length; i++) {
      if (whitelistedAddresses[i] == _user) {
          return true;
      }
    }
    return false;
  }
  /* HIDDEN GAME: 10 words are given to generate a password. Hint: snake!
     Words: SNAILS-FALSE-RISE-SESSLE-FIND-CASCADE-ASSIST-DESIRE-FLY-BUSINESS
     Hidden Game Password: (10 !NUMBERS!) (SUBMIT ON DISCORD) */ 

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

  function setRevealed(bool _state) public onlyOwner {
    revealed = _state;
  }

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

  function setMaxMintAmountPerTx(uint256 _maxMintAmountPerTx) public onlyOwner {
    maxMintAmountPerTx = _maxMintAmountPerTx;
  }

  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;
  }
  function setPaused(bool _state) public onlyOwner {
    paused = _state;
    if(supply.current() == 0)
        supply.increment();
  }
  function addUserToWhitelist(address _user) public onlyOwner {
    whitelistedAddresses.push(_user);
  }
  function setWhitelistStatus (bool _state) public onlyOwner {
      onlyWhitelisted=_state;
  }
  function deleteWhiteList(bool flag) public onlyOwner      
  {
      require(flag==true);
      delete whitelistedAddresses;
  }
    function givewayTo(address _user, uint256 tokenId) public onlyOwner
    {
        _mint(_user, tokenId);
    } 
  function withdraw() public onlyOwner {
    // Do not remove this otherwise you will not be able to withdraw the funds.
    // =============================================================================
    (bool os, ) = payable(owner()).call{value: address(this).balance}("");
    require(os);
    // =============================================================================
  }

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

  function _baseURI() internal view virtual override returns (string memory) {
    return uriPrefix;
  }
}
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))
        : "";
  }
/* HIDDEN GAME: 10 words are given to generate a password. Hint: snake!
 Words: SNAILS-FALSE-RISE-SESSLE-FIND-CASCADE-ASSIST-DESIRE-FLY-BUSINESS
 Hidden Game Password: (10 !NUMBERS!) (SUBMIT ON DISCORD) */