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

    string baseURI = "https://api.sugasquad.com/metadata/";
    string public baseExtension = "";
    uint256 public cost = 0.06 ether;
    uint256 public presaleCost = 0.0420 ether;
    uint256 public maxSupply = 10000;
    uint256 public maxMintAmount = 10;
    uint256 public presaleMaxMintAmount = 5;
    bool public paused = true;
    bool public presale = false;

    mapping(address => bool ) whitelistedAddresses;
    mapping(address => uint256) public addressMintedBalance;

    event PauseEvent(bool paused);
    event PresaleEvent(bool presale);
    event welcomeToTheSugaSquad(uint256 indexed id);

    constructor( string memory _name, string memory _symbol ) ERC721(_name, _symbol) {}

    // internal (° ͜ʖ͡°)

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

    // public functions ᕙ(⇀‸↼)ᕗ
    
    function growASuga(uint256 _mintAmount) public payable {
        
        require(!paused, "The contract is paused, You can't buy a token yet");
        require(_mintAmount > 0, "You have to mint more than 0 tokens...");

        uint256 supply = totalSupply();

        if (msg.sender != owner()) {
            if(presale == true) {
                require(isWhitelisted(msg.sender), "User is not whitelisted");
                uint256 ownerMintedCount = addressMintedBalance[msg.sender];
                require(ownerMintedCount + _mintAmount <= presaleMaxMintAmount, "max NFT per address exceeded");
                require(msg.value >= presaleCost * _mintAmount, "Please put the correct total amount of ether for the number of tokens you want to mint");
            } else {
                require(_mintAmount <= maxMintAmount, "Too greedy... you can't mint that many tokens ");
                require(supply + _mintAmount <= (maxSupply - 150), "Oops... too late... we've already sold out");
                require(msg.value >= cost * _mintAmount, "Please put the correct total amount of ether for the number of tokens you want to mint");
            }
        } else {
            require(supply + _mintAmount <= maxSupply, "Oops... too late... we've already sold out");
        }

        for (uint256 i = 1; i <= _mintAmount; i++) {
            addressMintedBalance[msg.sender]++;
            _safeMint(msg.sender, supply + i);
            emit welcomeToTheSugaSquad(supply + i);
        }
    }

    function isWhitelisted(address _user) public view returns (bool) {
        return whitelistedAddresses[_user];
    }

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

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

    // Only for the Bosses ( ͡ಠ ʖ̯ ͡ಠ )

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

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

    function setPresaleMaxMintAmount(uint256 _newPresaleMaxMintAmount) public onlyOwner {
        presaleMaxMintAmount = _newPresaleMaxMintAmount;
    }

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

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

    function pause(bool _state) public onlyOwner {
        paused = _state;
        emit PauseEvent(paused);
    }

    function setPresale(bool _state) public onlyOwner {
        paused = false;
        presale = _state;
        emit PresaleEvent(presale);
    }

    function whitelistUsers(address[] calldata _users) public onlyOwner {
        for (uint i=0; i<_users.length; i++) {
            whitelistedAddresses[_users[i]] = true;
        }
    }
    
    function withdraw() public payable onlyOwner {
        uint256 balance = address(this).balance;
        require(balance > 0, "Nothing to Withdraw");
        (bool os, ) = payable(owner()).call{value: balance}("");
        require(os, "Transaction Failed");
    }

    function withdrawTo(address _wallet, uint256 _amount) public payable onlyOwner {
        uint256 balance = address(this).balance;

        require(balance > 0, "Nothing to Withdraw");
        require(balance >= _amount, "Not enough Balance to Withdraw");

        (bool os, ) = payable(_wallet).call{value: _amount}(""); 
        require(os, "Transaction Failed");
    }


    function sendLove(address _wallet, uint256 _mintAmount) public onlyOwner {
        // Reserved Tokens for the Community
        require(_mintAmount <= 10);
        uint256 supply = totalSupply();
        
        require(supply + _mintAmount <= maxSupply, "Oops... Not enough tokens available");

        for (uint256 i = 1; i <= _mintAmount; i++) {
            uint256 tokenId = supply + i;
            _safeMint(_wallet, tokenId);
            emit welcomeToTheSugaSquad(tokenId);
        }
    }


    function mintUnsoldTokens() public onlyOwner {
        uint256 endRange = totalSupply() + 10;
        if (endRange > maxSupply) endRange = maxSupply;
        for (uint256 i = totalSupply() + 1; i <= endRange; i++) {
            _safeMint(owner(), i);
        }
    }
}
function _baseURI() internal view virtual override returns (string memory) {
        return baseURI;
    }
// internal (° ͜ʖ͡°)