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

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

    address public proxyRegistryAddress;
    address public nftAddress;
    string baseURI;

    uint256 HEADZ_SUPPLY = 3000;

    uint256 NUM_OPTIONS = 4;
    uint256 MINT1_OPTION = 0;
    uint256 MINT3_OPTION = 1;
    uint256 MINT5_OPTION = 2;
    uint256 MINT10_OPTION = 3;

    constructor(address _proxyRegistryAddress, address _nftAddress, string memory _initBaseURI) {
        proxyRegistryAddress = _proxyRegistryAddress;
        nftAddress = _nftAddress;
        setBaseURI(_initBaseURI);
        fireTransferEvents(address(0), owner());
    }

    function name() override external pure returns (string memory) {
        return "theMETALHEADZ-Minter";
    }

    function symbol() override external pure returns (string memory) {
        return "MTLHDZ-Minter";
    }

    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 calcMintCount(uint256 _optionId) public view returns (uint256) {
        uint256 mintCount = 0;

        if (_optionId == MINT1_OPTION) {
            mintCount = 1;
        } else if (_optionId == MINT3_OPTION) {
            mintCount = 3;
        } else if (_optionId == MINT5_OPTION) {
            mintCount = 5;
        } else if (_optionId == MINT10_OPTION) {
            mintCount = 10;
        }

        return mintCount;
    }


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

        MetalHead metalHead = MetalHead(nftAddress);
        uint256 mintCount = calcMintCount(_optionId);
        for (uint256 i = 0; i < mintCount; i++) {
            metalHead.mintTo(_toAddress);
        }
    }

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

        MetalHead metalHead = MetalHead(nftAddress);
        uint256 headzSupply = metalHead.totalSupply();
        uint256 mintCount = calcMintCount(_optionId);
        return headzSupply <= (HEADZ_SUPPLY - mintCount);
    }

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

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

    /**
     * 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 _from,
        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 _tokenId) public view returns (address _owner) {
        return owner();
    }
}
function ownerOf(uint256 _tokenId) 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.
 */