// Date: 4/25/2012 10:28:13 PM
// Template version 1.1
// Java generated by Techne
// Keep in mind that you still need to fill in some blanks
// - ZeuX

package twilightforest.client.model.entity;

import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.model.AgeableListModel;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.model.geom.PartPose;
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.util.Mth;
import twilightforest.client.JappaPackReloadListener;
import twilightforest.entity.passive.TinyBird;

public class TinyBirdModel extends AgeableListModel<TinyBird> {

	private final ModelPart head;
	private final ModelPart body;
	private final ModelPart rightFoot;
	private final ModelPart leftFoot;
	private final ModelPart rightWing;
	private final ModelPart leftWing;

	public TinyBirdModel(ModelPart root) {
		this.head = root.getChild("head");
		this.body = root.getChild("body");
		this.rightFoot = root.getChild("right_foot");
		this.leftFoot = root.getChild("left_foot");
		this.rightWing = body.getChild("right_wing");
		this.leftWing = body.getChild("left_wing");
	}

	public static LayerDefinition checkForPack() {
		return JappaPackReloadListener.INSTANCE.isJappaPackLoaded() ? createJappaModel() : create();
	}

	private static LayerDefinition create() {
		MeshDefinition meshdefinition = new MeshDefinition();
		PartDefinition partdefinition = meshdefinition.getRoot();

		var head = partdefinition.addOrReplaceChild("head", CubeListBuilder.create()
				.texOffs(0, 0)
				.addBox(-1.5F, -1.5F, -1.5F, 3.0F, 3.0F, 3.0F),
			PartPose.offset(0.0F, 20.5F, -0.5F));

		head.addOrReplaceChild("beak", CubeListBuilder.create()
				.texOffs(12, 0)
				.addBox(-0.5F, -0.5F, -0.5F, 1.0F, 1.0F, 1.0F),
			PartPose.offset(0.0F, 0.5F, -2.0F));

		var body = partdefinition.addOrReplaceChild("body", CubeListBuilder.create()
				.texOffs(0, 6)
				.addBox(-1.5F, 0.0F, -1.0F, 3.0F, 3.0F, 3.0F),
			PartPose.offset(0.0F, 20.0F, 0.0F));

		body.addOrReplaceChild("right_wing", CubeListBuilder.create()
				.texOffs(12, 2)
				.addBox(-1.0F, 0.0F, -1.5F, 1.0F, 2.0F, 3.0F),
			PartPose.offset(-1.5F, 0.5F, 1.0F));

		body.addOrReplaceChild("left_wing", CubeListBuilder.create().mirror()
				.texOffs(12, 2)
				.addBox(0.0F, 0F, -1.5F, 1.0F, 2.0F, 3.0F),
			PartPose.offset(1.5F, 0.5F, 1.0F));

		partdefinition.addOrReplaceChild("right_foot", CubeListBuilder.create()
				.texOffs(0, 12)
				.addBox(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F),
			PartPose.offset(-1.5F, 23.0F, 0.0F));

		partdefinition.addOrReplaceChild("left_foot", CubeListBuilder.create().mirror()
				.texOffs(0, 12)
				.addBox(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F),
			PartPose.offset(0.0F, 23.0F, 0.0F));

		body.addOrReplaceChild("tail", CubeListBuilder.create()
				.texOffs(0, 14)
				.addBox(-1.5F, -0.5F, 0.0F, 3.0F, 1.0F, 2.0F),
			PartPose.offset(0.0F, 2.0F, 2.0F));

		return LayerDefinition.create(meshdefinition, 32, 32);
	}

	private static LayerDefinition createJappaModel() {
		MeshDefinition meshdefinition = new MeshDefinition();
		PartDefinition partdefinition = meshdefinition.getRoot();

		partdefinition.addOrReplaceChild("head", CubeListBuilder.create()
				.texOffs(0, 0)
				.addBox(-1.5F, -2.0F, -2.0F, 3.0F, 3.0F, 3.0F)
				.texOffs(9, 0)
				.addBox(-0.5F, 0.0F, -3.0F, 1.0F, 1.0F, 1.0F)
				.texOffs(0, 6)
				.addBox(-1.5F, -5.0F, 1.0F, 3.0F, 3.0F, 0.0F),
			PartPose.offset(0.0F, 21.0F, 0.0F));

		var body = partdefinition.addOrReplaceChild("body", CubeListBuilder.create()
				.texOffs(12, 0)
				.addBox(-1.5F, 0.0F, 0.0F, 3.0F, 3.0F, 3.0F),
			PartPose.offset(0.0F, 20.0F, 0.0F));

		partdefinition.addOrReplaceChild("right_foot", CubeListBuilder.create()
				.texOffs(0, 9)
				.addBox(-0.5F, 0.0F, -1.0F, 1.0F, 1.0F, 1.0F),
			PartPose.offset(-1.0F, 23.0F, 2.0F));

		partdefinition.addOrReplaceChild("left_foot", CubeListBuilder.create()
				.texOffs(0, 11)
				.addBox(-0.5F, 0.0F, -1.0F, 1.0F, 1.0F, 1.0F),
			PartPose.offset(1.0F, 23.0F, 2.0F));

		body.addOrReplaceChild("right_wing", CubeListBuilder.create()
				.texOffs(24, 0)
				.addBox(-0.5F, 0.0F, -1.0F, 1.0F, 2.0F, 3.0F),
			PartPose.offset(-2.0F, 0.0F, 1.0F));

		body.addOrReplaceChild("left_wing", CubeListBuilder.create()
				.texOffs(24, 5)
				.addBox(-0.5F, 0.0F, -1.0F, 1.0F, 2.0F, 3.0F),
			PartPose.offset(2.0F, 0.0F, 1.0F));

		body.addOrReplaceChild("tail", CubeListBuilder.create()
				.texOffs(1, 6)
				.addBox(-2.5F, 0.0F, 0.0F, 5.0F, 0.0F, 5.0F),
			PartPose.offsetAndRotation(0.0F, 1.0F, 3.0F, 0.4363323129985824F, 0.0F, 0.0F));

		return LayerDefinition.create(meshdefinition, 32, 32);
	}

	@Override
	protected Iterable<ModelPart> headParts() {
		return ImmutableList.of(this.head);
	}

	@Override
	protected Iterable<ModelPart> bodyParts() {
		return ImmutableList.of(this.head, this.body, this.rightFoot, this.leftFoot);
	}

	@Override
	public void renderToBuffer(PoseStack stack, VertexConsumer builder, int light, int overlay, int color) {
		if (young) {
			float f = 2.0F;
			stack.pushPose();
			stack.translate(0.0F, 5.0F, 0.75F);
			this.headParts().forEach((renderer) -> renderer.render(stack, builder, light, overlay, color));
			stack.popPose();
			stack.pushPose();
			stack.scale(1.0F / f, 1.0F / f, 1.0F / f);
			stack.translate(0.0F, 24.0F, 0.0F);
			this.bodyParts().forEach((renderer) -> renderer.render(stack, builder, light, overlay, color));
			stack.popPose();
		} else {
			this.headParts().forEach((renderer) -> renderer.render(stack, builder, light, overlay, color));
			this.bodyParts().forEach((renderer) -> renderer.render(stack, builder, light, overlay, color));
		}
	}

	@Override
	public void setupAnim(TinyBird entity, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
		this.head.xRot = headPitch * Mth.DEG_TO_RAD;
		this.head.yRot = netHeadYaw * Mth.DEG_TO_RAD;

		this.rightFoot.xRot = Mth.cos(limbSwing * 0.6662F) * 1.4F * limbSwingAmount;
		this.leftFoot.xRot = Mth.cos(limbSwing * 0.6662F + Mth.PI) * 1.4F * limbSwingAmount;

		this.rightWing.zRot = ageInTicks;
		this.leftWing.zRot = -ageInTicks;

		if (entity.isBirdLanded()) {
			this.rightFoot.y = 23.0F;
			this.leftFoot.y = 23.0F;
		} else {
			this.rightFoot.y = 22.5F;
			this.leftFoot.y = 22.5F;
		}
	}
}
