// File: @openzeppelin/contracts/access/Ownable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// File: @openzeppelin/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

// File: contracts/chimpers-middle.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

interface IERC1155 {
    function balanceOf(address account, uint256 id) external view returns (uint256);
    function safeTransferFrom(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes calldata data
    ) external;
}

interface IERC721 {
    function ownerOf(uint256 tokenId) external view returns (address);
}

contract TokenInterface is Ownable{
    IERC1155 public erc1155;
    IERC721 public erc721;

    mapping(uint256 => bool) public usedERC721Tokens;
    mapping(uint256 => uint256) public erc1155TotalBurnCounter;
    mapping(uint256 => uint256) public erc721ToBurnedErc1155;
    mapping(uint256 => uint256) public erc721BurnCount;
    address public gemStash = 0x94112A7B2aE525e9E370e2C2a80Ae3a48b69F780;

    event TokenEvolved(
        address indexed user,
        uint256 burnedErc1155TokenId,
        uint256 usedErc721TokenId
    );

    constructor() {
        erc1155 = IERC1155(0x7cC7ADd921e2222738561D03c89589929ceFcF21);
        erc721 = IERC721(0x80336Ad7A747236ef41F47ed2C7641828a480BAA);
    }

    function updateGemStash(address newGemStash) onlyOwner public {
        gemStash = newGemStash;
    }

    function ownsERC1155TokenView(address user, uint256 tokenId) public view returns (bool) {
        return erc1155.balanceOf(user, tokenId) > 0;
    }

    function ownsERC721View(address user, uint256 tokenId) public view returns (bool) {
        return erc721.ownerOf(tokenId) == user;
    }

    function evolveChimp(uint256 erc1155TokenId, uint256 erc721TokenId) public {
        require(ownsERC1155TokenView(msg.sender, erc1155TokenId), "You don't own the ERC-1155 token");
        require(ownsERC721View(msg.sender, erc721TokenId), "You don't own the ERC-721 token");
        require(erc1155TokenId == 12 || erc1155TokenId == 13 || erc1155TokenId == 14, "Invalid ERC-1155 token ID, you can only burn gems");
        require(!usedERC721Tokens[erc721TokenId], "ERC-721 token has already been used");

        usedERC721Tokens[erc721TokenId] = true;
        erc1155TotalBurnCounter[erc1155TokenId] += 1;

        erc1155.safeTransferFrom(msg.sender, gemStash, erc1155TokenId, 1, "0x");

        emit TokenEvolved(msg.sender, erc1155TokenId, erc721TokenId);
        erc721ToBurnedErc1155[erc721TokenId] = erc1155TokenId;
        erc721BurnCount[erc721TokenId] = erc1155TotalBurnCounter[erc1155TokenId];
    }

    function getBurnInfoForERC1155(uint256 erc1155TokenId) public view returns (uint256 totalBurned) {
        return erc1155TotalBurnCounter[erc1155TokenId];
    }

    function getBurnInfoForERC721(uint256 erc721TokenId) public view returns (uint256 erc1155TokenId, uint256 count) {
        erc1155TokenId = erc721ToBurnedErc1155[erc721TokenId];
        if (erc1155TokenId == 0) {
            return (0, 0);
        }
        return (erc1155TokenId, erc721BurnCount[erc721TokenId]);
    }
}