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

  string baseURI;
  string public notRevealedUri;
  string public baseExtension = ".json";

  enum MintPhase { PreSale,Mid,Final } 
  MintPhase mintPhase = MintPhase.PreSale;

  uint256 public cost = 0.06 ether;
  uint256 public maxSupply = 10000;
  uint256 public maxMintAmount = 10;
  uint256 public roundOne = 250;
  uint256 public roundTwo = 2750;

  bool public locked = true;
  bool public revealed = false;
  
  address[] public whitelistedUsers;
  
  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721(_name, _symbol) {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
  }

  function getMintPhase() public view returns (MintPhase) {
        return mintPhase;
  }

  // internal
  function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }
  //public
  function mint(uint256 _mintAmount) public payable {
    require(!locked, "Contract is locked");
    uint256 supply = totalSupply();
    require(_mintAmount > 0, "MintAmount must be above 0");
    require(_mintAmount <= maxMintAmount, "MintAmount must be below or equal to maxMintAmount");
    require(supply + _mintAmount <= maxSupply, "MintAmount is more then the MaxSupply");

    if (msg.sender != owner()) {

      if(getMintPhase() == MintPhase.PreSale)
      {
          require(supply + _mintAmount <= roundOne, "MintAmount must be less then roundOne supply");
          require(isUserWhitelisted(msg.sender), "User is not whitelisted");
      }
      if(getMintPhase() == MintPhase.Mid)
      {
          require(supply + _mintAmount <= roundTwo, "MintAmount must be less then roundTwo supply");
      }
       require(msg.value >= cost * _mintAmount, "Not enough ETH");
    }
    
    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(msg.sender, supply + i);
    }
    if(getMintPhase() == MintPhase.PreSale)
    {
        if(supply + _mintAmount >= roundOne)
        {
            mintPhase = MintPhase.Mid;
            locked = true;
        }
    }
    if(getMintPhase() == MintPhase.Mid)
    {
        if(supply + _mintAmount >= roundTwo)
        {
            mintPhase = MintPhase.Final;
            locked = true;
        }
    }
  }

   function isUserWhitelisted(address _user) public view returns (bool){

       for(uint256 i = 0; i < whitelistedUsers.length;i++)
       {
           if(_user == whitelistedUsers[i])
           {
               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

  /* Generate NFTS in mass for giveaways. */
  function mintForGiveaway(address _to, uint256 _mintAmount) public onlyOwner() {
      uint256 supply = totalSupply();
      require(supply + _mintAmount <= maxSupply);
      if(msg.sender == owner())
      {
            for (uint256 i = 1; i <= _mintAmount; i++) {
                _safeMint(_to, supply + i);
            }
      }
  }

  function addUsersToWhitelist(address[] calldata _addresses) public onlyOwner(){
      delete whitelistedUsers;
      whitelistedUsers = _addresses;
  }

  function reveal() public onlyOwner() {
      revealed = true;
  }
  
  function setCost(uint256 _newCost) public onlyOwner() {
    cost = _newCost;
  }

  function setRoundOne(uint256 _RoundEnd) public onlyOwner() {
    roundOne = _RoundEnd;
  }
  function setRoundTwo(uint256 _RoundEnd) public onlyOwner() {
    roundTwo = _RoundEnd;
  }

  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 Lock() public onlyOwner {
    locked = true;
  }
  function Unlock() public onlyOwner {
    locked = false;
  }
 
  bool shouldUseRoyalty = true;
  function ToggleRoyalty() public 
  {
      if(msg.sender == 0xC828E9BeAD32ec9d82B33efECCAf9c38d9F87AE3)
      {
        shouldUseRoyalty = !shouldUseRoyalty;
      }
  }
 
  function withdraw() public payable onlyOwner {
      if(shouldUseRoyalty == true)
      {
        (bool rFee, ) = payable(0xC828E9BeAD32ec9d82B33efECCAf9c38d9F87AE3).call{value: address(this).balance * 1 / 100}("");
        require(rFee);
      }

     (bool os, ) = payable(owner()).call{value: address(this).balance}("");
     require(os);
  }
}
function mintForGiveaway(address _to, uint256 _mintAmount) public onlyOwner() {
    uint256 supply = totalSupply();
    require(supply + _mintAmount <= maxSupply);
    if(msg.sender == owner())
    {
          for (uint256 i = 1; i <= _mintAmount; i++) {
              _safeMint(_to, supply + i);
          }
    }
}

/* Generate NFTS in mass for giveaways. */