const PAN = 'croptopia:frying_pan'
const COOKING_PAN = 'croptopia:cooking_pan'
const COOKING_POT = 'croptopia:cooking_pot'

const POT_RECIPE_OVERRIDES = {
    'croptopia:cabbage_roll': [
        { tag: 'croptopia:beef_replacements' },
        { tag: 'forge:onions' },
        { tag: 'forge:salts' },
        { tag: 'forge:cabbage' },
        { tag: 'forge:cabbage' },
        { tag: 'forge:rice' }
    ],
    'croptopia:cinnamon_roll': [
        { tag: 'forge:doughs' },
        { tag: 'forge:milks' },
        { item: 'minecraft:egg' },
        { tag: 'forge:butters' },
        { item: 'minecraft:sugar' },
        { tag: 'forge:cinnamon' }
    ],
    'croptopia:goulash': [
        { tag: 'croptopia:beef_replacements' },
        { tag: 'croptopia:pork_replacements' },
        { tag: 'forge:onions' },
        { tag: 'forge:cabbage' },
        { tag: 'forge:tomatoes' },
        { tag: 'forge:cabbage' }
    ],
    'croptopia:shaped_beef_wellington': [
        { item: 'minecraft:beef' },
        { item: 'minecraft:brown_mushroom' },
        { tag: 'forge:onions' },
        { tag: 'forge:mustard' },
        { item: 'minecraft:egg' },
        { tag: 'forge:flour' }
    ],
    'croptopia:shaped_ratatouille': [
        { tag: 'forge:tomatoes' },
        { tag: 'forge:onions' },
        { tag: 'forge:eggplants' },
        { tag: 'forge:zucchini' },
        { tag: 'forge:bellpeppers' },
        { tag: 'forge:basil' }
    ],
    'croptopia:shaped_shepherds_pie': [
        { tag: 'croptopia:beef_mutton' },
        { item: 'minecraft:potato' },
        { tag: 'forge:tomatoes' },
        { tag: 'forge:corn' },
        { tag: 'forge:garlic' },
        { tag: 'forge:onions' }
    ],
    'croptopia:shaped_stuffed_artichoke': [
        { tag: 'forge:artichokes' },
        { item: 'minecraft:bread' },
        { tag: 'forge:cheeses' },
        { tag: 'forge:olive_oils' },
        { tag: 'forge:lemons' },
        { tag: 'forge:salts' }
    ],
    'croptopia:the_big_breakfast': [
        { item: 'minecraft:egg' },
        { item: 'croptopia:bacon' },
        { item: 'croptopia:hashed_brown' },
        { item: 'croptopia:toast' },
        { item: 'croptopia:baked_beans' },
        { tag: 'forge:sausages' }
    ]
}



function getIngredients(json) {
    // shapeless
    if (json.ingredients) {
        return json.ingredients
    }

    // shaped
    if (json.pattern && json.key) {
        const list = []

        for (const row of json.pattern) {
            for (const symbol of row.split("")) {
                if (symbol === " ") continue
                let ing = json.key[symbol]
                if (ing) list.push(ing)
            }
        }
        return list
    }

    // smelting / campfire
    if (json.ingredient) {
        return [json.ingredient]
    }

    return [];
}



ServerEvents.recipes(event => {
    event.forEachRecipe({ mod: 'croptopia' }, recipe => {

        const recipeId = recipe.getId().toString()
        const json = JSON.parse(recipe.json.toString())

        const ingList = getIngredients(json)

        const usesPan = ingList.some(i => i?.item === PAN)
        if (!usesPan) return

        event.remove({ id: recipe.getId() })

        const newIngredients = POT_RECIPE_OVERRIDES[recipeId] ?? ingList.filter(i => i?.item !== PAN)

        const resultItem = json.result?.item ?? json.result
        const resultCount = Math.max(1, json.result?.count ?? 1);

        const resultItems = Array.isArray(resultItem)
            ? resultItem.map(r => r.item)
            : [resultItem.item];

        const result = { item: resultItem, count: resultCount }

        event.custom({
            type: "farmersdelight:cooking",
            ingredients: newIngredients,
            result: result,
            cookingtime: 200,
            experience: 0.0
        }).id(`kubejs:fd_pot/${recipeId.replace(':', '_')}`)

        event.custom({
            type: "create:mixing",
            heatRequirement: "heated",
            ingredients: newIngredients,
            results: [
                result
            ],
            processingTime: 200
        }).id(`kubejs:mixing/farmersdelight_pot/${recipeId.replace(':', '/')}`)
    })

    event.remove({ output: PAN })
    event.remove({ output: COOKING_PAN })
    event.remove({ output: COOKING_POT })
})
