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

    string baseURI;
    string public baseExtension = ".json";
    uint256 public cost = 95700000000000000;
    uint256 public maxSupply = 7979;
    uint256 public reserved = 200;
    uint256 public maxMintAmount = 100;
    bytes32 public root;
    bool public paused = true;
    bool public revealed = false;
    bool public whitelistedSale = true;
    string public notRevealedUri;

    constructor(
        string memory _name,
        string memory _symbol,
        string memory _notRevealedUri
    ) ERC721(_name, _symbol) {
        notRevealedUri = _notRevealedUri;
    }

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

    // presale
    function presaleMint(uint256 _mintAmount, uint256 _mintableAmount, bytes32[] memory _proof)
        public
        payable
    {
        bytes32 leaf = keccak256(abi.encode(msg.sender, _mintableAmount));

        require(!paused, "Contract is paused");

        require(whitelistedSale, "Whitelist sale is not started");

        require(verify(_proof, leaf), "Invalid proof");

        require(_mintAmount > 0, "Must mint more than 0");
        
        require(msg.sender != owner());

        uint256 supply = totalSupply();
        uint256 totalAmount;
        uint256 tokenCount = balanceOf(msg.sender);

        // each address can mint at most *_mintableAmount* NFTs
        require(
            tokenCount + _mintAmount <= _mintableAmount,
            string(abi.encodePacked("Limit token ", tokenCount.toString()))
        );

        require(
            supply + _mintAmount <= maxSupply - reserved,
            "Max Supply"
        );

        totalAmount = cost * _mintAmount;

        require(
            msg.value >= totalAmount,
            string(
                abi.encodePacked(
                    "Incorrect amount ",
                    totalAmount.toString(),
                    " ",
                    msg.value.toString()
                )
            )
        );

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

    // public sale
    function mint(uint256 _mintAmount) public payable {

        require(!paused, "Contract is paused");

        require(!whitelistedSale, "Whitelist sale is not closed");

        require(_mintAmount > 0, "Must mint more than 0");

        uint256 supply = totalSupply();
        uint256 totalAmount;
        // uint256 tokenCount = balanceOf(msg.sender);

        // each address can mint at most *maxMintAmount* NFTs
        // require(
        //     tokenCount + _mintAmount <= maxMintAmount,
        //     string(abi.encodePacked("Limit token ", tokenCount.toString()))
        // );

        require(
            supply + _mintAmount <= maxSupply - reserved,
            "Max Supply"
        );
        
        totalAmount = cost * _mintAmount;

        require(
            msg.value >= totalAmount,
            string(
                abi.encodePacked(
                    "Incorrect amount ",
                    totalAmount.toString(),
                    " ",
                    msg.value.toString()
                )
            )
        );

        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 bytes(notRevealedUri).length > 0
                ? string(
                    abi.encodePacked(
                        notRevealedUri,
                        tokenId.toString(),
                        baseExtension
                    )
                )
                : "";
        }

        string memory currentBaseURI = _baseURI();

        return
            bytes(currentBaseURI).length > 0
                ? string(
                    abi.encodePacked(
                        currentBaseURI,
                        tokenId.toString(),
                        baseExtension
                    )
                )
                : "";
    }

    //only owner
    function switchToReserve(bytes32 _root) public onlyOwner {
        root = _root;
        reserved = 0;
        cost = 0;
        whitelistedSale = true;
    }

    function setReveal(bool _revealed) public onlyOwner {
        revealed = _revealed;
    }

    function setReserved(uint256 _reserved) public onlyOwner {
        reserved = _reserved;
    }

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

    function setMaxMintAmount(uint256 _maxMintAmount) public onlyOwner {
        maxMintAmount = _maxMintAmount;
    }

    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 setPause(bool _state) public onlyOwner {
        paused = _state;
    }

    function setWhitelistSale(bool _whitelistSale) public onlyOwner {
        whitelistedSale = _whitelistSale;
    }

    function setRoot(bytes32 _root) public onlyOwner {
        root = _root;
    }

    function withdraw() public payable onlyOwner {
        (bool oa, ) = payable(owner()).call{value: address(this).balance}("");

        require(oa);
    }

    function partialWithdraw(uint256 _amount) public payable onlyOwner {
        (bool success, ) = payable(owner()).call{value: _amount}("");

        require(success);
    }

    function getBalance() public view onlyOwner returns (uint256) {
        return address(this).balance;
    }

    function verify(bytes32[] memory _proof, bytes32 _leaf) public view returns (bool) {
        bytes32 computedHash = _leaf;

        for (uint256 i = 0; i < _proof.length; i++) {
            bytes32 proofElement = _proof[i];
            
            if (computedHash <= proofElement) {
                computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
            } else {
                computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
            }
        }

        // Check if the computed hash (root) is equal to the provided root
        return computedHash == root;
    }

    function verifyParams(address _address, uint256 _mintableAmount, bytes32[] memory _proof) public view returns (bool) {
        bytes32 leaf = keccak256(abi.encode(_address, _mintableAmount));
        return verify(_proof, leaf);
    }
}
function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
}

// internal