package electrosphere.entity.types.structure;

import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.types.collision.CollisionObjUtils;
import electrosphere.game.collision.collidable.Collidable;
import electrosphere.game.data.structure.type.model.CollisionObjectTemplate;
import electrosphere.game.data.structure.type.model.StructureType;
import electrosphere.main.Globals;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.net.parser.net.message.NetworkMessage;
import electrosphere.net.server.Player;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3d;
import org.joml.Vector3f;
import org.joml.Vector4f;

/**
 *
 * @author amaterasu
 */
public class StructureUtils {
    
    
    public static Entity spawnBasicStructure(String type, Vector3f position, Quaternionf rotation){
        StructureType rawType = Globals.gameConfigCurrent.getStructureTypeMap().getType(type);
        Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath());
        EntityUtils.getPosition(rVal).set(position);
        EntityUtils.getRotation(rVal).set(rotation);
        for(CollisionObjectTemplate template : rawType.getCollision()){
            switch(template.getType()){
                case CollisionObjectTemplate.TYPE_CUBE:
                    Matrix4f rotationTransform = new Matrix4f().rotate(rotation);
                    Vector4f rotatedPosition = rotationTransform.transform(new Vector4f(template.getPositionX(),template.getPositionY(),template.getPositionZ(),1.0f));
                    Vector3f cubePosition = new Vector3f(position).add(rotatedPosition.x,rotatedPosition.y,rotatedPosition.z);
                    Quaternionf cubeRotation = new Quaternionf(rotation).mul(new Quaternionf(template.getRotationX(),template.getRotationY(),template.getRotationZ(),template.getRotationW())).normalize();
                    CollisionObjUtils.attachCollisionCube(new Vector3f(template.getScaleX(),template.getScaleY(),template.getScaleZ()), cubePosition, cubeRotation, rVal);
//                    Collidable collidable = new Collidable(rVal, Collidable.TYPE_STRUCTURE);
//                    Globals.collisionEngine.registerPhysicsEntity(rVal);
//                    Globals.collisionEngine.registerCollisionObject(rigidBody, collidable);
                    break;
                case CollisionObjectTemplate.TYPE_PLANE:
                    throw new UnsupportedOperationException("Haven't implemented plane collision on structures");
            }
        }
        rVal.putData(EntityDataStrings.STRUCTURE_IS_STRUCTURE,true);
        rVal.putData(EntityDataStrings.STRUCTURE_TYPE,type);
        return rVal;
    }
    
    public static boolean isStructure(Entity entity){
        return entity.getDataKeys().contains(EntityDataStrings.STRUCTURE_IS_STRUCTURE);
    }
    
    public static String getType(Entity structure){
        return (String)structure.getData(EntityDataStrings.STRUCTURE_TYPE);
    }
    
    public static void sendStructureToPlayer(Player player, Entity structure){
        int id = structure.getId();
        String type = StructureUtils.getType(structure);
        Vector3d position = EntityUtils.getPosition(structure);
        NetworkMessage message = EntityMessage.constructCreateMessage(id, EntityDataStrings.ENTITY_CATEGORY_STRUCTURE, type, (float)position.x, (float)position.y, (float)position.z);
        player.addMessage(message);
    }
}
