package net.yyc12345.teyvatcraft.init;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import net.fabricmc.fabric.api.object.builder.v1.trade.TradeOfferHelper;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.village.VillagerProfession;
import net.yyc12345.teyvatcraft.mixin.VillagerAccess;
import net.yyc12345.teyvatcraft.trading.VillagerTradingBuilder;

public class VillagerManager {

    public static void RegisterAll() {
        // register villager food
        ImmutableMap.Builder<Item, Integer> villagerFoodItems = new ImmutableMap.Builder<Item, Integer>().putAll(VillagerAccess.getItemFoodValues());
        villagerFoodItems.put(ItemsManager.RICE, ItemsManager.RICE.getFoodComponent().getHunger());
        villagerFoodItems.put(ItemsManager.TOMATO, ItemsManager.TOMATO.getFoodComponent().getHunger());
        villagerFoodItems.put(ItemsManager.ONION, ItemsManager.ONION.getFoodComponent().getHunger());
        villagerFoodItems.put(ItemsManager.CABBAGE, ItemsManager.CABBAGE.getFoodComponent().getHunger());
        villagerFoodItems.put(ItemsManager.RADISH, ItemsManager.RADISH.getFoodComponent().getHunger());
        VillagerAccess.setItemFoodValues(villagerFoodItems.build());

        // register villager gatherables
        ImmutableSet.Builder<Item> villagerGatherables= new ImmutableSet.Builder<Item>().addAll(VillagerAccess.getGatherableItems());
        villagerGatherables.add(ItemsManager.RICE);
        villagerGatherables.add(ItemsManager.TOMATO);
        villagerGatherables.add(ItemsManager.ONION);
        villagerGatherables.add(ItemsManager.CABBAGE);
        villagerGatherables.add(ItemsManager.RADISH);
        VillagerAccess.setGatherableItems(villagerGatherables.build());

        // register trading
        TradeOfferHelper.registerVillagerOffers(VillagerProfession.FARMER, 1, (factories -> {
            // crops
            VillagerTradingBuilder.VillagerBuyVanilla(factories, 1, ItemsManager.RADISH, 26, 1);
            VillagerTradingBuilder.VillagerBuyVanilla(factories, 1, ItemsManager.TOMATO, 26, 1);
            VillagerTradingBuilder.VillagerBuyVanilla(factories, 1, ItemsManager.ONION, 26, 1);
            VillagerTradingBuilder.VillagerBuyVanilla(factories, 1, ItemsManager.CABBAGE, 26, 1);
            VillagerTradingBuilder.VillagerBuyVanilla(factories, 1, ItemsManager.RICE, 26, 1);
        }));
        TradeOfferHelper.registerVillagerOffers(VillagerProfession.FARMER, 3, (factories -> {
            // sold items
            VillagerTradingBuilder.VillagerSellVanilla(factories, 3, ItemsManager.ALMOND, 1, 6);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 3, ItemsManager.TOFU, 1, 6);
        }));
        TradeOfferHelper.registerVillagerOffers(VillagerProfession.FARMER, 5, (factories -> {
            VillagerTradingBuilder.VillagerProcessVanilla(factories, 5, Items.BAMBOO, ItemsManager.BAMBOO_SHOOT, 2);
        }));

        TradeOfferHelper.registerVillagerOffers(VillagerProfession.BUTCHER, 3, (factories -> {
            // soled items
            VillagerTradingBuilder.VillagerSellVanilla(factories, 3, ItemsManager.PEPPER, 1, 1);
            VillagerTradingBuilder.VillagerProcessVanilla(factories, 3, Items.CHICKEN, ItemsManager.SMOKED_FOWL, 2);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 3, ItemsManager.SALT, 1, 1);
        }));

        TradeOfferHelper.registerVillagerOffers(VillagerProfession.FISHERMAN, 4, (factories -> {
            VillagerTradingBuilder.VillagerSellVanilla(factories, 4, ItemsManager.SHRIMP_MEAT, 1, 6);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 4, ItemsManager.CRAB, 1, 6);
        }));

        TradeOfferHelper.registerVillagerOffers(VillagerProfession.CLERIC, 3, (factories -> {
            VillagerTradingBuilder.VillagerSellVanilla(factories, 3, ItemsManager.FROG, 1, 6);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 3, ItemsManager.LIZARD_TAIL, 1, 6);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 3, ItemsManager.BUTTERFLY_WINGS, 1, 6);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 3, ItemsManager.CRYSTAL_CORE, 1, 6);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 3, ItemsManager.LUMINESCENT_SPINE, 1, 6);
        }));
        TradeOfferHelper.registerVillagerOffers(VillagerProfession.CLERIC, 4, (factories -> {
            VillagerTradingBuilder.VillagerSellVanilla(factories, 4, ItemsManager.MIST_FLOWER_COROLLA, 1, 6);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 4, ItemsManager.FLAMING_FLOWER_STAMEN, 1, 6);
        }));
        TradeOfferHelper.registerVillagerOffers(VillagerProfession.CLERIC, 5, (factories -> {
            VillagerTradingBuilder.VillagerSellVanilla(factories, 5, ItemsManager.LOACH_PEARL, 1, 6);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 5, ItemsManager.STARCONCH, 1, 6);
        }));

        // register wandering trading
        TradeOfferHelper.registerWanderingTraderOffers(2, (factories -> {
            VillagerTradingBuilder.VillagerSellVanilla(factories, 5, ItemsManager.RICE_SEED, 1, 2);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 5, ItemsManager.RADISH, 1, 2);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 5, ItemsManager.TOMATO, 1, 2);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 5, ItemsManager.ONION, 1, 2);
            VillagerTradingBuilder.VillagerSellVanilla(factories, 5, ItemsManager.CABBAGE, 1, 2);
        }));
    }
}
