package net.mcreator.gts.entity.model;

import software.bernie.geckolib.util.ClientUtil;
import software.bernie.geckolib.model.data.EntityModelData;
import software.bernie.geckolib.model.GeoModel;
import software.bernie.geckolib.constant.DataTickets;
import software.bernie.geckolib.cache.object.GeoBone;
import software.bernie.geckolib.animation.AnimationState;
import software.bernie.geckolib.animation.AnimationProcessor;

import org.joml.Vector3d;

import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.Entity;
import net.minecraft.util.Mth;
import net.minecraft.resources.ResourceLocation;

import net.mcreator.gts.entity.AraEntity;

import java.util.Collection;

public class AraModel extends GeoModel<AraEntity> {
	@Override
	public ResourceLocation getAnimationResource(AraEntity entity) {
		return ResourceLocation.parse("gts:animations/arageckolib.animation.json");
	}

	@Override
	public ResourceLocation getModelResource(AraEntity entity) {
		return ResourceLocation.parse("gts:geo/arageckolib.geo.json");
	}

	@Override
	public ResourceLocation getTextureResource(AraEntity entity) {
		return ResourceLocation.parse("gts:textures/entities/" + entity.getTexture() + ".png");
	}

	@Override
	public void setCustomAnimations(AraEntity entity, long instanceId, AnimationState animationState) {
		GeoBone head = getAnimationProcessor().getBone("Head");
		if (head != null) {
			EntityModelData entityData = (EntityModelData) animationState.getData(DataTickets.ENTITY_MODEL_DATA);
			head.setRotZ(-entityData.headPitch() * Mth.DEG_TO_RAD);
			head.setRotY(entityData.netHeadYaw() * Mth.DEG_TO_RAD);
		}
		Player player = ClientUtil.getClientPlayer();
		if (player instanceof Entity playerEntity) {
			AnimationProcessor processor = getAnimationProcessor();
			Collection<GeoBone> bones = processor.getRegisteredBones();
			int index = 0;
			for (GeoBone bone : bones) {
				if (bone.getName().startsWith("S_")) {
					Vector3d bPos = bone.getPositionVector();
					Vec3 offset = new Vec3(bPos.x(), bPos.y(), -bPos.z()).scale((entity.getAttribute(Attributes.SCALE).getBaseValue() / 16.0));
					offset = offset.yRot(Mth.DEG_TO_RAD * (float) -entity.getVisualRotationYInDegrees());
					entity.setPassengerSlotOffset(index, offset);
				}
			}
		}
	}
}
