/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package electrosphere.entity;

import electrosphere.entity.state.collidable.CollidableTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.renderer.Model;
import electrosphere.main.Globals;
import electrosphere.renderer.Actor;
import electrosphere.renderer.ActorUtils;
import org.joml.Quaternionf;
import org.joml.Vector3d;
import org.joml.Vector3f;

/**
 *
 * @author amaterasu
 */
public class EntityUtils {
    
    public static Vector3d getPosition(Entity e){
        return (Vector3d)e.getData(EntityDataStrings.DATA_STRING_POSITION);
    }
    
    public static Quaternionf getRotation(Entity e){
        return (Quaternionf)e.getData(EntityDataStrings.DATA_STRING_ROTATION);
    }
    
    public static Vector3f getScale(Entity e){
        return (Vector3f)e.getData(EntityDataStrings.DATA_STRING_SCALE);
    }
    
    public static String getModelPath(Entity e){
        return (String)e.getData(EntityDataStrings.DATA_STRING_MODEL_PATH);
    }
    
    public static Entity spawnDrawableEntity(String modelPath){
        Entity rVal = new Entity();
        rVal.putData(EntityDataStrings.DATA_STRING_ACTOR, ActorUtils.createActorFromModelPath(modelPath));
//        rVal.putData(EntityDataStrings.DATA_STRING_MODEL_PATH, modelPath);
        rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0));
        rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaternionf().identity());
        rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
        rVal.putData(EntityDataStrings.DATA_STRING_DRAW, true);
        Globals.entityManager.registerEntity(rVal);
        Globals.entityManager.registerDrawableEntity(rVal);
        return rVal;
    }
    
    public static Entity spawnDrawableEntityWithPreexistingModel(String modelPath){
        Entity rVal = new Entity();
        rVal.putData(EntityDataStrings.DATA_STRING_ACTOR, ActorUtils.createActorFromModelPath(modelPath));
        rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0));
        rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaternionf().rotateAxis((float)0, new Vector3f(1,0,0)));
        rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
        rVal.putData(EntityDataStrings.DATA_STRING_DRAW, true);
        Globals.entityManager.registerEntity(rVal);
        Globals.entityManager.registerDrawableEntity(rVal);
        return rVal;
    }
    
    public static Entity spawnUIEntity(String modelPath){
        Entity rVal = new Entity();
        rVal.putData(EntityDataStrings.DATA_STRING_ACTOR, ActorUtils.createActorFromModelPath(modelPath));
        rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0));
        rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaternionf().rotateAxis((float)0, new Vector3f(1,0,0)));
        rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
        rVal.putData(EntityDataStrings.DATA_STRING_UI_ELEMENT, true);
        Globals.entityManager.registerEntity(rVal);
        Globals.entityManager.registerUIEntity(rVal);
        return rVal;
    }
    
    public static void cleanUpDrawableEntity(Entity e){
        if(e != null){
            Globals.entityManager.deregisterEntity(e);
//            getEntityModelPath(e).free();
        }
    }
    
    public static void setEntityID(Entity e, int id){
        Globals.entityManager.overrideEntityId(e, id);
    }
    
    public static Actor getActor(Entity e){
        return (Actor)e.getData(EntityDataStrings.DATA_STRING_ACTOR);
    }
    
    public static CollidableTree getCollidableTree(Entity e){
        return (CollidableTree)e.getData(EntityDataStrings.COLLIDABLE_TREE);
    }
    
    public static void setVisible(Entity entity, boolean visible){
        entity.putData(EntityDataStrings.DATA_STRING_DRAW, visible);
    }
    
}
