package xyz.quickbasic.tieredrepairkits;

import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import xyz.quickbasic.tieredrepairkits.config.RepairTier;
import xyz.quickbasic.tieredrepairkits.config.RecipeIngredient;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

// Main config class for TieredRepairKits
public class TRKConfig {

    public static final BuilderCodec<TRKConfig> CODEC = BuilderCodec.builder(TRKConfig.class, TRKConfig::new)

            .append(new KeyedCodec<RepairTier>("TierCopper", RepairTier.CODEC),
                    (cfg, val) -> cfg.tierCopper = val,
                    cfg -> cfg.tierCopper).add()

            .append(new KeyedCodec<RepairTier>("TierIron", RepairTier.CODEC),
                    (cfg, val) -> cfg.tierIron = val,
                    cfg -> cfg.tierIron).add()

            .append(new KeyedCodec<RepairTier>("TierSilver", RepairTier.CODEC),
                    (cfg, val) -> cfg.tierSilver = val,
                    cfg -> cfg.tierSilver).add()

            .append(new KeyedCodec<RepairTier>("TierGold", RepairTier.CODEC),
                    (cfg, val) -> cfg.tierGold = val,
                    cfg -> cfg.tierGold).add()

            .append(new KeyedCodec<RepairTier>("TierThorium", RepairTier.CODEC),
                    (cfg, val) -> cfg.tierThorium = val,
                    cfg -> cfg.tierThorium).add()

            .append(new KeyedCodec<RepairTier>("TierCobalt", RepairTier.CODEC),
                    (cfg, val) -> cfg.tierCobalt = val,
                    cfg -> cfg.tierCobalt).add()

            .append(new KeyedCodec<RepairTier>("TierAdamantite", RepairTier.CODEC),
                    (cfg, val) -> cfg.tierAdamantite = val,
                    cfg -> cfg.tierAdamantite).add()

            .append(new KeyedCodec<RepairTier>("TierMithril", RepairTier.CODEC),
                    (cfg, val) -> cfg.tierMithril = val,
                    cfg -> cfg.tierMithril).add()

            .append(new KeyedCodec<RepairTier>("TierOnyxium", RepairTier.CODEC),
                    (cfg, val) -> cfg.tierOnyxium = val,
                    cfg -> cfg.tierOnyxium).add()

            .append(new KeyedCodec<RepairTier>("TierPrisma", RepairTier.CODEC),
                    (cfg, val) -> cfg.tierPrisma = val,
                    cfg -> cfg.tierPrisma).add()

            .build();

    // --- Tier fields ---
    private RepairTier tierCopper = new RepairTier();
    private RepairTier tierIron = new RepairTier();
    private RepairTier tierSilver = new RepairTier();
    private RepairTier tierGold = new RepairTier();
    private RepairTier tierThorium = new RepairTier();
    private RepairTier tierCobalt = new RepairTier();
    private RepairTier tierAdamantite = new RepairTier();
    private RepairTier tierMithril = new RepairTier();
    private RepairTier tierOnyxium = new RepairTier();
    private RepairTier tierPrisma = new RepairTier();

    // Cached map for runtime convenience
    private Map<String, RepairTier> cachedMap;

    public TRKConfig() {

        // TierCopper
        tierCopper.setEnabled(true);
        tierCopper.setRepairPenalty(0.05);
        tierCopper.setRecipe(list(
                ing("Ingredient_Fibre", 2),
                ing("Ingredient_Bar_Copper", 4),
                ing("Ingredient_Stick", 2)
        ));

        // TierIron
        tierIron.setEnabled(true);
        tierIron.setRepairPenalty(0);
        tierIron.setRecipe(list(
                ing("Ingredient_Fabric_Scrap_Linen", 2),
                ing("Ingredient_Bar_Iron", 8),
                ing("Ingredient_Leather_Light", 2)
        ));

        // TierSilver
        tierSilver.setEnabled(true);
        tierSilver.setRepairPenalty(-0.01);
        tierSilver.setRecipe(list(
                ing("Ingredient_Bolt_Wool", 4),
                ing("Ingredient_Bar_Silver", 16),
                ing("Ingredient_Fire_Essence", 4)
        ));

        // TierGold
        tierGold.setEnabled(true);
        tierGold.setRepairPenalty(-0.01);
        tierGold.setRecipe(list(
                ing("Ingredient_Bolt_Wool", 4),
                ing("Ingredient_Bar_Gold", 16),
                ing("Ingredient_Fire_Essence", 4)
        ));

        // TierThorium
        tierThorium.setEnabled(true);
        tierThorium.setRepairPenalty(-0.025);
        tierThorium.setRecipe(list(
                ing("Ingredient_Chitin_Sturdy", 2),
                ing("Ingredient_Bar_Thorium", 16),
                ing("Ingredient_Sac_Venom", 2)
        ));

        // TierCobalt
        tierCobalt.setEnabled(true);
        tierCobalt.setRepairPenalty(-0.05);
        tierCobalt.setRecipe(list(
                ing("Ingredient_Fabric_Scrap_Shadoweave", 4),
                ing("Ingredient_Bar_Cobalt", 16),
                ing("Ingredient_Ice_Essence", 4)
        ));

        // TierAdamantite
        tierAdamantite.setEnabled(true);
        tierAdamantite.setRepairPenalty(-0.075);
        tierAdamantite.setRecipe(list(
                ing("Ingredient_Crystal_Red", 4),
                ing("Ingredient_Bar_Adamantite", 16),
                ing("Ingredient_Fire_Essence", 4)
        ));

        // TierMithril
        tierMithril.setEnabled(true);
        tierMithril.setRepairPenalty(-0.1);
        tierMithril.setRecipe(list(
                ing("Ingredient_Voidheart", 4),
                ing("Ingredient_Bar_Mithril", 16),
                ing("Ingredient_Leather_Storm", 4)
        ));

        // TierOnyxium
        tierOnyxium.setEnabled(true);
        tierOnyxium.setRepairPenalty(-0.125);
        tierOnyxium.setRecipe(list(
                ing("Rock_Gem_Voidstone", 1),
                ing("Ingredient_Bar_Onyxium", 16),
                ing("Ingredient_Leather_Storm", 4)
        ));

        // TierPrisma
        tierPrisma.setEnabled(true);
        tierPrisma.setRepairPenalty(-0.15);
        tierPrisma.setRecipe(list(
                ing("Rock_Gem_Diamond", 1),
                ing("Ingredient_Bar_Prisma", 16),
                ing("Ingredient_Leather_Storm", 4)
        ));
    }

    public Map<String, RepairTier> getTiers() {
        if (cachedMap == null) {
            cachedMap = new LinkedHashMap<>();
            cachedMap.put("TierCopper", tierCopper);
            cachedMap.put("TierIron", tierIron);
            cachedMap.put("TierSilver", tierSilver);
            cachedMap.put("TierGold", tierGold);
            cachedMap.put("TierThorium", tierThorium);
            cachedMap.put("TierCobalt", tierCobalt);
            cachedMap.put("TierAdamantite", tierAdamantite);
            cachedMap.put("TierMithril", tierMithril);
            cachedMap.put("TierOnyxium", tierOnyxium);
            cachedMap.put("TierPrisma", tierPrisma);
        }
        return cachedMap;
    }

    // Individual tier getters
    public RepairTier getTierCopper() { return tierCopper; }
    public RepairTier getTierIron() { return tierIron; }
    public RepairTier getTierSilver() { return tierSilver; }
    public RepairTier getTierGold() { return tierGold; }
    public RepairTier getTierThorium() { return tierThorium; }
    public RepairTier getTierCobalt() { return tierCobalt; }
    public RepairTier getTierAdamantite() { return tierAdamantite; }
    public RepairTier getTierMithril() { return tierMithril; }
    public RepairTier getTierOnyxium() { return tierOnyxium; }
    public RepairTier getTierPrisma() { return tierPrisma; }

    // Individual tier setters (also clears cached map)
    public void setTierCopper(RepairTier tier) { this.tierCopper = tier; cachedMap = null; }
    public void setTierIron(RepairTier tier) { this.tierIron = tier; cachedMap = null; }
    public void setTierSilver(RepairTier tier) { this.tierSilver = tier; cachedMap = null; }
    public void setTierGold(RepairTier tier) { this.tierGold = tier; cachedMap = null; }
    public void setTierThorium(RepairTier tier) { this.tierThorium = tier; cachedMap = null; }
    public void setTierCobalt(RepairTier tier) { this.tierCobalt = tier; cachedMap = null; }
    public void setTierAdamantite(RepairTier tier) { this.tierAdamantite = tier; cachedMap = null; }
    public void setTierMithril(RepairTier tier) { this.tierMithril = tier; cachedMap = null; }
    public void setTierOnyxium(RepairTier tier) { this.tierOnyxium = tier; cachedMap = null; }
    public void setTierPrisma(RepairTier tier) { this.tierPrisma = tier; cachedMap = null; }

    // --- Helper methods ---
    private static RecipeIngredient ing(String id, int qty) {
        RecipeIngredient r = new RecipeIngredient();
        r.setItemId(id);
        r.setQuantity(qty);
        return r;
    }

    @SafeVarargs
    private static <T> List<T> list(T... items) {
        return new ArrayList<>(Arrays.asList(items));
    }
}