package electrosphere.entity;

import electrosphere.main.Globals;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;

/**
 *
 * @author amaterasu
 */
public class CameraEntityUtils {
    
    
    public static Entity spawnBasicCameraEntity(Vector3f center, Vector3f eye){
        Entity rVal = new Entity();
        Globals.entityManager.registerEntity(rVal);
        rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_TYPE, EntityDataStrings.DATA_STRING_CAMERA_TYPE_BASIC);
        rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_CENTER, center);
        rVal.putData(EntityDataStrings .DATA_STRING_CAMERA_EYE, eye);
        rVal.putData(EntityDataStrings.CAMERA_PITCH, 0.0f);
        rVal.putData(EntityDataStrings.CAMERA_YAW, 0.0f);
        return rVal;
    }
    
    public static Entity spawnPointTrackingCameraEntity(Vector3f center, Vector3f eye){
        Entity rVal = new Entity();
        Globals.entityManager.registerEntity(rVal);
        rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_TYPE, EntityDataStrings.DATA_STRING_CAMERA_TYPE_ORBIT);
        rVal.putData(EntityDataStrings.DATA_STRING_CAMERA_CENTER, center);
        rVal.putData(EntityDataStrings .DATA_STRING_CAMERA_EYE, eye);
        rVal.putData(EntityDataStrings.CAMERA_PITCH, 0.0f);
        rVal.putData(EntityDataStrings.CAMERA_YAW, 0.0f);
        return rVal;
    }
    
    public static Entity getOrbitalCameraTarget(Entity camera){
        return (Entity)camera.getData(EntityDataStrings.DATA_STRING_CAMERA_ORBIT_TARGET);
    }
    
    public static float getOrbitalCameraDistance(Entity camera){
        return (float)camera.getData(EntityDataStrings.DATA_STRING_CAMERA_ORBIT_DISTANCE);
    }
    
    public static void setCameraCenter(Entity camera, Vector3f center){
        camera.putData(EntityDataStrings.DATA_STRING_CAMERA_CENTER, center);
    }
    
    public static Vector3f getCameraCenter(Entity camera){
        return (Vector3f)camera.getData(EntityDataStrings.DATA_STRING_CAMERA_CENTER);
    }
    
    public static void setCameraEye(Entity camera, Vector3f eye){
        camera.putData(EntityDataStrings.DATA_STRING_CAMERA_EYE, eye);
    }
    
    public static Vector3f getCameraEye(Entity camera){
        return (Vector3f)camera.getData(EntityDataStrings.DATA_STRING_CAMERA_EYE);
    }
    
    public static void setCameraPitch(Entity camera, float pitch){
        camera.putData(EntityDataStrings.CAMERA_PITCH, pitch);
    }
    
    public static float getCameraPitch(Entity camera){
        return (float)camera.getData(EntityDataStrings.CAMERA_PITCH);
    }
    
    public static void setCameraYaw(Entity camera, float yaw){
        camera.putData(EntityDataStrings.CAMERA_YAW, yaw);
    }
    
    public static float getCameraYaw(Entity camera){
        return (float)camera.getData(EntityDataStrings.CAMERA_YAW);
    }
    
    public static void destroyCameraEntity(Entity e){
        if(e != null){
            Globals.entityManager.deregisterEntity(e);
        }
    }
    
    public static Matrix4f getCameraViewMatrix(Entity camera){
        Vector3f cameraCenter = new Vector3f(0,1,0);
        Vector3f cameraEye = new Vector3f(cameraCenter).add(getCameraEye(camera));
        Vector3f cameraUp = new Vector3f(0,1.0f,0);
//        System.out.println("eye: " + cameraEye);
//        System.out.println("center: " + cameraCenter);
//        System.out.println("up: " + cameraUp);
        Matrix4f rVal = new Matrix4f().setLookAt(
                    cameraEye, //eye
                    cameraCenter, //center
                    cameraUp // up
            ).scale(1.0f, 1.0f, 1.0f);
        return rVal;
    }
    
}
