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

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

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

    // data
    var CustomConversions = [
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'minecraft:wheat_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'minecraft:pumpkin_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'minecraft:melon_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'minecraft:beetroot_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'ex_hammers:grass_seed', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'farmersdelight:rice', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'farmersdelight:cabbage_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'farmersdelight:tomato_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'vinery:red_grape_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'vinery:white_grape_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'vinery:savanna_grape_seeds_red', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'vinery:savanna_grape_seeds_white', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'vinery:taiga_grape_seeds_red', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'vinery:taiga_grape_seeds_white', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'vinery:jungle_grape_seeds_red', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'vinery:jungle_grape_seeds_white', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'bakery:strawberry_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'bakery:oat_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'brewery:hops_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'brewery:barley_seeds', description: 'Random' },    
        { catalystBlock: 'simple_resource_generators:random_seed_generator', output: 'brewery:corn_seeds', description: 'Random' },    
    ];

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

    function verifyRecipe(recipe) {
        return !!(recipe && recipe.data && 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', 11, 0)
                .addItemStack(catalystStack)
                .setSlotName('catalyst');

            builder.addSlot('OUTPUT', 11, 17)
                .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), 1, 40, 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 = 40;
            category.height = 50;

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