package electrosphere.entity.state.collidable;

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.ServerGravityTree;
import electrosphere.server.datacell.Realm;

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

/**
 * Server collidable tree
 */
public class ServerCollidableTree implements BehaviorTree {
    
    Entity parent;
    DBody body;
    Collidable collidable;
    boolean applyImpulses = true;
    

    public ServerCollidableTree(Entity e, Collidable collidable, DBody body){
        parent = e;
        this.collidable = collidable;
        this.body = body;
    }
    
    public ServerCollidableTree(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);
            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(ServerGravityTree.getServerGravityTree(parent)!=null){
                    ServerGravityTree.getServerGravityTree(parent).start();
                }
            }
            if(impulse.type.matches(Collidable.TYPE_CREATURE)){
//                System.out.println(System.currentTimeMillis() + " creature hit!");
                if(ServerGravityTree.getServerGravityTree(parent)!=null){
                    ServerGravityTree.getServerGravityTree(parent).start();
                }
            }
        }
        Realm realm = Globals.realmManager.getEntityRealm(parent);
        //bound to world bounds
        if(newPosition.x < realm.getServerWorldData().getWorldBoundMin().x){
            newPosition.x = realm.getServerWorldData().getWorldBoundMin().x;
        }
        if(newPosition.y < realm.getServerWorldData().getWorldBoundMin().y){
            newPosition.y = realm.getServerWorldData().getWorldBoundMin().y;
        }
        if(newPosition.z < realm.getServerWorldData().getWorldBoundMin().z){
            newPosition.z = realm.getServerWorldData().getWorldBoundMin().z;
        }
        PhysicsUtils.setRigidBodyTransform(realm.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 hasServerCollidableTree(Entity e){
        return e.containsKey(EntityDataStrings.SERVER_COLLIDABLE_TREE);
    }

    public static ServerCollidableTree getServerCollidableTree(Entity e){
        return (ServerCollidableTree)e.getData(EntityDataStrings.SERVER_COLLIDABLE_TREE);
    }
    
    
}
