package electrosphere.entity.state.collidable;

import org.joml.Quaterniond;
import org.joml.Vector3d;
import org.ode4j.ode.DBody;

import electrosphere.collision.PhysicsUtils;
import electrosphere.collision.collidable.Collidable;
import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.btree.BehaviorTree;
import electrosphere.entity.state.gravity.ClientGravityTree;

/**
 * Client collidable tree
 */
public class ClientCollidableTree implements BehaviorTree {
    
    Entity parent;
    //the ode body for this collidable tree
    DBody body;
    Collidable collidable;

    boolean applyImpulses = true;
    
    public ClientCollidableTree(Entity e, Collidable collidable, DBody body){
        parent = e;
        this.collidable = collidable;
        this.body = body;
    }
    
    public ClientCollidableTree(Entity e, Collidable collidable, DBody body, boolean applyImpulses){
        parent = e;
        this.collidable = collidable;
        this.body = body;
        this.applyImpulses = applyImpulses;
    }
    
    static int incrementer = 0;
    
    public void simulate(float deltaTime){
        Vector3d position = EntityUtils.getPosition(parent);
        Quaterniond rotation = EntityUtils.getRotation(parent);
        Vector3d offsetVector = new Vector3d();
        Vector3d newPosition = new Vector3d(position);
        //have we hit a terrain impulse?
        boolean hitTerrain = false;
        //handle impulses
        for(Impulse impulse : collidable.getImpulses()){
//            collidable.getImpulses().remove(impulse);
            Vector3d impulseForce = new Vector3d(impulse.getDirection()).mul(impulse.getForce());
            if(impulse.type.matches(Collidable.TYPE_TERRAIN)){
                hitTerrain = true;
//                System.out.println("Impulse force: " + impulseForce);
//                System.out.println("Position: " + position);
            }
//             if(impulse.type.matches(Collidable.TYPE_ITEM)){
//                 if(ClientGravityTree.getClientGravityTree(parent)!=null){
//                     ClientGravityTree.getClientGravityTree(parent).start();
//                 }
//             }
//             if(impulse.type.matches(Collidable.TYPE_CREATURE)){
// //                System.out.println(System.currentTimeMillis() + " creature hit!");
//                 if(ClientGravityTree.getClientGravityTree(parent)!=null){
//                     ClientGravityTree.getClientGravityTree(parent).start();
//                 }
//             }
        }
        //bound to world bounds
        if(newPosition.x < Globals.clientWorldData.getWorldBoundMin().x){
            newPosition.x = Globals.clientWorldData.getWorldBoundMin().x;
        }
        if(newPosition.y < Globals.clientWorldData.getWorldBoundMin().y){
            newPosition.y = Globals.clientWorldData.getWorldBoundMin().y;
        }
        if(newPosition.z < Globals.clientWorldData.getWorldBoundMin().z){
            newPosition.z = Globals.clientWorldData.getWorldBoundMin().z;
        }
        PhysicsUtils.setRigidBodyTransform(Globals.clientSceneWrapper.getCollisionEngine(), newPosition, rotation, body);
    }
    
    /**
     * Sets the structures backing this collidable tree
     * @param body The ode body
     * @param collidable The collidable
     */
    public void setCollisionObject(DBody body, Collidable collidable){
        this.body = body;
        this.collidable = collidable;
    }

    public static boolean hasClientCollidableTree(Entity e){
        return e.containsKey(EntityDataStrings.CLIENT_COLLIDABLE_TREE);
    }

    public static ClientCollidableTree getClientCollidableTree(Entity e){
        return (ClientCollidableTree)e.getData(EntityDataStrings.CLIENT_COLLIDABLE_TREE);
    }
    
    
}
