/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.datagen.provider;

import net.minecraft.data.loot.LootTableSubProvider;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.state.properties.WoodType;
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.entries.LootItem;
import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
import net.minecraft.world.level.storage.loot.functions.SetEnchantmentsFunction;
import net.minecraft.world.level.storage.loot.providers.number.UniformGenerator;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.eroticdungeongame.core.register.EDGEnchantments;
import top.r3944realms.eroticdungeongame.core.register.EDGItems;
import top.r3944realms.eroticdungeongame.util.WoodTypeHelper;
import top.r3944realms.lib39.util.lang.Tuple;

import java.util.Iterator;
import java.util.function.BiConsumer;

public class EDGChestLootTable implements LootTableSubProvider {
    @Override
    public void generate(@NotNull BiConsumer<ResourceLocation, LootTable.Builder> biConsumer) {
        biConsumer.accept(
                EroticDungeon.rl("chests/dungeon_village"),
                LootTable.lootTable()
                        .withPool(prepareDungeonVillage())
        );
    }
    private LootPool.Builder prepareDungeonVillage() {
        LootPool.Builder dungeon_village_pool = LootPool.lootPool()
                .name("dungeon_village_pool")
                .setRolls(UniformGenerator.between(15, 21))
                .add(LootItem.lootTableItem(Items.IRON_BARS)
                        .setWeight(1200))
                .add(LootItem.lootTableItem(Items.IRON_INGOT)
                        .setQuality(5)
                        .setWeight(1200)
                )
                .add(LootItem.lootTableItem(Items.EMERALD)
                        .setQuality(132)
                )
                .add(LootItem.lootTableItem(Items.GOLD_INGOT)
                        .setWeight(1100)
                )
                .add(LootItem.lootTableItem(Items.DIAMOND)
                        .setQuality(32)
                        .setWeight(600)
                );
        for (WoodType supportedWoodType : WoodTypeHelper.SUPPORTED_WOOD_TYPES) {
            Tuple allWoodItems = WoodTypeHelper.getAllWoodItems(supportedWoodType);
            Iterator<Object> iterator = allWoodItems.stream().iterator();
            while (iterator.hasNext()) {
                dungeon_village_pool.add(LootItem.lootTableItem((ItemLike) iterator.next()).setQuality(4));
            }
        }

        EDGItems.SEAT_ITEMS.forEach(i->dungeon_village_pool.add(LootItem.lootTableItem(i.get())));
        dungeon_village_pool.add(LootItem.lootTableItem(Items.ENCHANTED_BOOK)
                .setWeight(60)
                .apply(() -> createRandomEnchantment(EDGEnchantments.CARROT_AND_STICK.get(), 1.0f, 5.0f)));
        dungeon_village_pool.add(LootItem.lootTableItem(Items.ENCHANTED_BOOK)
                .setWeight(10)
                .apply(() -> createRandomEnchantment(Enchantments.UNBREAKING, 1.0f, 3.0f)));
        dungeon_village_pool.add(LootItem.lootTableItem(Items.ENCHANTED_BOOK)
                .setWeight(130)
                .apply(() -> createRandomEnchantment(Enchantments.SHARPNESS, 2.0f, 3.0f)));
        return dungeon_village_pool;
    }
    private LootItemFunction createRandomEnchantment(Enchantment enchantment, float min, float max) {
        if (min >= max) throw new IllegalArgumentException("min must be less than max");
        return new SetEnchantmentsFunction.Builder()
                .withEnchantment(enchantment, UniformGenerator.between(min, max))
                .build();
    }
}
