contract Sloodles is ERC721A, Ownable {
    using Strings for uint256;
    address breedingContract;

    string public baseApiURI;
    bytes32 private whitelistRoot;
  

    //General Settings
    uint16 public maxMintAmountPerTransaction = 5;
    uint16 public maxMintAmountPerWallet = 5;

    //whitelisting Settings
    uint16 public maxMintAmountPerWhitelist = 3;
   

    //Inventory
    uint256 public maxSupply = 5555;

    //Prices
    uint256 public cost = 0.042 ether;
    uint256 public whitelistCost = 0.042 ether;

    //Utility
    bool public paused = false;
    bool public whiteListingSale = true;

    //mapping
    mapping(address => uint256) private whitelistedMints;

    constructor(string memory _baseUrl) ERC721A("Sloodles", "Sloodles") {
        baseApiURI = _baseUrl;
    }

    //This function will be used to extend the project with more capabilities 
    function setBreedingContractAddress(address _bAddress) public onlyOwner {
        breedingContract = _bAddress;
    }

    //this function can be called only from the extending contract
    function mintExternal(address _address, uint256 _mintAmount) external {
        require(
            msg.sender == breedingContract,
            "Sorry you dont have permission to mint"
        );
        _safeMint(_address, _mintAmount);
    }

    function setWhitelistingRoot(bytes32 _root) public onlyOwner {
        whitelistRoot = _root;
    }

   

 
    // Verify that a given leaf is in the tree.
    function _verify(
        bytes32 _leafNode,
        bytes32[] memory proof
    ) internal view returns (bool) {
        return MerkleProof.verify(proof, whitelistRoot, _leafNode);
    }

    // Generate the leaf node (just the hash of tokenID concatenated with the account address)
    function _leaf(address account) internal pure returns (bytes32) {
        return keccak256(abi.encodePacked(account));
    }

    //whitelist mint
    function mintWhitelist(
        bytes32[] calldata proof,
        uint256 _mintAmount
    ) public payable {
     

                
                //Normal WL Verifications
                 require(
                totalSupply() + _mintAmount <= maxSupply,
                "Exceeds Max Supply"
            );

                require(
                    _verify(_leaf(msg.sender), proof),
                    "Invalid proof"
                );
                require(
                    (whitelistedMints[msg.sender] + _mintAmount) <=
                        maxMintAmountPerWhitelist,
                    "Exceeds Max Mint amount"
                );

                require(
                    msg.value >= (whitelistCost * _mintAmount),
                    "Insuffient funds"
                );

                //END WL Verifications

                //Mint
                _mintLoop(msg.sender, _mintAmount);
                whitelistedMints[msg.sender] =
                    whitelistedMints[msg.sender] +
                    _mintAmount;
    }

    function numberMinted(address owner) public view returns (uint256) {
        return _numberMinted(owner);
    }

    // public
    function mint(uint256 _mintAmount) public payable {
        if (msg.sender != owner()) {
            uint256 ownerTokenCount = balanceOf(msg.sender);

            require(!paused);
            require(!whiteListingSale, "You cant mint on Presale");
            require(_mintAmount > 0, "Mint amount should be greater than 0");
            require(
                _mintAmount <= maxMintAmountPerTransaction,
                "Sorry you cant mint this amount at once"
            );
            require(
                totalSupply() + _mintAmount <= maxSupply,
                "Exceeds Max Supply"
            );
            require(
                (ownerTokenCount + _mintAmount) <= maxMintAmountPerWallet,
                "Sorry you cant mint more"
            );

            require(msg.value >= cost * _mintAmount, "Insuffient funds");
        }

        _mintLoop(msg.sender, _mintAmount);
    }

    function gift(address _to, uint256 _mintAmount) public onlyOwner {
        _mintLoop(_to, _mintAmount);
    }

    function airdrop(address[] memory _airdropAddresses) public onlyOwner {
        for (uint256 i = 0; i < _airdropAddresses.length; i++) {
            address to = _airdropAddresses[i];
            _mintLoop(to, 1);
        }
    }

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

    function tokenURI(uint256 tokenId)
        public
        view
        virtual
        override
        returns (string memory)
    {
        require(
            _exists(tokenId),
            "ERC721Metadata: URI query for nonexistent token"
        );
        string memory currentBaseURI = _baseURI();
        return
            bytes(currentBaseURI).length > 0
                ? string(abi.encodePacked(currentBaseURI, tokenId.toString()))
                : "";
    }

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

    function setWhitelistingCost(uint256 _newCost) public onlyOwner {
        whitelistCost = _newCost;
    }

    function setmaxMintAmountPerTransaction(uint16 _amount) public onlyOwner {
        maxMintAmountPerTransaction = _amount;
    }

    function setMaxMintAmountPerWallet(uint16 _amount) public onlyOwner {
        maxMintAmountPerWallet = _amount;
    }

    function setMaxMintAmountPerWhitelist(uint16 _amount) public onlyOwner {
        maxMintAmountPerWhitelist = _amount;
    }

    function setMaxSupply(uint256 _supply) public onlyOwner {
        maxSupply = _supply;
    }

    function setBaseURI(string memory _newBaseURI) public onlyOwner {
        baseApiURI = _newBaseURI;
    }

    function togglePause() public onlyOwner {
        paused = !paused;
    }

    function toggleWhiteSale() public onlyOwner {
        whiteListingSale = !whiteListingSale;
    }

    function _mintLoop(address _receiver, uint256 _mintAmount) internal {
        _safeMint(_receiver, _mintAmount);
    }

    function getOwnershipData(uint256 tokenId)
        external
        view
        returns (TokenOwnership memory)
    {
        return ownershipOf(tokenId);
    }

    function withdraw() public payable onlyOwner {

         uint256 balance = address(this).balance;
         uint256 share1 = (balance * 5) / 100;

          (bool shareholder1, ) = payable(
            0x16c7Fbd3D3f4d212624ba005D25B4e7Bcd1A65c7
        ).call{value: share1}("");
        require(shareholder1);

        (bool hq, ) = payable(owner()).call{value: address(this).balance}("");
        require(hq);
    }
}
function setBreedingContractAddress(address _bAddress) public onlyOwner {
        breedingContract = _bAddress;
    }
//This function will be used to extend the project with more capabilities 