package electrosphere.collision;

import java.nio.IntBuffer;

import org.joml.Vector3d;
import org.lwjgl.PointerBuffer;
import org.lwjgl.assimp.AIFace;
import org.lwjgl.assimp.AIMesh;
import org.lwjgl.assimp.AIScene;
import org.lwjgl.assimp.AIVector3D;
import org.ode4j.math.DMatrix3;
import org.ode4j.ode.DBody;
import org.ode4j.ode.DBox;
import org.ode4j.ode.DCapsule;
import org.ode4j.ode.DCylinder;
import org.ode4j.ode.DGeom;
import org.ode4j.ode.DSphere;
import org.ode4j.ode.DTriMesh;

import electrosphere.entity.types.terrain.TerrainChunkData;

/**
 * Utilities for creating types of rigid bodies
 */
public class CollisionBodyCreation {

    //Matrix for correcting initial axis of eg cylinders or capsules
    //this rotates by 90 degrees along the x axis
    public static final DMatrix3 AXIS_CORRECTION_MATRIX = new DMatrix3(
        1.0000000,  0.0000000,  0.0000000,
        0.0000000,  0.0000000, -1.0000000,
        0.0000000,  1.0000000,  0.0000000
    );


    
    //The width of a plane rigid body
    //It's really a box under the hood
    static final double PLANE_WIDTH = 0.3;

    /**
     * Creates a plane DBody. Dimensions x and z control the length and width of the plane;
     * @param dimensions The dimensions of the plane
     * @return The DBody
     */
    public static DBody createPlaneBody(CollisionEngine collisionEngine, Vector3d dimensions, long categoryBits){
        DBox geom = collisionEngine.createCubeGeom(new Vector3d(dimensions.x,PLANE_WIDTH,dimensions.z),categoryBits);
        return collisionEngine.createDBody(geom);
    }

    /**
     * Creates a cube DBody. Dimensions controlled by the dimensions vector.
     * @param collisionEngine The collision engine to create the body inside of
     * @param dimensions The dimensions of the cube
     * @return The DBody
     */
    public static DBody createCubeBody(CollisionEngine collisionEngine, Vector3d dimensions, long categoryBits){
        DBox geom = collisionEngine.createCubeGeom(new Vector3d(dimensions),categoryBits);
        return collisionEngine.createDBody(geom);
    }

    /**
     * Creates a cylinder DBody. Dimensions controlled by the dimensions vector.
     * @param collisionEngine The collision engine to create the body inside of
     * @param dimensions The dimensions of the cube
     * @return The DBody
     */
    public static DBody createCylinderBody(CollisionEngine collisionEngine, double radius, double length, long categoryBits){
        DCylinder geom = collisionEngine.createCylinderGeom(radius,length,categoryBits);
        DBody returnBody = collisionEngine.createDBody(geom);
        collisionEngine.setOffsetRotation(geom); //ode4j required geom to already be on body before rotating for some reason
        return returnBody;
    }

    /**
     * Creates a sphere body in the collision engine
     * @param collisionEngine The collision engine
     * @param radius The radius of the sphere
     * @return The DBody
     */
    public static DBody createSphereBody(CollisionEngine collisionEngine, double radius, long categoryBits){
        DSphere geom = collisionEngine.createSphereGeom(radius,categoryBits);
        return collisionEngine.createDBody(geom);
    }

    /**
     * Creates a dbody with existing shapes that are provided
     * @param collisionEngine the collision engine to create it in
     * @param geoms the geometries to attach
     * @return the dbody
     */
    public static DBody createBodyWithShapes(CollisionEngine collisionEngine, DGeom ... geoms){
        return collisionEngine.createDBody(geoms);
    }

    /**
     * Creates a sphere shape
     * @param collisionEngine the collision engine
     * @param radius the radius of the sphere
     * @param categoryBits the category bits for the shape
     * @return the sphere shape
     */
    public static DSphere createShapeSphere(CollisionEngine collisionEngine, double radius, long categoryBits){
        return collisionEngine.createSphereGeom(radius, categoryBits);
    }

    /**
     * Creates a capsule shape
     * @param collisionEngine The collision engine
     * @param radius the radius of the capsule
     * @param length the length of the capsule
     * @param categoryBits the category bits for the shape
     * @return the capsule shape
     */
    public static DCapsule createCapsuleShape(CollisionEngine collisionEngine, double radius, double length, long categoryBits){
        return collisionEngine.createCapsuleGeom(radius, length, categoryBits);
    }

    /**
     * Sets the provided body to be a kinematic body (no gravity applied)
     * @param collisionEngine The collision engine
     * @param body The body
     */
    public static void setKinematic(CollisionEngine collisionEngine, DBody body){
        collisionEngine.setKinematic(body);
    }

    /**
     * Sets the gravity mode of the body
     * @param collisionEngine the collision engine
     * @param body the body
     * @param gravityMode the gravity mode value
     */
    public static void setGravityMode(CollisionEngine collisionEngine, DBody body, boolean gravityMode){
        collisionEngine.setGravityMode(body, gravityMode);
    }

    /**
     * Sets the offset position of the first geometry in a given body
     * @param collisionEngine The collision engine
     * @param body The body
     * @param offsetPosition The position to offset the first geometry by
     */
    public static void setOffsetPosition(CollisionEngine collisionEngine, DBody body, Vector3d offsetPosition){
        collisionEngine.setOffsetPosition(body, offsetPosition);
    }

    /**
     * Removes a geom from a body
     * @param collisionEngine the collision engine
     * @param body the body
     * @param geom the geometry
     */
    public static void removeShapeFromBody(CollisionEngine collisionEngine, DBody body, DGeom geom){
        collisionEngine.removeGeometryFromBody(body, geom);
    }

    /**
     * Destroys a geometry
     * @param collisionEngine The collision engine
     * @param geom the geometry
     */
    public static void destroyShape(CollisionEngine collisionEngine, DGeom geom){
        collisionEngine.destroyGeom(geom);
    }

    /**
     * Attaches a geom to a body
     * @param collisionEngine the collision engine
     * @param body the body
     * @param geom the geometry
     */
    public static void attachGeomToBody(CollisionEngine collisionEngine, DBody body, DGeom geom){
        collisionEngine.attachGeomToBody(body, geom);
    }

    
    /**
     * Creates an ode DBody from a terrain chunk data object
     * @param data The terrain data
     * @return The DBody
     */
    public static DBody generateBodyFromTerrainData(CollisionEngine collisionEngine, TerrainChunkData data, long categoryBits){
        DBody body = null;
        
        //create data
        int numberTriangles = data.getFaceElements().size() / 3;
        int numberVertices = data.getVertices().size() / 3;
        
        float[] vertices = new float[numberVertices * 3];
        int vertexInserterPos = 0;
        int[] indices = new int[numberTriangles * 3];
        int indexInserterPos = 0;
        
        for(float vertexValue : data.getVertices()){
            vertices[vertexInserterPos] = vertexValue;
            vertexInserterPos++;
        }

        for(int element : data.getFaceElements()){
            indices[indexInserterPos] = element;
            indexInserterPos++;
        }
        
        //create trimesh
        if(vertices.length > 0){
            DTriMesh triMesh = collisionEngine.createTrimeshGeom(vertices,indices,categoryBits);
            body = collisionEngine.createDBody(triMesh);
        }

        return body;
    }

    /**
     * Generates a body from an AIScene
     * @param scene The AIScene to generate a rigid body off of
     * @return A rigid body based on the AIScene
     */
    public static DBody generateRigidBodyFromAIScene(CollisionEngine collisionEngine, AIScene scene, long categoryBits){

        DBody body = collisionEngine.createDBody((DGeom[])null);

        PointerBuffer meshesBuffer = scene.mMeshes();
        while(meshesBuffer.hasRemaining()){
            float[] verts;
            int numVertices;
            int[] indices;
            int numTriangles;

            AIMesh aiMesh = AIMesh.create(meshesBuffer.get());
            //allocate array for vertices
            numVertices = aiMesh.mNumVertices();
            verts = new float[numVertices * 3];
            //read vertices
            AIVector3D.Buffer vertexBuffer = aiMesh.mVertices();
            int vertPos = 0;
            while(vertexBuffer.hasRemaining()){
                AIVector3D vector = vertexBuffer.get();
                verts[vertPos+0] = vector.x();
                verts[vertPos+1] = vector.y();
                verts[vertPos+2] = vector.z();
                vertPos = vertPos + 3;
            }
            numTriangles = aiMesh.mNumFaces();
            indices = new int[numTriangles * 3];
            int indicesPos = 0;
            //read faces
            AIFace.Buffer faceBuffer = aiMesh.mFaces();
            while(faceBuffer.hasRemaining()){
                AIFace currentFace = faceBuffer.get();
                IntBuffer indexBuffer = currentFace.mIndices();
                while(indexBuffer.hasRemaining()){
                    int index = indexBuffer.get();
                    indices[indicesPos] = index;
                    indicesPos++;
                }
            }
            DTriMesh meshGeom = collisionEngine.createTrimeshGeom(verts, indices,categoryBits);
            meshGeom.setBody(body);
        }
        
        return body;
    }
    
}
