const ResourceLocation = Java.loadClass('net.minecraft.resources.ResourceLocation')
const BuiltInRegistries = Java.loadClass('net.minecraft.core.registries.BuiltInRegistries')

const WOODWORKS_OUTPUTS = {
    slab: { planks: 2, logs: 8 },
    wall: { planks: 1, logs: 4 },
    stairs: { planks: 1, logs: 4 },
    fence: { planks: 1, logs: 4 },
    fence_gate: { planks: 1, logs: 1 },
    door: { planks: 1, logs: 2 },
    trapdoor: { planks: 1, logs: 2 },
    button: { planks: 1, logs: 4 },
    pressure_plate: { planks: 1, logs: 2 }
}

const WOOD_FAMILIES = [
    { mod: 'minecraft', name: 'oak', logTag: 'minecraft:oak_logs' },
    { mod: 'minecraft', name: 'spruce', logTag: 'minecraft:spruce_logs' },
    { mod: 'minecraft', name: 'birch', logTag: 'minecraft:birch_logs' },
    { mod: 'minecraft', name: 'jungle', logTag: 'minecraft:jungle_logs' },
    { mod: 'minecraft', name: 'acacia', logTag: 'minecraft:acacia_logs' },
    { mod: 'minecraft', name: 'dark_oak', logTag: 'minecraft:dark_oak_logs' },
    { mod: 'minecraft', name: 'mangrove', logTag: 'minecraft:mangrove_logs' },
    { mod: 'minecraft', name: 'cherry', logTag: 'minecraft:cherry_logs' },
    { mod: 'minecraft', name: 'bamboo', logTag: 'minecraft:bamboo_blocks' },
    { mod: 'minecraft', name: 'bamboo_mosaic', planks: 'minecraft:bamboo_mosaic' },
    { mod: 'minecraft', name: 'crimson', logTag: 'minecraft:crimson_stems' },
    { mod: 'minecraft', name: 'warped', logTag: 'minecraft:warped_stems' },

    { mod: 'alexscaves', name: 'pewen', logTag: 'alexscaves:pewen_logs' },
    { mod: 'alexscaves', name: 'thornwood', logTag: 'alexscaves:thornwood_logs' },

    { mod: 'caverns_and_chasms', name: 'azalea', logTag: 'caverns_and_chasms:azalea_logs' },

    { mod: 'croptopia', name: 'cinnamon', logTag: 'croptopia:cinnamon_logs' },

    { mod: 'enlightened_end', name: 'cerulean', logTag: 'enlightened_end:cerulean_logs' },
    { mod: 'enlightened_end', name: 'indigo' },

    { mod: 'quark', name: 'azalea', logTag: 'quark:azalea_logs' },
    { mod: 'quark', name: 'blossom', logTag: 'quark:blossom_logs' },
    { mod: 'quark', name: 'ancient', logTag: 'quark:ancient_logs' },

    { mod: 'regions_unexplored', name: 'alpha' },
    { mod: 'regions_unexplored', name: 'baobab', logTag: 'regions_unexplored:baobab_logs' },
    { mod: 'regions_unexplored', name: 'blackwood', logTag: 'regions_unexplored:blackwood_logs' },
    { mod: 'regions_unexplored', name: 'blue_bioshroom', logTag: 'regions_unexplored:blue_bioshroom_logs' },
    { mod: 'regions_unexplored', name: 'brimwood', logTag: 'regions_unexplored:brimwood_logs' },
    { mod: 'regions_unexplored', name: 'cobalt', logTag: 'regions_unexplored:cobalt_logs' },
    { mod: 'regions_unexplored', name: 'cypress', logTag: 'regions_unexplored:cypress_logs' },
    { mod: 'regions_unexplored', name: 'dead', logTag: 'regions_unexplored:dead_logs' },
    { mod: 'regions_unexplored', name: 'eucalyptus', logTag: 'regions_unexplored:eucalyptus_logs' },
    { mod: 'regions_unexplored', name: 'green_bioshroom', logTag: 'regions_unexplored:green_bioshroom_logs' },
    { mod: 'regions_unexplored', name: 'joshua', logTag: 'regions_unexplored:joshua_logs' },
    { mod: 'regions_unexplored', name: 'kapok', logTag: 'regions_unexplored:kapok_logs' },
    { mod: 'regions_unexplored', name: 'larch', logTag: 'regions_unexplored:larch_logs' },
    { mod: 'regions_unexplored', name: 'magnolia' },
    { mod: 'regions_unexplored', name: 'maple', logTag: 'regions_unexplored:maple_logs' },
    { mod: 'regions_unexplored', name: 'mauve', logTag: 'regions_unexplored:mauve_logs' },
    { mod: 'regions_unexplored', name: 'palm', logTag: 'regions_unexplored:palm_logs' },
    { mod: 'regions_unexplored', name: 'pine', logTag: 'regions_unexplored:pine_logs' },
    { mod: 'regions_unexplored', name: 'pink_bioshroom', logTag: 'regions_unexplored:pink_bioshroom_logs' },
    { mod: 'regions_unexplored', name: 'redwood', logTag: 'regions_unexplored:redwood_logs' },
    { mod: 'regions_unexplored', name: 'socotra', logTag: 'regions_unexplored:socotra_logs' },
    { mod: 'regions_unexplored', name: 'willow', logTag: 'regions_unexplored:willow_logs' },
    { mod: 'regions_unexplored', name: 'yellow_bioshroom', logTag: 'regions_unexplored:yellow_bioshroom_logs' },

    { mod: 'regions_unexplored', name: 'black_painted' },
    { mod: 'regions_unexplored', name: 'blue_painted' },
    { mod: 'regions_unexplored', name: 'brown_painted' },
    { mod: 'regions_unexplored', name: 'cyan_painted' },
    { mod: 'regions_unexplored', name: 'gray_painted' },
    { mod: 'regions_unexplored', name: 'green_painted' },
    { mod: 'regions_unexplored', name: 'light_blue_painted' },
    { mod: 'regions_unexplored', name: 'light_gray_painted' },
    { mod: 'regions_unexplored', name: 'lime_painted' },
    { mod: 'regions_unexplored', name: 'magenta_painted' },
    { mod: 'regions_unexplored', name: 'orange_painted' },
    { mod: 'regions_unexplored', name: 'pink_painted' },
    { mod: 'regions_unexplored', name: 'purple_painted' },
    { mod: 'regions_unexplored', name: 'red_painted' },
    { mod: 'regions_unexplored', name: 'white_painted' },
    { mod: 'regions_unexplored', name: 'yellow_painted' }
]

function itemExists(id) {
    return BuiltInRegistries.ITEM.containsKey(new ResourceLocation(id))
}

function sawmill(event, result, ingredient, count, idSuffix) {
    event.custom({
        type: 'woodworks:sawmill',
        ingredient: ingredient,
        result: result,
        count: count
    }).id(`kubejs:woodworks/sawmill/${idSuffix}`)
}

ServerEvents.recipes(event => {
    WOOD_FAMILIES.forEach(family => {
        const planks = family.planks || `${family.mod}:${family.name}_planks`
        if (!itemExists(planks)) return

        Object.entries(WOODWORKS_OUTPUTS).forEach(([suffix, counts]) => {
            const result = `${family.mod}:${family.name}_${suffix}`
            if (!itemExists(result)) return

            event.remove({ type: 'minecraft:crafting_shaped', output: result })
            event.remove({ type: 'minecraft:crafting_shapeless', output: result })
            event.remove({ type: 'woodworks:sawmill', output: result })

            const idName = result.replace(':', '/')
            sawmill(event, result, { item: planks }, counts.planks, `${idName}_from_planks`)

            if (family.logTag) {
                sawmill(event, result, { tag: family.logTag }, counts.logs, `${idName}_from_logs`)
            }
        })
    })

    event.remove({
        id: /^(woodworks|caverns_and_chasms|st_wqc):.*_(slab|wall|stairs|fence|fence_gate|door|trapdoor|button|pressure_plate)_from_.*_sawing$/
    })
})
