// Contract: COCA
// Target function: function tokenURI(uint256 tokenId_) public view override returns (string memory)
, tokenId.toString())) : "";
    }

    /**
     * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
     * token will be the concatenation of the `baseURI` and the `tokenId`. Empty
     * by default, can be overriden in child contracts.
     */
    function _baseURI() internal view virtual returns (string memory) {
        return "";
    }

    /**
     * @dev See {IERC721-approve}.
     */
    function approve(address to, uint256 tokenId) public virtual override {
        address owner = ERC721.ownerOf(tokenId);
        require(to != owner, "ERC721: approval to current owner");

        require(
            _msgSender() == owner || isApprovedForAll(owner, _msgSender()),
            "ERC721: approve caller is not owner nor approved for all"
        );

        _approve(to, tokenId);
    }

    /**
     * @dev See {IERC721-getApproved}.
     */
    function getApproved(uint256 tokenId) public view virtual override returns (address) {
        require(_exists(tokenId), "ERC721: approved query for nonexistent token");

        return _tokenApprovals[tokenId];
    }

    /**
     * @dev See {IERC721-setApprovalForAll}.
     */
    function setApprovalForAll(address operator, bool approved) public virtual override {
        _setApprovalForAll(_msgSender(), operator, approved);
    }

    /**
     * @dev See {IERC721-isApprovedForAll}.
     */
    function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
        return _operatorApprovals[owner][operator];
    }

    /**
     * @dev See {IERC721-transferFrom}.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        //solhint-disable-next-line max-line-length
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");

        _transfer(from, to, tokenId);
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        safeTransferFrom(from, to, tokenId, "");
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) public virtual override {
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
        _safeTransfer(from, to, tokenId, _data);
    }

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * `_data` is additional data, it has no specified format and it is sent in call to `to`.
     *
     * This internal function is equivalent to {safeTransferFrom}, and can be used to e.g.
     * implement alternative mechanisms to perform token transfer, such as signature-based.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeTransfer(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) internal virtual {
        _transfer(from, to, tokenId);
        require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
    }

    /**
     * @dev Returns whether `tokenId` exists.
     *
     * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
     *
     * Tokens start existing when they are minted (`_mint`),
     * and stop existing when they are burned (`_burn`).
     */
    function _exists(uint256 tokenId) internal view virtual returns (bool) {
        return _owners[tokenId] != address(0);
    }

    /**
     * @dev Returns whether `spender` is allowed to manage `tokenId`.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
        require(_exists(tokenId), "ERC721: operator query for nonexistent token");
        address owner = ERC721.ownerOf(tokenId);
        return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
    }

    /**
     * @dev Safely mints `tokenId` and transfers it to `to`.
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeMint(address to, uint256 tokenId) internal virtual {
        _safeMint(to, tokenId, "");
    }

    /**
     * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
     * forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
     */
    function _safeMint(
        address to,
        uint256 tokenId,
        bytes memory _data
    ) internal virtual {
        _mint(to, tokenId);
        require(
            _checkOnERC721Received(address(0), to, tokenId, _data),
            "ERC721: transfer to non ERC721Receiver implementer"
        );
    }

    /**
     * @dev Mints `tokenId` and transfers it to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - `to` cannot be the zero address.
     *
     * Emits a {Transfer} event.
     */
    function _mint(address to, uint256 tokenId) internal virtual {
        require(to != address(0), "ERC721: mint to the zero address");
        require(!_exists(tokenId), "ERC721: token already minted");

        _beforeTokenTransfer(address(0), to, tokenId);

        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(address(0), to, tokenId);
    }

    /**
     * @dev Destroys `tokenId`.
     * The approval is cleared when the token is burned.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     *
     * Emits a {Transfer} event.
     */
    function _burn(uint256 tokenId) internal virtual {
        address owner = ERC721.ownerOf(tokenId);

        _beforeTokenTransfer(owner, address(0), tokenId);

        // Clear approvals
        _approve(address(0), tokenId);

        _balances[owner] -= 1;
        delete _owners[tokenId];

        emit Transfer(owner, address(0), tokenId);
    }

    /**
     * @dev Transfers `tokenId` from `from` to `to`.
     *  As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     *
     * Emits a {Transfer} event.
     */
    function _transfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {
        require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer of token that is not own");
        require(to != address(0), "ERC721: transfer to the zero address");

        _beforeTokenTransfer(from, to, tokenId);

        // Clear approvals from the previous owner
        _approve(address(0), tokenId);

        _balances[from] -= 1;
        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(from, to, tokenId);
    }

    /**
     * @dev Approve `to` to operate on `tokenId`
     *
     * Emits a {Approval} event.
     */
    function _approve(address to, uint256 tokenId) internal virtual {
        _tokenApprovals[tokenId] = to;
        emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
    }

    /**
     * @dev Approve `operator` to operate on all of `owner` tokens
     *
     * Emits a {ApprovalForAll} event.
     */
    function _setApprovalForAll(
        address owner,
        address operator,
        bool approved
    ) internal virtual {
        require(owner != operator, "ERC721: approve to caller");
        _operatorApprovals[owner][operator] = approved;
        emit ApprovalForAll(owner, operator, approved);
    }

    /**
     * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
     * The call is not executed if the target address is not a contract.
     *
     * @param from address representing the previous owner of the given token ID
     * @param to target address that will receive the tokens
     * @param tokenId uint256 ID of the token to be transferred
     * @param _data bytes optional data to send along with the call
     * @return bool whether the call correctly returned the expected magic value
     */
    function _checkOnERC721Received(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) private returns (bool) {
        if (to.isContract()) {
            try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) {
                return retval == IERC721Receiver.onERC721Received.selector;
            } catch (bytes memory reason) {
                if (reason.length == 0) {
                    revert("ERC721: transfer to non ERC721Receiver implementer");
                } else {
                    assembly {
                        revert(add(32, reason), mload(reason))
                    }
                }
            }
        } else {
            return true;
        }
    }

    /**
     * @dev Hook that is called before any token transfer. This includes minting
     * and burning.
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
     * transferred to `to`.
     * - When `from` is zero, `tokenId` will be minted for `to`.
     * - When `to` is zero, ``from``'s `tokenId` will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {}
}

interface IERC20 {
    function transferFrom(address from_, address to_, uint256 amount_) external;
}
// interface IERC721 {
//     function transferFrom(address from_, address to_, uint256 tokenId_) external;
// }
interface IERC1155 {
    function safeTransferFrom(address from_, address to_, uint256 id_, uint256 amount_, bytes calldata data_) external;
    function safeBatchTransferFrom(address from_, address to_, uint256[] calldata ids_, uint256[] calldata amounts_, bytes calldata data_) external;
}

abstract contract ERCWithdrawable {
    /*  All functions in this abstract contract are marked as internal because it should
        generally be paired with ownable.
        Virtual is for overwritability.
    */
    function _withdrawERC20(address contractAddress_, uint256 amount_) internal virtual {
        IERC20(contractAddress_).transferFrom(address(this), msg.sender, amount_);
    }
    function _withdrawERC721(address contractAddress_, uint256 tokenId_) internal virtual {
        IERC721(contractAddress_).transferFrom(address(this), msg.sender, tokenId_);
    }
    function _withdrawERC1155(address contractAddress_, uint256 tokenId_, uint256 amount_) internal virtual {
        IERC1155(contractAddress_).safeTransferFrom(address(this), msg.sender, tokenId_, amount_, "");
    }
    function _withdrawERC1155Batch(address contractAddress_, uint256[] calldata ids_, uint256[] calldata amounts_) internal virtual {
        IERC1155(contractAddress_).safeBatchTransferFrom(address(this), msg.sender, ids_, amounts_, "");
    }
}

// Open0x Ownable
abstract contract Ownable {
    address public owner;
    
    event OwnershipTransferred(address indexed oldOwner_, address indexed newOwner_);

    constructor() { owner = msg.sender; }

    modifier onlyOwner {
        require(owner == msg.sender, "Ownable: caller is not the owner");
        _;
    }

    function _transferOwnership(address newOwner_) internal virtual {
        address _oldOwner = owner;
        owner = newOwner_;
        emit OwnershipTransferred(_oldOwner, newOwner_);    
    }

    function transferOwnership(address newOwner_) public virtual onlyOwner {
        require(newOwner_ != address(0x0), "Ownable: new owner is the zero address!");
        _transferOwnership(newOwner_);
    }

    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0x0));
    }
}

interface iRenderer {
    function tokenURI(uint256 tokenId_) external view returns (string memory);
}

interface iYield {
    function updateReward(address from_, address to_, uint256 tokenId_) external;
}

contract COCA is ERC721, Ownable, ERCWithdrawable {
    // Constructor
    constructor() ERC721("The Club of Cool Apes", "COCA") {}


    // Project Constraints
    uint16 immutable public maxTokens = 3000;
    
    // General NFT Variables
    uint256 public totalSupply;

    string internal baseTokenURI = "ipfs://QmeBMgzfLWM5BY5pPAwYJ5XTSaeSmeQMBQQo2eKHaXurGq/";

    // Interfaces
    iYield public yieldToken;

    // Whitelist Mappings
    mapping(address => uint16) public addressToPublicMinted;

    // Events
    event Mint(address to_, uint256 tokenId_);
    event NameChange(uint256 tokenId_, string name_);
    event BioChange(uint256 tokenId_, string bio_);

    // Contract Governance
    function withdrawEther() external onlyOwner {
        payable(msg.sender).transfer(address(this).balance); }
    

    function publicMint(uint16 amount) external payable onlySender publicSale {
        require(amount <= 3, "Max 3 mints per transaction");
        require(addressToPublicMinted[msg.sender] < 12, "Max 12 mints per account!");
        require(maxTokens > totalSupply, "No more remaining tokens!");

        uint priceRemaining = msg.value; 
        for (uint16 i = 0; i < amount; i++) {
            uint currentPrice = getPrice(__getTokenId());
            require(priceRemaining >= currentPrice, "Invalid value sent!");
            _mint(msg.sender, __getTokenId());
            emit Mint(msg.sender, __getTokenId());
            addressToPublicMinted[msg.sender]++;
            totalSupply++;
            priceRemaining -= currentPrice;
        }
    }

    // ERCWithdrawable
    function withdrawERC20(address contractAddress_, uint256 amount_) external onlyOwner {
        _withdrawERC20(contractAddress_, amount_);
    }
    function withdrawERC721(address contractAddress_, uint256 tokenId_) external onlyOwner {
        _withdrawERC721(contractAddress_, tokenId_);
    }


    // Public Sale
    bool public publicSaleEnabled;
    uint256 public publicSaleTime;
    function setPublicSale(bool bool_) external onlyOwner {
        publicSaleEnabled = bool_; }
    modifier publicSale {
        require(publicSaleEnabled, "Public Sale is not open yet!"); _; }
    function publicSaleIsEnabled() public view returns (bool) {
        return (publicSaleEnabled); }

    // Modifiers
    modifier onlySender {
        require(msg.sender == tx.origin, "No smart contracts!"); _; }

    // Contract Administration
    function setYieldToken(address address_) external onlyOwner {
        yieldToken = iYield(address_); }
    function setBaseTokenURI(string memory uri_) external onlyOwner {
        baseTokenURI = uri_; }
    
    // Mint functions
    function __getTokenId() internal view returns (uint256) {
        return totalSupply + 1;
    }

    function getPrice(uint256 tokenId) internal pure returns (uint256) {
        if (tokenId <= 500) {
            return 0;
        }
        
        return 0.027 ether;
    }

    // Transfer Hooks 
    function transferFrom(address from_, address to_, uint256 tokenId_) public override {
        if ( yieldToken != iYield(address(0x0)) ) {
            yieldToken.updateReward(from_, to_, tokenId_);
        }
        ERC721.transferFrom(from_, to_, tokenId_);
    }
    function safeTransferFrom(address from_, address to_, uint256 tokenId_, bytes memory data_) public override {
        if ( yieldToken != iYield(address(0x0)) ) {
            yieldToken.updateReward(from_, to_, tokenId_);
        }
        ERC721.safeTransferFrom(from_, to_, tokenId_, data_);
    }