package com.tiedup.remake.mixin.client;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.tiedup.remake.compat.wildfire.WildfireCompat;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.world.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/**
 * Mixin for MCA's PlayerEntityExtendedModel to hide breasts when Wildfire is loaded.
 *
 * <p>MCA adds breasts to players through PlayerEntityExtendedModel. When Wildfire
 * is also installed, we disable MCA's breasts to avoid double-rendering (Wildfire
 * renders its own breasts with physics).
 *
 * <p>Uses @Pseudo annotation - mixin is optional and will be skipped if MCA is not installed.
 *
 * <p>Target class: forge.net.mca.client.model.PlayerEntityExtendedModel
 */
@Pseudo
@Mixin(
    targets = "forge.net.mca.client.model.PlayerEntityExtendedModel",
    remap = false
)
public class MixinMCAPlayerExtendedModel<T extends LivingEntity> {

    /**
     * Shadow the breasts ModelPart to control visibility.
     */
    @Shadow(remap = false)
    public ModelPart breasts;

    /**
     * Shadow the breastsWear ModelPart (overlay layer).
     */
    @Shadow(remap = false)
    public ModelPart breastsWear;

    /**
     * Inject at the end of setAngles (m_6973_) to hide MCA breasts when Wildfire is loaded.
     *
     * <p>This runs after applyVillagerDimensions() which sets breast visibility.
     */
    @Inject(method = "m_6973_", at = @At("TAIL"), remap = false)
    private void tiedup$hideBreastsInSetAngles(
        T entity,
        float limbSwing,
        float limbSwingAmount,
        float ageInTicks,
        float netHeadYaw,
        float headPitch,
        CallbackInfo ci
    ) {
        tiedup$hideBreastsIfWildfire();
    }

    /**
     * Inject at the end of copyVisibility to prevent it from re-enabling breasts.
     *
     * <p>MCA's copyVisibility sets breasts.visible = model.body.visible.
     */
    @Inject(method = "copyVisibility", at = @At("TAIL"), remap = false)
    private void tiedup$hideBreastsAfterCopyVisibility(CallbackInfo ci) {
        tiedup$hideBreastsIfWildfire();
    }

    /**
     * Inject at the end of render to hide breasts after breastsWear visibility is set.
     *
     * <p>MCA's render() sets breastsWear.visible = jacket.visible before rendering.
     */
    @Inject(method = "m_7695_", at = @At("HEAD"), remap = false)
    private void tiedup$hideBreastsBeforeRender(
        PoseStack matrices,
        VertexConsumer vertices,
        int light,
        int overlay,
        float red,
        float green,
        float blue,
        float alpha,
        CallbackInfo ci
    ) {
        tiedup$hideBreastsIfWildfire();
    }

    /**
     * Helper method to hide both breast parts when Wildfire is loaded.
     */
    private void tiedup$hideBreastsIfWildfire() {
        if (WildfireCompat.isLoaded()) {
            if (breasts != null) {
                breasts.visible = false;
            }
            if (breastsWear != null) {
                breastsWear.visible = false;
            }
        }
    }
}
