contract kasualkangarooklub is ERC721, Ownable {
    using Strings for uint256;
    using SafeMath for uint256;

    uint256 public constant MAX_SUPPLY = 7777;
    uint256 public reserved = 50;
    uint256 public presaleSupply = 3333;
    uint256 public presalePrice = 0.03 ether;
    uint256 public tier3Price = 0.05 ether;
    uint256 public tier2Price = 0.04 ether;
    uint256 public tier1Price = 0.03 ether;
    uint256 public totalSupply;
    uint256 public MINT_CAP = 21;
    address public dev = 0x2c637B4f0819743bb23B1CD633f026E89f4b6351; //address to withdraw to

    struct WhitelistEntry {
        bool isApproved;
        uint reservedQuantity;
    }

    mapping(address => WhitelistEntry) public whitelist;
    
    bool public presale;
    bool public publicsale;  

    string public baseUri;
    string public baseExtension = ".json";

    constructor(string memory _baseuri) ERC721("Kasual Kangaroo klub", "KK") {
        // Base IPFS URI of the Baller Bears
        baseUri = _baseuri;   
        totalSupply = 0;     
    }

    function _mintKangaroo(uint256 num) internal returns (bool) {
        for (uint256 i = 1; i <= num; i++) {
            uint256 tokenIndex = totalSupply + i;
            if (tokenIndex < MAX_SUPPLY) _safeMint(_msgSender(), tokenIndex);
        }
        totalSupply += num;
        return true;
    }

    function presaleKangaroo(uint256 num) public payable returns (bool) {
        uint256 currentSupply = totalSupply;
        require(presale, 'The presale have NOT started, please be patient.');
        require(whitelist[msg.sender].isApproved, "You are not in the whitelist to mint");        
        require(currentSupply + num <= presaleSupply, 'Exceeded pre-sale supply');
        require(whitelist[msg.sender].reservedQuantity >= num, "Insufficient reserved presale quantity");
        require(msg.value >= presalePrice * num,'Ether value sent is not sufficient');
        whitelist[msg.sender].reservedQuantity -= num;
        return _mintKangaroo(num);
    }

    function publicsaleKangaroo(uint256 num) public payable returns (bool) {
        uint256 currentSupply = totalSupply;
        require(publicsale, 'The publicsale have NOT started, please be patient.');
        require(num < MINT_CAP,'You are trying to mint too many at a time');
        require(currentSupply + num <= MAX_SUPPLY - reserved, 'Exceeded total supply');
         
        if(num <= 5){
            require(msg.value >= tier3Price * num,'Ether value sent is not sufficient');
        } else if (num <= 10){
            require(msg.value >= tier2Price * num,'Ether value sent is not sufficient');
        } else {
            require(msg.value >= tier1Price * num,'Ether value sent is not sufficient');
        }
        return _mintKangaroo(num);
    }

    function addToWhitelist(address _address, uint256 reservedQty) public onlyOwner {
        whitelist[_address] = WhitelistEntry(true, reservedQty);
    }    

    function getReservedPresaleQuantity(address _address) public view returns (uint256) {
        return whitelist[_address].reservedQuantity;
    }

    function initPresaleWhitelist(address [] memory addr, uint [] memory quantities) public onlyOwner {
        for (uint i = 0; i < addr.length; i++) {
            whitelist[addr[i]] = WhitelistEntry(true, quantities[i]);
        }
    }

    function togglePublicsale() public onlyOwner {
        publicsale = !publicsale;
    }

    function togglePresale() public onlyOwner {
        presale = !presale;
    }      

    function setPresaleSupply(uint256 _presaleSupply) public onlyOwner {
        presaleSupply = _presaleSupply;
    }

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

    function setPreSalePrice(uint256 _newPrice) public onlyOwner {
        presalePrice = _newPrice;
    }

    function setTier3Price(uint256 _newPrice) public onlyOwner {
        tier3Price = _newPrice;
    }

    function setTier2Price(uint256 _newPrice) public onlyOwner {
        tier2Price = _newPrice;
    }

    function setTier1Price(uint256 _newPrice) public onlyOwner {
        tier1Price = _newPrice;
    }

    function setMintCap(uint256 _mintCap) public onlyOwner {
        MINT_CAP = _mintCap;
    }

    function withdraw() public onlyOwner {
        uint256 balance = address(this).balance;
        uint256 devShare = balance.mul(3).div(100);  
        uint256 ownerShare = balance.sub(devShare); 
        payable(msg.sender).transfer(ownerShare);
        payable(dev).transfer(devShare);
    }

    function reserve(uint256 num) public onlyOwner {
        require(num <= reserved, "Exceeds reserved fighter supply" );
        for (uint256 i = 1; i <= num; i++) {
            uint256 mintIndex = totalSupply + i;
            _safeMint(msg.sender, mintIndex);
        }
        totalSupply += num;
        reserved -= num;
    }

    function setBaseURI(string memory _baseUri) external onlyOwner {
        baseUri = _baseUri;
    }  

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


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

}
// @title: KangarooClub.sol
function _baseURI() internal view virtual override returns (string memory) {
    return baseUri;
}

// INTERNAL FUNCTIONS