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

import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.model.geom.ModelLayerLocation;
import net.minecraft.client.model.geom.PartPose;
import net.minecraft.client.model.geom.builders.*;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.event.EntityRenderersEvent;
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
import ru.dbotthepony.mc.otm.registry.MNames;

public final class GravitationStabilizerModel {
	public static final ModelLayerLocation LAYER_LOCATION = new ModelLayerLocation(OverdriveThatMatters.loc(MNames.PORTABLE_GRAVITATION_STABILIZER), "main");

	private GravitationStabilizerModel() {}

	private static HumanoidModel<?> model;
	private static LayerDefinition def;

	public static HumanoidModel<?> getModel() {
		if (model == null) {
			return model = new HumanoidModel<>(def.bakeRoot());
		}

		return model;
	}

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

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

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

		PartDefinition body = partdefinition.addOrReplaceChild("body", CubeListBuilder.create().texOffs(0, 0).addBox(-2.0F, 1.0F, -3.0F, 4.0F, 4.0F, 1.0F, new CubeDeformation(0.0F))
				.texOffs(0, 5).addBox(-4.0F, 3.0F, 5.0F, 8.0F, 2.0F, 2.0F, new CubeDeformation(0.0F))
				.texOffs(0, 5).addBox(-4.0F, 0.0F, 5.0F, 8.0F, 2.0F, 2.0F, new CubeDeformation(0.0F))
				.texOffs(0, 9).addBox(2.0F, -1.0F, 5.0F, 1.0F, 12.0F, 3.0F, new CubeDeformation(0.0F))
				.texOffs(0, 9).addBox(-3.0F, -1.0F, 5.0F, 1.0F, 12.0F, 3.0F, new CubeDeformation(0.0F))
				.texOffs(40, 16).addBox(-4.0F, 0.0F, -2.0F, 8.0F, 12.0F, 4.0F, new CubeDeformation(0.41F))
				.texOffs(44, 33).addBox(-4.0F, 0.0F, 2.8F, 8.0F, 6.0F, 2.0F, new CubeDeformation(0.4F)), PartPose.offset(0.0F, 0.0F, 0.0F));

		PartDefinition backpackslope_r1 = body.addOrReplaceChild("backpackslope_r1", CubeListBuilder.create().texOffs(44, 41).addBox(-4.0F, 0.4F, -2.4F, 8.0F, 3.0F, 2.0F, new CubeDeformation(0.39F)), PartPose.offsetAndRotation(0.0F, 6.4F, 5.2F, -1.0908F, 0.0F, 0.0F));

		PartDefinition right_arm = partdefinition.addOrReplaceChild("right_arm", CubeListBuilder.create(), PartPose.offset(-5.0F, 2.0F, 0.0F));

		PartDefinition left_arm = partdefinition.addOrReplaceChild("left_arm", CubeListBuilder.create(), PartPose.offset(5.0F, 2.0F, 0.0F));

		PartDefinition right_leg = partdefinition.addOrReplaceChild("right_leg", CubeListBuilder.create(), PartPose.offset(-1.9F, 12.0F, 0.0F));

		PartDefinition left_leg = partdefinition.addOrReplaceChild("left_leg", CubeListBuilder.create(), PartPose.offset(1.9F, 12.0F, 0.0F));

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

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