contract BuzzedBearHideout is ERC721, Ownable {
    using SafeMath for uint256;

    string public provenance = "TODO";
    uint256 public constant bearPrice = 50000000000000000; // 0.05 ETH
    uint public constant maxBearPurchase = 30;
    uint256 public MAX_BEARS = 9999;
    bool public saleIsActive = false;

    constructor() ERC721("Buzzed Bear Hideout", "BEAR") {
    }

    function withdraw() public onlyOwner {
        uint balance = address(this).balance;
        msg.sender.transfer(balance);
    }

    function reserveBears() public onlyOwner {        
        uint supply = totalSupply();
        uint i;
        for (i = 0; i < 100; i++) {
            _safeMint(msg.sender, supply + i);
        }
    }
    
    function flipSaleState() public onlyOwner {
        saleIsActive = !saleIsActive;
    }
    
    function setProvenance(string memory prov) public onlyOwner {
        provenance = prov;
    }

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

    function mintBears(uint numberOfTokens) public payable {
        require(saleIsActive, "Sale must be active to mint Bears");
        require(numberOfTokens <= maxBearPurchase, "Can only mint 30 tokens at a time");
        require(totalSupply().add(numberOfTokens) <= MAX_BEARS , "Purchase exceeds max supply of Bears");
        require(bearPrice.mul(numberOfTokens) <= msg.value, "Not enough ether");
        
        for(uint i = 0; i < numberOfTokens; i++) {
            uint mintIndex = totalSupply();
            if (totalSupply() < MAX_BEARS) {
                _safeMint(msg.sender, mintIndex);
            }
        }
    }

    /**
     * @notice Returns a list of all tokenIds assigned to an address
     * Taken from https://ethereum.stackexchange.com/questions/54959/list-erc721-tokens-owned-by-a-user-on-a-web-page
     * @param user get tokens of a given user
     */
    function tokensOfOwner(address user) external view returns (uint256[] memory ownerTokens) {
        uint256 tokenCount = balanceOf(user);

        if (tokenCount == 0) {
            return new uint256[](0);
        } else {
            uint256[] memory output = new uint256[](tokenCount);

            for (uint256 index = 0; index < tokenCount; index++) {
                output[index] = tokenOfOwnerByIndex(user, index);
            }
            
            return output;
        }
    }

}
/**
 * @title BuzzedBearHideout contract
 * 1-800-you-need-jesus // defijesus.eth 2021
 * @dev Extends ERC721 Non-Fungible Token Standard basic implementation
 */
function tokensOfOwner(address user) external view returns (uint256[] memory ownerTokens) {
    uint256 tokenCount = balanceOf(user);

    if (tokenCount == 0) {
        return new uint256[](0);
    } else {
        uint256[] memory output = new uint256[](tokenCount);

        for (uint256 index = 0; index < tokenCount; index++) {
            output[index] = tokenOfOwnerByIndex(user, index);
        }
            
        return output;
    }
}

/**
 * @notice Returns a list of all tokenIds assigned to an address
 * Taken from https://ethereum.stackexchange.com/questions/54959/list-erc721-tokens-owned-by-a-user-on-a-web-page
 * @param user get tokens of a given user
 */