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

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

    //Public Sale variables

  uint256 public cost = 0.0375 ether;
  uint256 public maxSupply = 4000;
  uint256 public maxMintAmount = 20;

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

  mapping(address => uint256) public addressMintedBalance;

    // white list variables
    uint256 public nftPerAddressLimit = 3;
    uint256 public itemPricePresale = 0.025 ether;
    bool public isWhitelistActive;
    uint256 public whitelistMaxMint = 3;
    mapping(address => bool) public onWhitelist;
    mapping(address => uint256) public whitelistClaimedBy;


constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721(_name, _symbol) {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
  }

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

    ////////////////////
    //   Whitelist    //
    ////////////////////

    function addToWhitelist(address[] calldata addresses) external onlyOwner {
        for (uint256 i = 0; i < addresses.length; i++)
            onWhitelist[addresses[i]] = true;
    }

    function removeFromWhitelist(address[] calldata addresses)
        external
        onlyOwner
    {
        for (uint256 i = 0; i < addresses.length; i++)
            onWhitelist[addresses[i]] = false;
    }

    function purchasePresaleTokens(uint256 _mintAmount)
        external
        payable
    {
         require(_mintAmount > 0, "Mint atleast 1 UFbrO");
         uint256 supply = totalSupply();
        
         require(supply <= 555, "Presale is sold out.");

        require(supply + _mintAmount <= maxSupply, "Max NFT limit exceeded");
        require(isWhitelistActive, "Whitelist is not active");
        require(onWhitelist[msg.sender], "You are not in whitelist");
        require(
            whitelistClaimedBy[msg.sender] + _mintAmount <= whitelistMaxMint,
            "Purchase exceeds max allowed"
        );
        require(
            msg.value >= _mintAmount * itemPricePresale,
            "Try to send more ETH"
        );

        whitelistClaimedBy[msg.sender] += _mintAmount;

        for (uint256 i = 1; i <= _mintAmount; i++)
            _safeMint(msg.sender, supply + i);
    
    }

    ////////////////////
    //     Public    //
    ////////////////////

  function mint(uint256 _mintAmount) public payable {
    require(!paused, "the contract is paused");
    uint256 supply = totalSupply();
    require(_mintAmount > 0, "need to mint at least 1 NFT");
    require(_mintAmount <= maxMintAmount, "max mint amount per session exceeded");
    require(supply + _mintAmount <= maxSupply, "max NFT limit exceeded");
    require(msg.value >= cost * _mintAmount, "insufficient funds");
    
    for (uint256 i = 1; i <= _mintAmount; i++)
       _safeMint(msg.sender, supply + i);

  }
  
  

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

    ////////////////////
    //     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 setBaseURI(string memory _newBaseURI) public onlyOwner {
    baseURI = _newBaseURI;
  }

  function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
  }
  
  function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
    notRevealedUri = _notRevealedURI;
  }

    // set limit of Whitelist
    function setwhitelistMaxMint(uint256 _whitelistMaxMint) external onlyOwner {
        whitelistMaxMint = _whitelistMaxMint;
    }

    // Change presale price in case of ETH price changes too much
    function setPricePresale(uint256 _itemPricePresale) external onlyOwner {
        itemPricePresale = _itemPricePresale;
    }

    function setisWhitelistActive(bool _isWhitelistActive) external onlyOwner {
        isWhitelistActive = _isWhitelistActive;
    }

  function pause(bool _state) public onlyOwner {
    paused = _state;
  }
  
  function setOnlyWhitelisted(bool _state) public onlyOwner {
    onlyWhitelisted = _state;
  }
  
 
  function withdraw() public payable onlyOwner {
        (bool success, ) = payable(msg.sender).call{
            value: address(this).balance
        }("");
        require(success);
    }
}
function reveal() public onlyOwner {
    revealed = true;
}

////////////////////