// HAMMERS JEI RECIPES.js
// Forge 1.20.1
// Requires: JEI + KubeJS + KubeJS Additions
// CLIENT DISPLAY ONLY.

(function () {
    console.info('[MB] Loading custom JEI conversions (infinite_blocks)...');

    // unique per file
    var CATEGORY_ID = 'kubejs:infinite_blocks';

    // data
    var CustomConversions = [
        { input: 'infinite_blocks:blackest_stone', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'infinite_blocks:blackest_stone', description: 'Out' },
        { input: 'xtonesreworked:xtone_tile', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'xtonesreworked:xtone_tile', description: 'Out' },
        { input: 'laboratoryblocks:laboratory_block', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'laboratoryblocks:laboratory_block', description: 'Out' },
        { input: 'laboratoryblocks:pla_block', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'laboratoryblocks:pla_block', description: 'Out' },
        { input: 'factory_blocks:factory', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'factory_blocks:factory', description: 'Out' },
        { input: 'factory_blocks:sturdy', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'factory_blocks:sturdy', description: 'Out' },
        { input: 'minecraft:cobblestone', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:cobblestone', description: 'Out' },
        { input: 'minecraft:mossy_cobblestone', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:mossy_cobblestone', description: 'Out' },
        { input: 'minecraft:stone', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:stone', description: 'Out' },
        { input: 'minecraft:granite', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:granite', description: 'Out' },
        { input: 'minecraft:polished_granite', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:polished_granite', description: 'Out' },
        { input: 'minecraft:diorite', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:diorite', description: 'Out' },
        { input: 'minecraft:polished_diorite', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:polished_diorite', description: 'Out' },
        { input: 'minecraft:andesite', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:andesite', description: 'Out' },
        { input: 'minecraft:polished_andesite', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:polished_andesite', description: 'Out' },
        { input: 'gtceu:red_granite_cobblestone', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'gtceu:red_granite_cobblestone', description: 'Out' },
        { input: 'gtceu:red_granite', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'gtceu:red_granite', description: 'Out' },
        { input: 'gtceu:marble_cobblestone', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'gtceu:marble_cobblestone', description: 'Out' },
        { input: 'gtceu:marble', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'gtceu:marble', description: 'Out' },
        { input: 'minecraft:cobbled_deepslate', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:cobbled_deepslate', description: 'Out' },
        { input: 'minecraft:deepslate', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:deepslate', description: 'Out' },
        { input: 'minecraft:polished_deepslate', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:polished_deepslate', description: 'Out' },
        { input: 'minecraft:tuff', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:tuff', description: 'Out' },
        { input: 'minecraft:infested_cobblestone', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:infested_cobblestone', description: 'Out' },
        { input: 'minecraft:infested_stone', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:infested_stone', description: 'Out' },
        { input: 'minecraft:infested_deepslate', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:infested_deepslate', description: 'Out' },
        { input: 'minecraft:dirt', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:dirt', description: 'Out' },
        { input: 'minecraft:coarse_dirt', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:coarse_dirt', description: 'Out' },
        { input: 'minecraft:rooted_dirt', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:rooted_dirt', description: 'Out' },
        { input: 'minecraft:mud', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:mud', description: 'Out' },
        { input: 'minecraft:grass_block', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:grass_block', description: 'Out' },
        { input: 'minecraft:podzol', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:podzol', description: 'Out' },
        { input: 'minecraft:mycelium', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:mycelium', description: 'Out' },
        { input: 'minecraft:moss_block', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:moss_block', description: 'Out' },
        { input: 'minecraft:muddy_mangrove_roots', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:muddy_mangrove_roots', description: 'Out' },
    
        { input: 'minecraft:terracotta', catalystBlock: 'infinite_blocks:infinite_block_generator', output: 'minecraft:terracotta', description: 'Out' },
    ];

    // Add all dye terracotta variants
    Color.DYE.forEach(color => {
        CustomConversions.push({
            input: `minecraft:${color}_terracotta`,
            catalystBlock: 'infinite_blocks:infinite_block_generator',
            output: `minecraft:${color}_terracotta`,
            description: 'Out'
        });
    });

    // // Add all glazed terracotta variants
    // Color.DYE.forEach(color => {
    //     CustomConversions.push({
    //         input: `minecraft:${color}_glazed_terracotta`,
    //         catalystBlock: 'infinite_blocks:infinite_block_generator',
    //         output: `minecraft:${color}_glazed_terracotta`,
    //         description: 'Out'
    //     });
    // });

    // Add all dye concrete variants
    Color.DYE.forEach(color => {
        CustomConversions.push({
            input: `minecraft:${color}_concrete`,
            catalystBlock: 'infinite_blocks:infinite_block_generator',
            output: `minecraft:${color}_concrete`,
            description: 'Out'
        });
    });

    function err(msg) { console.error('[MB] ' + msg); }

    function verifyRecipe(recipe) {
        return !!(recipe && recipe.data && recipe.data.input && recipe.data.catalystBlock && recipe.data.output);
    }

    function handleLookup(builder, recipe, focuses) {
        try {
            if (!verifyRecipe(recipe)) return;

            var inStack = Item.of(String(recipe.data.input));
            var catalystStack = Item.of(String(recipe.data.catalystBlock));
            var outStack = Item.of(String(recipe.data.output));

            builder.addSlot('INPUT', 7, 0)
                .addItemStack(inStack)
                .setSlotName('input');

            builder.addSlot('CATALYST', 7, 17)
                .addItemStack(catalystStack)
                .setSlotName('catalyst');

            builder.addSlot('OUTPUT', 7, 34)
                .addItemStack(outStack)
                .setSlotName('output');

            builder.addInvisibleIngredients('INPUT').addItemStack(inStack);
            builder.addInvisibleIngredients('CATALYST').addItemStack(catalystStack);
            builder.addInvisibleIngredients('OUTPUT').addItemStack(outStack);

        } catch (e) {
            err('handleLookup error: ' + String(e));
        }
    }

    function render(recipe, recipeSlotsView, guiGraphics, mouseX, mouseY) {
        try {
            if (!verifyRecipe(recipe)) return;

            var desc = (recipe.data.description) ? String(recipe.data.description) : '';
            if (desc.length > 0) {
                guiGraphics.drawWordWrap(Client.font, Text.of(desc), 6, 51, 160, 50);
            }
        } catch (e) {
            err('draw error: ' + String(e));
        }
    }

    JEIAddedEvents.registerCategories(function (event) {
        var guiHelper = event.JEI_HELPERS.guiHelper;

        event.custom(CATEGORY_ID, function (category) {
            category.width = 30;
            category.height = 60;

            category
                .title('Infinite Blocks')
                .background(guiHelper.createBlankDrawable(100, 50))
                .icon(guiHelper.createDrawableItemStack(Item.of('infinite_blocks:infinite_block_generator')))
                .isRecipeHandled(function (recipe) { return verifyRecipe(recipe); })
                .handleLookup(function (builder, recipe, focuses) { handleLookup(builder, recipe, focuses); })
                .setDrawHandler(function (recipe, recipeSlotsView, guiGraphics, mouseX, mouseY) {
                    render(recipe, recipeSlotsView, guiGraphics, mouseX, mouseY);
                });
        });
    });

    JEIAddedEvents.registerRecipes(function (event) {
        var cat = event.custom(CATEGORY_ID);

        for (var i = 0; i < CustomConversions.length; i++) {
            var entry = CustomConversions[i];
            cat.add({
                input: entry.input,
                catalystBlock: entry.catalystBlock,
                output: entry.output,
                description: entry.description || ''
            });
        }

        console.info('[MB] Registered ' + CustomConversions.length + ' custom conversion JEI entries (' + CATEGORY_ID + ').');
    });
})();
