/*
 * Derived from Epic Fight (https://github.com/Epic-Fight/epicfight)
 * by the Epic Fight Team, licensed under GPLv3.
 * Modifications © 2026 TiedUp! Remake Contributors, distributed under GPLv3.
 */

package com.tiedup.remake.rig.collider;

import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;

import com.tiedup.remake.rig.math.OpenMatrix4f;
import com.tiedup.remake.rig.math.Vec3f;

/**
 * OBB géométrique forké depuis EF, strippé de la logique combat
 * (isCollide(Entity), drawInternal, updateAndSelectCollideEntity).
 * Ne garde que les données + transform() utilisés par ClothOBBCollider.
 */
public class OBBCollider {
	protected final Vec3 modelCenter;
	protected final AABB outerAABB;
	protected Vec3 worldCenter;
	protected final Vec3[] modelVertices;
	protected final Vec3[] modelNormals;
	protected Vec3[] rotatedVertices;
	protected Vec3[] rotatedNormals;
	protected Vec3f scale = new Vec3f(1.0F, 1.0F, 1.0F);

	public OBBCollider(double vertexX, double vertexY, double vertexZ, double centerX, double centerY, double centerZ) {
		this(getInitialAABB(vertexX, vertexY, vertexZ, centerX, centerY, centerZ), vertexX, vertexY, vertexZ, centerX, centerY, centerZ);
	}

	protected OBBCollider(AABB outerAABB, double vertexX, double vertexY, double vertexZ, double centerX, double centerY, double centerZ) {
		this.modelCenter = new Vec3(centerX, centerY, centerZ);
		this.outerAABB = outerAABB;
		this.worldCenter = new Vec3(0.0D, 0.0D, 0.0D);

		this.modelVertices = new Vec3[4];
		this.modelNormals = new Vec3[3];
		this.rotatedVertices = new Vec3[4];
		this.rotatedNormals = new Vec3[3];
		this.modelVertices[0] = new Vec3(vertexX, vertexY, -vertexZ);
		this.modelVertices[1] = new Vec3(vertexX, vertexY, vertexZ);
		this.modelVertices[2] = new Vec3(-vertexX, vertexY, vertexZ);
		this.modelVertices[3] = new Vec3(-vertexX, vertexY, -vertexZ);
		this.modelNormals[0] = new Vec3(1, 0, 0);
		this.modelNormals[1] = new Vec3(0, 1, 0);
		this.modelNormals[2] = new Vec3(0, 0, 1);
		this.rotatedVertices[0] = new Vec3(0.0D, 0.0D, 0.0D);
		this.rotatedVertices[1] = new Vec3(0.0D, 0.0D, 0.0D);
		this.rotatedVertices[2] = new Vec3(0.0D, 0.0D, 0.0D);
		this.rotatedVertices[3] = new Vec3(0.0D, 0.0D, 0.0D);
		this.rotatedNormals[0] = new Vec3(0.0D, 0.0D, 0.0D);
		this.rotatedNormals[1] = new Vec3(0.0D, 0.0D, 0.0D);
		this.rotatedNormals[2] = new Vec3(0.0D, 0.0D, 0.0D);
	}

	static AABB getInitialAABB(double posX, double posY, double posZ, double center_x, double center_y, double center_z) {
		double xLength = Math.abs(posX) + Math.abs(center_x);
		double yLength = Math.abs(posY) + Math.abs(center_y);
		double zLength = Math.abs(posZ) + Math.abs(center_z);
		double maxLength = Math.max(xLength, Math.max(yLength, zLength));
		return new AABB(maxLength, maxLength, maxLength, -maxLength, -maxLength, -maxLength);
	}

	public void transform(OpenMatrix4f modelMatrix) {
		OpenMatrix4f noTranslation = modelMatrix.removeTranslation();

		for (int i = 0; i < this.modelVertices.length; i++) {
			this.rotatedVertices[i] = OpenMatrix4f.transform(noTranslation, this.modelVertices[i]);
		}

		for (int i = 0; i < this.modelNormals.length; i++) {
			this.rotatedNormals[i] = OpenMatrix4f.transform(noTranslation, this.modelNormals[i]);
		}

		this.scale = noTranslation.toScaleVector();
		this.worldCenter = OpenMatrix4f.transform(modelMatrix, this.modelCenter);
	}
}
