package ru.dbotthepony.mc.otm.client.model.entity;

import net.minecraft.client.model.HierarchicalModel;
import net.minecraft.client.model.geom.ModelLayerLocation;
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.neoforged.neoforge.client.event.EntityRenderersEvent;
import org.jetbrains.annotations.NotNull;
import ru.dbotthepony.mc.otm.client.animation.BreadMonsterAnimation;
import ru.dbotthepony.mc.otm.entity.BreadMonster;
import ru.dbotthepony.mc.otm.registry.MNames;

import static ru.dbotthepony.mc.otm.OverdriveThatMatters.loc;

public class BreadMonsterModel {
	public static final ModelLayerLocation LAYER_LOCATION = new ModelLayerLocation(loc(MNames.BREAD_MONSTER), "main");

	private static HierarchicalModel<BreadMonster> model;
	private static LayerDefinition def;

	public static HierarchicalModel<BreadMonster> getModel() {
		if (model == null) {
			return model = new BreadMonsterHierarchicalModel<>(def.bakeRoot());
		}

		return model;
	}

	public static LayerDefinition createBodyLayer() {
		MeshDefinition meshdefinition = new MeshDefinition();
		PartDefinition partdefinition = meshdefinition.getRoot();

		PartDefinition root = partdefinition.addOrReplaceChild("root", CubeListBuilder.create(), PartPose.offset(0.0F, 24.0F, 0.0F));

		PartDefinition Body = root.addOrReplaceChild("Body", CubeListBuilder.create().texOffs(0, 0).addBox(-4.0F, -3.0F, -5.0F, 8.0F, 6.0F, 6.0F, new CubeDeformation(0.0F)), PartPose.offset(0.0F, -3.0F, 0.0F));
		PartDefinition Ljaw = Body.addOrReplaceChild("Ljaw", CubeListBuilder.create().texOffs(26, 24).addBox(-4.0F, -2.0F, -4.0F, 8.0F, 3.0F, 4.0F, new CubeDeformation(0.1F)), PartPose.offset(0.0F, 2.0F, -3.0F));
		PartDefinition UJaw = Body.addOrReplaceChild("UJaw", CubeListBuilder.create().texOffs(0, 24).addBox(-4.0F, -2.0F, -4.0F, 8.0F, 3.0F, 5.0F, new CubeDeformation(0.1F)), PartPose.offset(0.0F, -1.0F, -3.0F));
		PartDefinition tail = Body.addOrReplaceChild("tail", CubeListBuilder.create().texOffs(0, 12).addBox(-4.0F, -3.0F, -0.1F, 8.0F, 6.0F, 6.0F, new CubeDeformation(-0.1F)), PartPose.offset(0.0F, 0.0F, 1.0F));

		model = null;
		return def = LayerDefinition.create(meshdefinition, 64, 32);
	}

	private static class BreadMonsterHierarchicalModel<T extends BreadMonster> extends HierarchicalModel<T> {
		private final ModelPart root;
		public BreadMonsterHierarchicalModel(ModelPart root) {
			this.root = root.getChild("root");
		}

		@Override
		public void setupAnim(@NotNull T entity, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
			this.root().getAllParts().forEach(ModelPart::resetPose);

//			this.applyStatic(BreadMonsterAnimation.IDLE);
			this.animate(entity.getIdleState(), BreadMonsterAnimation.IDLE, ageInTicks, 1.0F);
			this.animateWalk(BreadMonsterAnimation.MOVE, limbSwing, limbSwingAmount, 1.0F, 2.5F);
		}

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

	public static void register(EntityRenderersEvent.RegisterLayerDefinitions event) {
		event.registerLayerDefinition(LAYER_LOCATION, BreadMonsterModel::createBodyLayer);
	}
}
