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

  string baseURI;
  string public baseExtension = ".json";
  string public notRevealedUri;
  uint256 public cost = 0.033 ether;
  uint256 public maxSupply = 3333;
  uint256 public maxMintAmount = 20;
  uint256 public nftPerAddressLimit = 20;
  uint256 public _reserved = 33;
  bool public paused = true;
  bool public revealed = false;
  bool public onlyWhitelisted = true;
  address[] public whitelistedAddresses;
  mapping(address => uint256) public addressesMintedBalance; 
  //   presale price 0.03 ether
  //   public sale price 0.033 ether

  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721(_name, _symbol) {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
  }
  
   //sale holders
  address[] public fundRecipients = [
    0x638b389C966e29BA91dA3ccEe8326d0Fa54C4B0c,
   0x3097617CbA85A26AdC214A1F87B680bE4b275cD0,
   0x7943C4027Df99dFBAd320784F2FA534d5865a6D5,
   0x181Cb650FB05f91986C89fD8c2c25A8b6f6dB864
  ];
  uint256[] public receivePercentagePt = [5200, 2250, 550, 2000];   //distribution in basis points

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

   // public
  function mint(uint256 _mintAmount) public payable {
    require(!paused, "The contract is currently paused.");
    uint256 supply = totalSupply();
    require(_mintAmount > 0, "You need to adopt at least 1 bunny");
    uint256 ownerMintedCount = addressesMintedBalance[msg.sender];
    require(_mintAmount <= maxMintAmount, "Max bunny adoptions per transaction exceeded");
    require(supply + _mintAmount <= maxSupply, "Max bunny mint limit exceeded");

    if (msg.sender != owner()) {
        if (onlyWhitelisted == true){
            require(isWhitelisted(msg.sender), "Your address is not whitelisted for this sale.");
            // uint256 ownerMintedCount = addressesMintedBalance[msg.sender];
            require(ownerMintedCount + _mintAmount <= nftPerAddressLimit, "Max bunnies for your wallet for this sale would be exceeded");
        }
      require(msg.value >= cost * _mintAmount, "You will need more ETH to adopt your bunnies during this sale.");
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
        addressesMintedBalance[msg.sender]++;
      _safeMint(msg.sender, supply + i);
    }
  }
  
  function isWhitelisted(address _user) public view returns (bool) {
      for(uint256 i = 0; i < whitelistedAddresses.length; i++) {
        if (whitelistedAddresses[i] == _user) {
            return true;
        }  
      }
      return false;
  }
  

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

  //only owner
  function reveal() public onlyOwner() {
      revealed = true;
  }
  
  function setNftPerAddressLimit(uint256 _limit) public onlyOwner() {
    nftPerAddressLimit = _limit;
  }
  
  function setCost(uint256 _newCost) public onlyOwner() {
    cost = _newCost;
  }

  function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner() {
    maxMintAmount = _newmaxMintAmount;
  }
  
  function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
    notRevealedUri = _notRevealedURI;
  }

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

  //fund withdraw functions ---
  function withdrawFund() public onlyOwner {
    uint256 currentBal = address(this).balance;
    require(currentBal > 0);
    for (uint256 i = 0; i < fundRecipients.length-1; i++) {
      _withdraw(fundRecipients[i], currentBal * receivePercentagePt[i] / 10000);
    }
    //final address receives remainder to prevent ether dust
    _withdraw(fundRecipients[fundRecipients.length-1], address(this).balance);
  }
  function _withdraw(address _addr, uint256 _amt) private {
    (bool success,) = _addr.call{value: _amt}("");
    require(success, "Transfer failed");
  }

    function giveAway(address _to, uint256 _amount) external onlyOwner() {
        require( _amount <= _reserved, "Exceeds reserved supply" );

        uint256 supply = totalSupply();
        for(uint256 i; i < _amount; i++){
            _safeMint( _to, supply + i );
        }

        _reserved -= _amount;
    }

    function setReserved(uint256 _newReserved) public onlyOwner {
        _reserved = _newReserved;
    }
}
function withdrawFund() public onlyOwner {
  uint256 currentBal = address(this).balance;
  require(currentBal > 0);
  for (uint256 i = 0; i < fundRecipients.length-1; i++) {
    _withdraw(fundRecipients[i], currentBal * receivePercentagePt[i] / 10000);
  }
  //final address receives remainder to prevent ether dust
  _withdraw(fundRecipients[fundRecipients.length-1], address(this).balance);
}

//fund withdraw functions ---