package net.minecraft.client.model.monster.hoglin;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.model.EntityModel;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.model.geom.PartPose;
import net.minecraft.client.model.geom.builders.CubeDeformation;
import net.minecraft.client.model.geom.builders.CubeListBuilder;
import net.minecraft.client.model.geom.builders.LayerDefinition;
import net.minecraft.client.model.geom.builders.MeshDefinition;
import net.minecraft.client.model.geom.builders.PartDefinition;
import net.minecraft.client.renderer.entity.state.HoglinRenderState;
import net.minecraft.util.Mth;

@Environment(EnvType.CLIENT)
public class HoglinModel extends EntityModel<HoglinRenderState> {
	private static final float DEFAULT_HEAD_X_ROT = 0.87266463F;
	private static final float ATTACK_HEAD_X_ROT_END = (float) (-Math.PI / 9);
	protected final ModelPart head;
	private final ModelPart rightEar;
	private final ModelPart leftEar;
	private final ModelPart rightFrontLeg;
	private final ModelPart leftFrontLeg;
	private final ModelPart rightHindLeg;
	private final ModelPart leftHindLeg;

	public HoglinModel(final ModelPart root) {
		super(root);
		this.head = root.getChild("head");
		this.rightEar = this.head.getChild("right_ear");
		this.leftEar = this.head.getChild("left_ear");
		this.rightFrontLeg = root.getChild("right_front_leg");
		this.leftFrontLeg = root.getChild("left_front_leg");
		this.rightHindLeg = root.getChild("right_hind_leg");
		this.leftHindLeg = root.getChild("left_hind_leg");
	}

	public static LayerDefinition createBodyLayer() {
		MeshDefinition mesh = new MeshDefinition();
		PartDefinition root = mesh.getRoot();
		PartDefinition body = root.addOrReplaceChild(
			"body", CubeListBuilder.create().texOffs(1, 1).addBox(-8.0F, -7.0F, -13.0F, 16.0F, 14.0F, 26.0F), PartPose.offset(0.0F, 7.0F, 0.0F)
		);
		body.addOrReplaceChild(
			"mane",
			CubeListBuilder.create().texOffs(90, 33).addBox(0.0F, 0.0F, -9.0F, 0.0F, 10.0F, 19.0F, new CubeDeformation(0.001F)),
			PartPose.offset(0.0F, -14.0F, -7.0F)
		);
		PartDefinition head = root.addOrReplaceChild(
			"head",
			CubeListBuilder.create().texOffs(61, 1).addBox(-7.0F, -3.0F, -19.0F, 14.0F, 6.0F, 19.0F),
			PartPose.offsetAndRotation(0.0F, 2.0F, -12.0F, 0.87266463F, 0.0F, 0.0F)
		);
		head.addOrReplaceChild(
			"right_ear",
			CubeListBuilder.create().texOffs(1, 1).addBox(-6.0F, -1.0F, -2.0F, 6.0F, 1.0F, 4.0F),
			PartPose.offsetAndRotation(-6.0F, -2.0F, -3.0F, 0.0F, 0.0F, (float) (-Math.PI * 2.0 / 9.0))
		);
		head.addOrReplaceChild(
			"left_ear",
			CubeListBuilder.create().texOffs(1, 6).addBox(0.0F, -1.0F, -2.0F, 6.0F, 1.0F, 4.0F),
			PartPose.offsetAndRotation(6.0F, -2.0F, -3.0F, 0.0F, 0.0F, (float) (Math.PI * 2.0 / 9.0))
		);
		head.addOrReplaceChild(
			"right_horn", CubeListBuilder.create().texOffs(10, 13).addBox(-1.0F, -11.0F, -1.0F, 2.0F, 11.0F, 2.0F), PartPose.offset(-7.0F, 2.0F, -12.0F)
		);
		head.addOrReplaceChild(
			"left_horn", CubeListBuilder.create().texOffs(1, 13).addBox(-1.0F, -11.0F, -1.0F, 2.0F, 11.0F, 2.0F), PartPose.offset(7.0F, 2.0F, -12.0F)
		);
		int frontLegHeight = 14;
		int backLegHeight = 11;
		root.addOrReplaceChild(
			"right_front_leg", CubeListBuilder.create().texOffs(66, 42).addBox(-3.0F, 0.0F, -3.0F, 6.0F, 14.0F, 6.0F), PartPose.offset(-4.0F, 10.0F, -8.5F)
		);
		root.addOrReplaceChild(
			"left_front_leg", CubeListBuilder.create().texOffs(41, 42).addBox(-3.0F, 0.0F, -3.0F, 6.0F, 14.0F, 6.0F), PartPose.offset(4.0F, 10.0F, -8.5F)
		);
		root.addOrReplaceChild(
			"right_hind_leg", CubeListBuilder.create().texOffs(21, 45).addBox(-2.5F, 0.0F, -2.5F, 5.0F, 11.0F, 5.0F), PartPose.offset(-5.0F, 13.0F, 10.0F)
		);
		root.addOrReplaceChild(
			"left_hind_leg", CubeListBuilder.create().texOffs(0, 45).addBox(-2.5F, 0.0F, -2.5F, 5.0F, 11.0F, 5.0F), PartPose.offset(5.0F, 13.0F, 10.0F)
		);
		return LayerDefinition.create(mesh, 128, 64);
	}

	public void setupAnim(final HoglinRenderState state) {
		super.setupAnim(state);
		float animationSpeed = state.walkAnimationSpeed;
		float animationPos = state.walkAnimationPos;
		this.rightEar.zRot = (float) (-Math.PI * 2.0 / 9.0) - animationSpeed * Mth.sin(animationPos);
		this.leftEar.zRot = (float) (Math.PI * 2.0 / 9.0) + animationSpeed * Mth.sin(animationPos);
		this.head.yRot = state.yRot * (float) (Math.PI / 180.0);
		float headbuttLerpFactor = 1.0F - Mth.abs(10 - 2 * state.attackAnimationRemainingTicks) / 10.0F;
		this.animateHeadbutt(headbuttLerpFactor);
		float amplitudeMultiplier = 1.2F;
		this.rightFrontLeg.xRot = Mth.cos(animationPos) * 1.2F * animationSpeed;
		this.leftFrontLeg.xRot = Mth.cos(animationPos + (float) Math.PI) * 1.2F * animationSpeed;
		this.rightHindLeg.xRot = this.leftFrontLeg.xRot;
		this.leftHindLeg.xRot = this.rightFrontLeg.xRot;
	}

	protected void animateHeadbutt(final float headbuttLerpFactor) {
		this.head.xRot = Mth.lerp(headbuttLerpFactor, 0.87266463F, (float) (-Math.PI / 9));
	}
}
