package com.tiedup.remake.entities.skins;

import com.google.gson.JsonObject;
import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.entities.DamselVariant;
import com.tiedup.remake.entities.KidnapperVariant;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;

/**
 * Data-driven skin definition for entity types.
 *
 * This record holds the skin metadata loaded from JSON files in datapacks.
 * Each entity type (kidnapper, damsel, etc.) has its own skin definitions.
 *
 */
public record SkinDefinition(
    String id,
    boolean hasSlimArms,
    String defaultName,
    ResourceLocation texture,
    Gender gender
) {
    /**
     * Parse a SkinDefinition from JSON and auto-generate texture path.
     *
     * @param json JSON object from datapack file
     * @param entityType Entity type (e.g., "kidnapper", "damsel", "kidnapper_elite")
     * @return SkinDefinition with auto-generated texture path
     */
    public static SkinDefinition fromJson(JsonObject json, String entityType) {
        String id = GsonHelper.getAsString(json, "id");
        boolean hasSlimArms = GsonHelper.getAsBoolean(
            json,
            "hasSlimArms",
            false
        );
        String defaultName = GsonHelper.getAsString(json, "defaultName", "NPC");
        String genderStr = GsonHelper.getAsString(json, "gender", "female");
        Gender gender = Gender.fromName(genderStr);

        // Auto-generate texture path: tiedup:textures/entity/{entity_type}/{id}.png
        // Map special entity types to correct texture folders:
        //   kidnapper_elite  → kidnapper/elite
        //   kidnapper_archer → kidnapper/archer
        //   kidnapper_merchant → kidnapper/merchant
        String texturePath = mapEntityTypeToTexturePath(entityType);
        ResourceLocation texture = ResourceLocation.fromNamespaceAndPath(
            "tiedup",
            "textures/entity/" + texturePath + "/" + id + ".png"
        );

        TiedUpMod.LOGGER.debug(
            "[SkinDefinition] Parsed {} skin: {} (slimArms={}, name={}, gender={})",
            entityType,
            id,
            hasSlimArms,
            defaultName,
            gender
        );

        return new SkinDefinition(
            id,
            hasSlimArms,
            defaultName,
            texture,
            gender
        );
    }

    /**
     * Map entity type to texture path.
     * Handles special cases like elite/archer/merchant variants.
     *
     * @param entityType Entity type from registry (e.g., "kidnapper_elite")
     * @return Texture path segment (e.g., "kidnapper/elite")
     */
    private static String mapEntityTypeToTexturePath(String entityType) {
        return switch (entityType) {
            case "kidnapper_elite" -> "kidnapper/elite";
            case "kidnapper_archer" -> "kidnapper/archer";
            case "kidnapper_merchant" -> "kidnapper/merchant";
            case "maid" -> "kidnapper/maid";
            case "slave_trader" -> "kidnapper/trader";
            case "damsel_shiny" -> "damsel/shiny";
            case "labor_guard" -> "guard";
            default -> entityType;
        };
    }

    /**
     * Convert to KidnapperVariant for backward compatibility.
     *
     * @return KidnapperVariant with same data
     */
    public KidnapperVariant toKidnapperVariant() {
        return new KidnapperVariant(
            id,
            texture,
            hasSlimArms,
            defaultName,
            gender
        );
    }

    /**
     * Convert to DamselVariant for backward compatibility.
     *
     * @return DamselVariant with same data
     */
    public DamselVariant toDamselVariant() {
        return new DamselVariant(id, texture, hasSlimArms, defaultName, gender);
    }
}
