// Date: 5/18/2012 11:45:03 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.legacy;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.model.HierarchicalModel;
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.world.entity.animal.Sheep;
import net.minecraft.world.item.DyeColor;
import net.minecraft.util.Mth;
import twilightforest.entity.passive.QuestRam;

import java.util.Arrays;

public class QuestRamLegacyModel extends HierarchicalModel<QuestRam> {
	//root
	public final ModelPart root;
	//fields
	ModelPart rearbody;
	public ModelPart leg1;
	public ModelPart haunch1;
	public ModelPart leg2;
	public ModelPart haunch2;
	public ModelPart leg3;
	public ModelPart haunch3;
	public ModelPart leg4;
	public ModelPart haunch4;
	public ModelPart neck;
	public final ModelPart head;
	public ModelPart[] segments = new ModelPart[16];

	int[] colorOrder = new int[]{0, 8, 7, 15, 14, 1, 4, 5, 13, 3, 9, 11, 10, 2, 6, 12};

	public QuestRamLegacyModel(ModelPart root) {
		this.root = root;

		this.head = root.getChild("head");
		this.neck = root.getChild("neck");
		this.rearbody = root.getChild("rear_body");
		this.haunch1 = root.getChild("right_front_haunch");
		this.leg1 = root.getChild("right_front_leg");
		this.haunch2 = root.getChild("left_front_haunch");
		this.leg2 = root.getChild("left_front_leg");
		this.haunch3 = root.getChild("right_back_haunch");
		this.leg3 = root.getChild("right_back_leg");
		this.haunch4 = root.getChild("left_back_haunch");
		this.leg4 = root.getChild("left_back_leg");
		Arrays.setAll(this.segments, (num) -> root.getChild(getSegmentName(num)));
		for (int i = 0; i < 16; i++) {
			segments[i].visible = false;
		}
	}

	public static LayerDefinition create() {
		MeshDefinition mesh = new MeshDefinition();
		PartDefinition base = mesh.getRoot();

		PartDefinition headpart = base.addOrReplaceChild("head", CubeListBuilder.create()
						.texOffs(0, 70).addBox(-6F, -4.5F, -15F, 12, 9, 15)
						.texOffs(0, 94).addBox(5F, -9F, -7F, 4, 4, 6)
						.texOffs(20, 96).addBox(7F, -8F, -2F, 3, 4, 4)
						.texOffs(34, 95).addBox(8F, -6F, 0F, 3, 6, 3)
						.texOffs(46, 98).addBox(9.5F, -2F, -2F, 3, 3, 3)
						.texOffs(58, 95).addBox(11F, 0F, -7F, 3, 3, 6)
						.texOffs(76, 95).addBox(12F, -4F, -9F, 3, 6, 3)
						.texOffs(88, 97).addBox(13F, -6F, -7F, 3, 3, 4)
						.texOffs(0, 94).addBox(-9F, -9F, -7F, 4, 4, 6)
						.texOffs(20, 96).addBox(-10F, -8F, -2F, 3, 4, 4)
						.texOffs(34, 95).addBox(-11F, -6F, 0F, 3, 6, 3)
						.texOffs(46, 98).addBox(-12.5F, -2F, -2F, 3, 3, 3)
						.texOffs(58, 95).addBox(-14F, 0F, -7F, 3, 3, 6)
						.texOffs(76, 95).addBox(-15F, -4F, -9F, 3, 6, 3)
						.texOffs(88, 97).addBox(-16F, -6F, -7F, 3, 3, 4),
				PartPose.offset(0F, -13F, -5F));
		headpart.addOrReplaceChild("nose", CubeListBuilder.create()
						.texOffs(54, 73)
						.addBox(-5.5F, -5F, -13F, 11, 9, 12),
				PartPose.offsetAndRotation(0F, -7F, -1F, 0.5235988F, 0F, 0F));
		base.addOrReplaceChild("neck", CubeListBuilder.create()
						.texOffs(66, 37)
						.addBox(-5.5F, -8F, -8F, 11, 14, 12),
				PartPose.offsetAndRotation(0F, -8F, -7F, 0.2617994F, 0F, 0F));
		base.addOrReplaceChild("front_body", CubeListBuilder.create()
						.texOffs(0, 0)
						.addBox(-9F, -7.5F, -15F, 18, 15, 15),
				PartPose.offset(0F, -1F, 2F));
		base.addOrReplaceChild("rear_body", CubeListBuilder.create()
						.texOffs(0, 30)
						.addBox(-9F, -7.5F, 0F, 18, 15, 15),
				PartPose.offset(0F, -1F, 4F));
		base.addOrReplaceChild("right_front_haunch", CubeListBuilder.create()
						.texOffs(90, 0)
						.addBox(-3.5F, 0F, -6F, 7, 10, 10),
				PartPose.offset(-6F, 2F, 13F));
		base.addOrReplaceChild("right_front_leg", CubeListBuilder.create()
						.texOffs(66, 0)
						.addBox(-3F, 10F, -3F, 6, 12, 6),
				PartPose.offset(-6F, 2F, 13F));
		base.addOrReplaceChild("left_front_haunch", CubeListBuilder.create()
						.texOffs(90, 0)
						.addBox(-3.5F, 0F, -6F, 7, 10, 10),
				PartPose.offset(6F, 2F, 13F));
		base.addOrReplaceChild("left_front_leg", CubeListBuilder.create()
						.texOffs(66, 0)
						.addBox(-3F, 10F, -3F, 6, 12, 6),
				PartPose.offset(6F, 2F, 13F));
		base.addOrReplaceChild("right_back_haunch", CubeListBuilder.create()
						.texOffs(90, 20)
						.addBox(-3.5F, 0F, -4F, 7, 10, 7),
				PartPose.offset(-6F, 1F, -8F));
		base.addOrReplaceChild("right_back_leg", CubeListBuilder.create()
						.texOffs(66, 18)
						.addBox(-3F, 10F, -3F, 6, 13, 6),
				PartPose.offset(-6F, 1F, -8F));
		base.addOrReplaceChild("left_back_haunch", CubeListBuilder.create()
						.texOffs(90, 20)
						.addBox(-3.5F, 0F, -4F, 7, 10, 7),
				PartPose.offset(6F, 1F, -8F));
		base.addOrReplaceChild("left_back_leg", CubeListBuilder.create()
						.texOffs(66, 18)
						.addBox(-3F, 10F, -3F, 6, 13, 6),
				PartPose.offset(6F, 1F, -8F));

		CubeListBuilder bodycube = CubeListBuilder.create()
				.texOffs(0, 104)
				.addBox(-9F, -7.5F, 0F, 18, 15, 2);
		for (int i = 0; i < 16; i++) {
			base.addOrReplaceChild(getSegmentName(i), bodycube, PartPose.offset(0F, -1F, 2F));
		}

		return LayerDefinition.create(mesh, 128, 128);
	}

	private static String getSegmentName(int num) {
		return "segment" + num;
	}

	@Override
	public ModelPart root() {
		return this.root;
	}

	@Override
	public void renderToBuffer(PoseStack stack, VertexConsumer builder, int light, int overlay, float red, float green, float blue, float alpha) {
		super.renderToBuffer(stack, builder, light, overlay, red, green, blue, alpha);

		for (int i = 0; i < 16; i++) {
			final float[] dyeRgb = Sheep.getColorArray(DyeColor.byId(i));
			segments[i].render(stack, builder, light, overlay, dyeRgb[0], dyeRgb[1], dyeRgb[2], alpha);
		}
	}

	@Override
	public void setupAnim(QuestRam entity, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
		this.head.xRot = headPitch / (180F / (float) Math.PI);
		this.head.yRot = netHeadYaw / (180F / (float) Math.PI);

		this.neck.yRot = this.head.yRot;

		this.leg1.xRot = Mth.cos(limbSwing * 0.6662F) * 1.4F * limbSwingAmount * 0.5F;
		this.leg2.xRot = Mth.cos(limbSwing * 0.6662F + (float) Math.PI) * 1.4F * limbSwingAmount * 0.5F;
		this.leg3.xRot = Mth.cos(limbSwing * 0.6662F + (float) Math.PI) * 1.4F * limbSwingAmount * 0.5F;
		this.leg4.xRot = Mth.cos(limbSwing * 0.6662F) * 1.4F * limbSwingAmount * 0.5F;
		this.haunch1.xRot = this.leg1.xRot;
		this.haunch2.xRot = this.leg2.xRot;
		this.haunch3.xRot = this.leg3.xRot;
		this.haunch4.xRot = this.leg4.xRot;
	}

	@Override
	public void prepareMobModel(QuestRam entity, float limbSwing, float limbSwingAmount, float partialTicks) {

		// how many colors should we display?
		int count = entity.countColorsSet();

		this.rearbody.z = 2 + 2 * count;
		this.leg1.z = 11 + 2 * count;
		this.leg2.z = 11 + 2 * count;
		this.haunch1.z = 11 + 2 * count;
		this.haunch2.z = 11 + 2 * count;

		// set up the colors displayed in color order
		int segmentOffset = 2;
		for (int color : colorOrder) {
			if (entity.isColorPresent(DyeColor.byId(color))) {
				segments[color].visible = true;
				segments[color].z = segmentOffset;

				segmentOffset += 2;
			} else {
				segments[color].visible = false;
			}
		}
	}
}
