package com.tiedup.remake.compat.mca;

import com.tiedup.remake.compat.mca.capability.MCAKidnappedAdapter;
import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.state.IRestrainable;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.EntityRenderersEvent;

/**
 * MCA Handler using reflection to avoid compilation issues with obfuscated jars.
 */
public class MCAHandler {

    private static Class<?> villagerLikeClass;
    private static Class<?> villagerEntityClass;

    public static void init() {
        try {
            // MCA Forge uses "forge.net.mca" package prefix
            villagerLikeClass = Class.forName(
                "forge.net.mca.entity.VillagerLike"
            );
            villagerEntityClass = Class.forName(
                "forge.net.mca.entity.VillagerEntityMCA"
            );
            TiedUpMod.LOGGER.info(
                "[MCA Compat] Handler initialized successfully via reflection"
            );
        } catch (ClassNotFoundException e) {
            TiedUpMod.LOGGER.error(
                "[MCA Compat] Failed to load MCA classes via reflection",
                e
            );
        }
    }

    public static boolean isMCAVillager(Entity entity) {
        if (entity == null) return false;

        // 1. Check loaded classes via reflection
        if (
            villagerLikeClass != null && villagerLikeClass.isInstance(entity)
        ) return true;
        if (
            villagerEntityClass != null &&
            villagerEntityClass.isInstance(entity)
        ) return true;

        // 2. Check EntityType namespace (fallback if classes not found/loaded)
        net.minecraft.resources.ResourceLocation typeId =
            net.minecraftforge.registries.ForgeRegistries.ENTITY_TYPES.getKey(
                entity.getType()
            );
        if (typeId != null && "mca".equals(typeId.getNamespace())) {
            String path = typeId.getPath();
            if (path.contains("villager")) {
                return true;
            }
        }

        // 3. Check class name string (ultimate fallback)
        String className = entity.getClass().getName();
        return className.contains("mca") && className.contains("Villager");
    }

    public static IRestrainable getKidnappedState(LivingEntity entity) {
        if (!isMCAVillager(entity)) return null;

        return entity
            .getCapability(MCACompat.MCA_KIDNAPPED)
            .map(cap -> new MCAKidnappedAdapter(entity, cap))
            .orElse(null);
    }

    @OnlyIn(Dist.CLIENT)
    public static void addBondageLayer(
        LivingEntityRenderer<?, ?> renderer,
        EntityRenderersEvent.AddLayers event
    ) {
        // V1 MCA bondage render layer removed — V2 render layer handles MCA villagers
    }
}
