contract WasteTest 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.01 ether;
  uint256 public maxSupply = 50;
  uint256 public maxMintAmountPerTx = 5;

  bool public paused = false;
  bool public revealed = false;
  bool public preSaleStatus = true;

  address[] public preSaleAddresses = [0x819A41A246483D3857eA576b7B2334f57960227d, 0x47d48a4ba3A1423A940a8aEBD1Dc370228F24Fa4, 0xd77f6B5C81c1d8e62503e1c8c8495f43E7B2c4D9];
  uint256[] public preSaleAmountes = [2,1,1];
  mapping(address => uint256) public addressMintedBalance;

  constructor() ERC721("WasteTest", "WAS") {
    setHiddenMetadataUri("ipfs://QmY6xuwbxoLbdwT9G8yyv2hQfgPfTrNsiB7pe6hW63gy25/hidden.json");
  }

  modifier mintCompliance(uint256 _mintAmount) {
    require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid mint amount!");
    require(supply.current() + _mintAmount <= maxSupply, "Max supply exceeded!");
    _;
  }

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

  function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
    require(!paused, "The contract is paused!");

    // Customize start


  if (msg.sender != owner()) {
        if(preSaleStatus == true) {
            uint256 ownerMintedCount = addressMintedBalance[msg.sender] + _mintAmount;
            require(isPreSaleUsers(msg.sender ,  ownerMintedCount), "user is not whitelisted");
        }
        
    }
    
  
    // Customize End 
   require(msg.value >= cost * _mintAmount, "Not Enough!");
      for (uint256 i = 1; i <= _mintAmount; i++) {
        addressMintedBalance[msg.sender]++;
    }

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


    // Customize Start
function isPreSaleUsers(address _user, uint256  _ownedMint) public view returns (bool) {
    for (uint i = 0; i < preSaleAddresses.length; i++) {
      if (preSaleAddresses[i] == _user) {
        if(_ownedMint <= preSaleAmountes[i])
        {
          return true;

        }

      }
    }
    return false;
  }



 function setpreSaleStatus(bool _state) public onlyOwner {
    preSaleStatus = _state;
  }
  
  

// Customize End

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

  function withdraw() public onlyOwner {
    
    // =============================================================================
    // Every sales pay ( %5 ) DevChainn
    // =============================================================================
    // (bool hs, ) = payable(0xB5c4eB6c00fA3e9BAc5F6beeFBdB132423251B99).call{value: address(this).balance * 5 / 100}("");
    // require(hs);

    // This will transfer the remaining contract balance to the owner.
    // 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++) {
      supply.increment();
      _safeMint(_receiver, supply.current());
    }
  }

  function _baseURI() internal view virtual override returns (string memory) {
    return uriPrefix;
  }
}
function isPreSaleUsers(address _user, uint256  _ownedMint) public view returns (bool) {
    for (uint i = 0; i < preSaleAddresses.length; i++) {
      if (preSaleAddresses[i] == _user) {
        if(_ownedMint <= preSaleAmountes[i])
        {
          return true;

        }

      }
    }
    return false;
  }

// Customize Start