package net.Chipperfluff.chipi.effect;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectCategory;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import java.util.UUID;

import net.Chipperfluff.chipi.entity.ModEntities;
import net.Chipperfluff.chipi.entity.PlayerJrEntity;

public class PregnantEffect extends StatusEffect {

    public static final int TOTAL_DURATION = 20 * 60 * 10; // 10 min
    private static final double MAX_SLOW = 0.90;

    private static final UUID SPEED_UUID =
        UUID.fromString("7c8b6b8f-4b6c-4f6f-9b7b-01c8e9f8a111");

    public PregnantEffect() {
        super(StatusEffectCategory.HARMFUL, 0xFFB6C1);
    }

    /* ---------- helpers used by mixin ---------- */

    /** 0.0 → normal jump, 1.0 → no jump */
    public static double getJumpClamp(ServerPlayerEntity player) {
        StatusEffectInstance inst = player.getStatusEffect(ModEffects.PREGNANT);
        if (inst == null) return 0.0;

        double progress =
            1.0 - (inst.getDuration() / (double) TOTAL_DURATION);

        return Math.min(Math.max(progress, 0.0), 1.0);
    }

    /* ---------- ticking ---------- */

    @Override
    public boolean canApplyUpdateEffect(int duration, int amplifier) {
        return true;
    }

    @Override
    public void applyUpdateEffect(LivingEntity entity, int amplifier) {
        if (!(entity instanceof ServerPlayerEntity player)) return;

        StatusEffectInstance inst = player.getStatusEffect(this);
        if (inst == null) return;

        int remaining = inst.getDuration();

        double progress =
            1.0 - (remaining / (double) TOTAL_DURATION);

        progress = Math.min(Math.max(progress, 0.0), 1.0);

        applySpeedModifier(player, progress * MAX_SLOW);

        if (remaining % 400 == 0 && remaining > 20) {
            player.sendMessage(
                Text.literal("Your knees file a formal complaint…"),
                true
            );
        }

        if (remaining == 1) {
            finishBirth(player);
        }
    }

    private void applySpeedModifier(ServerPlayerEntity player, double slowAmount) {
        EntityAttributeInstance attr =
            player.getAttributeInstance(EntityAttributes.GENERIC_MOVEMENT_SPEED);

        if (attr == null) return;

        EntityAttributeModifier old = attr.getModifier(SPEED_UUID);
        if (old != null) attr.removeModifier(old);

        if (slowAmount > 0.01) {
            attr.addTemporaryModifier(new EntityAttributeModifier(
                SPEED_UUID,
                "Pregnancy slowdown",
                -slowAmount,
                EntityAttributeModifier.Operation.MULTIPLY_TOTAL
            ));
        }
    }

    private void finishBirth(ServerPlayerEntity dad) {
        // cleanup
        EntityAttributeInstance attr =
            dad.getAttributeInstance(EntityAttributes.GENERIC_MOVEMENT_SPEED);
        if (attr != null) {
            EntityAttributeModifier old = attr.getModifier(SPEED_UUID);
            if (old != null) attr.removeModifier(old);
        }

        dad.removeStatusEffect(this);

        PlayerJrEntity jr = ModEntities.PLAYER_JR.create(dad.getWorld());
        if (jr == null) return;

        jr.refreshPositionAndAngles(
            dad.getX(), dad.getY(), dad.getZ(),
            dad.getYaw(), dad.getPitch()
        );

        jr.setDad(dad);
        dad.getWorld().spawnEntity(jr);

        dad.sendMessage(
            Text.literal(
                "You're a dad now. The name’s " +
                jr.getName().getString() +
                ". Warning: it might cheat."
            ),
            false
        );
    }
}
