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

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

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

    // data
    var CustomConversions = [
        { input: 'minecraft:bucket', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:water_bucket', description: ' In                            Out' },
        { input: 'minecraft:dirt', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:mud', description: ' In                            Out' },
        { input: 'ex_hammers:dust', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:clay', description: ' In                            Out' },
        { input: 'gtceu:stone_dust', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:clay_ball', description: ' In                            Out' },
        { input: 'gtceu:andesite_dust', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:clay_ball', description: ' In                            Out' },
        { input: 'gtceu:diorite_dust', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:clay_ball', description: ' In                            Out' },
        { input: 'gtceu:granite_dust', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:clay_ball', description: ' In                            Out' },
        { input: 'gtceu:deepslate_dust', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:clay_ball', description: ' In                            Out' },
        { input: 'gtceu:basalt_dust', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:clay_ball', description: ' In                            Out' },
        { input: 'gtceu:netherrack_dust', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:clay_ball', description: ' In                            Out' },
        { input: 'gtceu:endstone_dust', catalystBlock: 'simple_resource_generators:simple_water_condenser', output: 'minecraft:clay_ball', description: ' In                            Out' },
        { input: 'minecraft:bucket', catalystBlock: 'simple_resource_generators:simple_lava_condenser', output: 'minecraft:lava_bucket', description: ' In                            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', 0, 0)
                .addItemStack(inStack)
                .setSlotName('input');

            builder.addSlot('CATALYST', 43, 0)
                .addItemStack(catalystStack)
                .setSlotName('catalyst');

            builder.addSlot('OUTPUT', 84, 0)
                .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), 0, 20, 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 = 100;
            category.height = 30;

            category
                .title('Fluid Condensers')
                .background(guiHelper.createBlankDrawable(100, 50))
                .icon(guiHelper.createDrawableItemStack(Item.of('simple_resource_generators:simple_water_condenser')))
                .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 + ').');
    });
})();
