package com.xfestudio.mydimensionlord.client.gear;

import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;

/** Real layered armor geometry with separate animated crystal bones. */
public final class LordArmorModel extends HumanoidModel<LivingEntity> {
    public LordArmorModel(EquipmentSlot slot) { super(LordArmorGeometry.create(slot).bakeRoot()); }
    public void adopt(HumanoidModel<?> original) {
        head.copyFrom(original.head); body.copyFrom(original.body);
        leftArm.copyFrom(original.leftArm); rightArm.copyFrom(original.rightArm);
        leftLeg.copyFrom(original.leftLeg); rightLeg.copyFrom(original.rightLeg);
        young = original.young; crouching = original.crouching; riding = original.riding;
        // Geometry only exists on the equipped piece; empty parts remain empty.
        setAllVisible(true);
    }
    public void animate(long millis) {
        for (ModelPart part : new ModelPart[]{head,body,leftArm,rightArm,leftLeg,rightLeg}) {
            part.getAllParts().forEach(bone -> {
                if (bone == part) return; // retain the entity's limb/head pose
                bone.resetPose();
            });
        }
        animatePart(head,"crown_spark",millis,0,.3);
        animatePart(head,"crown_tip",millis,0,.3);
        animatePart(body,"chest_core",millis,.4,.12);
        animatePart(body,"back_core",millis,1.5,.16);
        animatePart(body,"belt_core",millis,.8,.12);
        animatePart(body,"hip_spark_-1",millis,0,.22);
        animatePart(body,"hip_spark_1",millis,2,.22);
        animatePart(body,"shoulder_spark_1",millis,0,.35);
        animatePart(body,"shoulder_spark_-1",millis,2,.35);
        for (ModelPart leg : new ModelPart[]{leftLeg,rightLeg}) {
            animatePart(leg,"knee_core",millis,1,.1);
            animatePart(leg,"boot_core",millis,.6,.12);
            animatePart(leg,"heel_spark",millis,1,.2);
        }
    }
    private static void animatePart(ModelPart parent,String name,long millis,double phase,double amplitude) {
        if (!parent.hasChild(name)) return;
        ModelPart bone=parent.getChild(name);
        bone.y += (float)(GearAnimation.bob(millis,phase)*amplitude);
        bone.yRot += (float)(GearAnimation.bob(millis,phase+1)*.24);
        // Prongs always remain above the forehead, including at maximum bob.
    }
}
