contract Factory is FactoryERC721, Ownable {
    using Strings for string;

    event Transfer(
        address indexed from,
        address indexed to,
        uint256 indexed tokenId
    );

    address public immutable proxyRegistryAddress;
    address public immutable nftAddress;
    string public baseURI;

    uint256 NFT_SUPPLY = 10_000;

    /*
     * Three different options for minting Nfts (basic, premium, and gold).
     */
    uint256 NUM_OPTIONS = 3;
    uint256 SINGLE_CREATURE_OPTION = 0;
    uint256 MULTIPLE_CREATURE_OPTION_1 = 1;
    uint256 MULTIPLE_CREATURE_OPTION_2 = 2;
    uint256 NUM_CREATURES_IN_MULTIPLE_CREATURE_OPTION_1 = 5;
    uint256 NUM_CREATURES_IN_MULTIPLE_CREATURE_OPTION_2 = 10;

    constructor(address _proxyRegistryAddress, address _nftAddress, string memory _baseURI) {
        proxyRegistryAddress = _proxyRegistryAddress;
        nftAddress = _nftAddress;
        baseURI = _baseURI;

        fireTransferEvents(address(0), owner());
    }

    function name() override external pure returns (string memory) {
        return "The Karma Chameleons Eggs Hatching";
    }

    function symbol() override external pure returns (string memory) {
        return "TKCEH";
    }

    function supportsFactoryInterface() override public pure returns (bool) {
        return true;
    }

    function numOptions() override public view returns (uint256) {
        return NUM_OPTIONS;
    }

    function transferOwnership(address newOwner) override public onlyOwner {
        address _prevOwner = owner();
        super.transferOwnership(newOwner);
        fireTransferEvents(_prevOwner, newOwner);
    }

    function fireTransferEvents(address _from, address _to) private {
        for (uint256 i = 0; i < NUM_OPTIONS; i++) {
            emit Transfer(_from, _to, i);
        }
    }

    function mint(uint256 _optionId, address _toAddress) override public {
        // Must be sent from the owner proxy or owner.
        ProxyRegistry proxyRegistry = ProxyRegistry(proxyRegistryAddress);
        assert(
            address(proxyRegistry.proxies(owner())) == _msgSender() ||
                owner() == _msgSender()
        );
        require(canMint(_optionId));

        Nft openSeaNft = Nft(nftAddress);
        if (_optionId == SINGLE_CREATURE_OPTION) {
            openSeaNft.mintTo(_toAddress);
        } else if (_optionId == MULTIPLE_CREATURE_OPTION_1) {
            for (
                uint256 i = 0;
                i < NUM_CREATURES_IN_MULTIPLE_CREATURE_OPTION_1;
                i++
            ) {
                openSeaNft.mintTo(_toAddress);
            }
        } else if (_optionId == MULTIPLE_CREATURE_OPTION_2) {
            for (
                uint256 i = 0;
                i < NUM_CREATURES_IN_MULTIPLE_CREATURE_OPTION_2;
                i++
            ) {
                openSeaNft.mintTo(_toAddress);
            }
        }
    }

    function canMint(uint256 _optionId) override public view returns (bool) {
        if (_optionId >= NUM_OPTIONS) {
            return false;
        }

        Nft openSeaNft = Nft(nftAddress);
        uint256 nftSupply = openSeaNft.totalSupply();

        uint256 numItemsAllocated = 0;
        if (_optionId == SINGLE_CREATURE_OPTION) {
            numItemsAllocated = 1;
        } else if (_optionId == MULTIPLE_CREATURE_OPTION_1) {
            numItemsAllocated = NUM_CREATURES_IN_MULTIPLE_CREATURE_OPTION_1;
        } else if (_optionId == MULTIPLE_CREATURE_OPTION_2) {
            numItemsAllocated = NUM_CREATURES_IN_MULTIPLE_CREATURE_OPTION_2;
        }
        return (nftSupply + numItemsAllocated) <= NFT_SUPPLY;
    }

    function tokenURI(uint256 _optionId) override external view returns (string memory) {
        return string(abi.encodePacked(baseURI, Strings.toString(_optionId)));
    }

    /**
     * Hack to get things to work automatically on OpenSea.
     * Use transferFrom so the frontend doesn't have to worry about different method names.
     */
    function transferFrom(
        address,
        address _to,
        uint256 _tokenId
    ) public {
        mint(_tokenId, _to);
    }

    /**
     * Hack to get things to work automatically on OpenSea.
     * Use isApprovedForAll so the frontend doesn't have to worry about different method names.
     */
    function isApprovedForAll(address _owner, address _operator)
        public
        view
        returns (bool)
    {
        if (owner() == _owner && _owner == _operator) {
            return true;
        }

        ProxyRegistry proxyRegistry = ProxyRegistry(proxyRegistryAddress);
        if (
            owner() == _owner &&
            address(proxyRegistry.proxies(_owner)) == _operator
        ) {
            return true;
        }

        return false;
    }

    /**
     * Hack to get things to work automatically on OpenSea.
     * Use isApprovedForAll so the frontend doesn't have to worry about different method names.
     */
    function ownerOf(uint256) public view returns (address _owner) {
        return owner();
    }
}
function ownerOf(uint256) public view returns (address _owner) {
    return owner();
}

/**
 * Hack to get things to work automatically on OpenSea.
 * Use isApprovedForAll so the frontend doesn't have to worry about different method names.
 */