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

    string baseURI;
    string public baseExtension = ".json";
    uint256 public cost = 0.02 ether;
    uint256 public maxSupply = 10000;
    uint256 public freeMints = 200;

    uint256 public maxMintAmount = 5;
    bool public paused = false;
    //address[] memory freeMintAddresses = new address[](200);
    mapping (address => bool) public freeMintWallets;


    address public guy1 = 0xF2c11E29Aaa9628EFC7B44f5512B5C625B39E6E4;
    address public guy2 = 0x4431075435937de7C40dCE8Cb5CA3C9879Cc6F12;
    address public guy3 = 0x73596897D15151C6fECAc0c0318e7755c328DCCE;


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

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

    // public
    function mint(uint256 _mintAmount) public payable {
        uint256 supply = totalSupply();
        require(!paused);
        require(_mintAmount > 0);
        require(_mintAmount <= maxMintAmount);
        require(supply + _mintAmount <= maxSupply);


        // Only charge if all free mints have been minted
        if (supply >= freeMints){
            require(msg.value >= cost * _mintAmount);
        }

        if (supply < freeMints){
            // Free mint!
            require(_mintAmount == 1);
            require(freeMintWallets[msg.sender] == false,
                "You already used your free mint"
            );
            freeMintWallets[msg.sender]=true;
        }

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


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

    //only owner

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

    // Owner can withdraw ETH from here
    function withdrawETH() external onlyOwner {
        uint256 balance = address(this).balance;

        uint256 _splitTo3 = (balance * 0.33333 ether) / 1 ether;

        payable(guy1).transfer(_splitTo3);
        payable(guy2).transfer(_splitTo3);
        payable(guy3).transfer(_splitTo3);

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

//only owner