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

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

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

    // data
    var CustomConversions = [

        { catalystBooster: 'minecraft:air', catalystBlock: 'simplefluidgenerators:basic_lava_generator', output: 'minecraft:lava', qty: 1, description: 'Tick' },
        { catalystBooster: 'gtceu:cupronickel_coil_block', catalystBlock: 'simplefluidgenerators:basic_lava_generator', output: 'minecraft:lava', qty: 2, description: 'Tick' },
        { catalystBooster: 'gtceu:kanthal_coil_block', catalystBlock: 'simplefluidgenerators:basic_lava_generator', output: 'minecraft:lava', qty: 5, description: 'Tick' },
        { catalystBooster: 'gtceu:nichrome_coil_block', catalystBlock: 'simplefluidgenerators:basic_lava_generator', output: 'minecraft:lava', qty: 10, description: 'Tick' },
        { catalystBooster: 'gtceu:rtm_alloy_coil_block', catalystBlock: 'simplefluidgenerators:basic_lava_generator', output: 'minecraft:lava', qty: 15, description: 'Tick' },
        { catalystBooster: 'gtceu:hssg_coil_block', catalystBlock: 'simplefluidgenerators:basic_lava_generator', output: 'minecraft:lava', qty: 20, description: 'Tick' },
        { catalystBooster: 'minecraft:air', catalystBlock: 'simplefluidgenerators:basic_water_generator', output: 'minecraft:water', qty: 100, description: 'Tick' },
        { catalystBooster: 'gtceu:cupronickel_coil_block', catalystBlock: 'simplefluidgenerators:basic_water_generator', output: 'minecraft:water', qty: 200, description: 'Tick' },
        { catalystBooster: 'gtceu:kanthal_coil_block', catalystBlock: 'simplefluidgenerators:basic_water_generator', output: 'minecraft:water', qty: 400, description: 'Tick' },
        { catalystBooster: 'gtceu:nichrome_coil_block', catalystBlock: 'simplefluidgenerators:basic_water_generator', output: 'minecraft:water', qty: 600, description: 'Tick' },
        { catalystBooster: 'gtceu:rtm_alloy_coil_block', catalystBlock: 'simplefluidgenerators:basic_water_generator', output: 'minecraft:water', qty: 800, description: 'Tick' },
        { catalystBooster: 'gtceu:hssg_coil_block', catalystBlock: 'simplefluidgenerators:basic_water_generator', output: 'minecraft:water', qty: 1000, description: 'Tick' },
    ];

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

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

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

            var inStack = Item.of(String(recipe.data.catalystBooster));
            var catalystStack = Item.of(String(recipe.data.catalystBlock));
            var outFluidId = String(recipe.data.output);
            var outFluidQty = (recipe.data.qty);

            builder.addSlot('CATALYST', 7, 34)
                .addItemStack(inStack)
                .setSlotName('catalyst');

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

            builder.addSlot('OUTPUT', 7, 0)
                .addFluidStack(outFluidId, outFluidQty)
                .setSlotName('output');

            builder.addInvisibleIngredients('CATALYST').addItemStack(inStack);
            builder.addInvisibleIngredients('CATALYST').addItemStack(catalystStack);
            builder.addInvisibleIngredients('OUTPUT').addFluidStack(outFluidId, outFluidQty);

        } 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('Simple Fluid Generators')
                .background(guiHelper.createBlankDrawable(100, 50))
                .icon(guiHelper.createDrawableItemStack(Item.of('simplefluidgenerators:basic_lava_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({
                catalystBooster: entry.catalystBooster,
                catalystBlock: entry.catalystBlock,
                output: entry.output,
                qty: entry.qty,
                description: entry.description || ''
            });
        }

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