package electrosphere.game.server.world;

import electrosphere.server.fluid.generation.DefaultFluidGenerator;
import electrosphere.server.fluid.manager.ServerFluidManager;
import electrosphere.server.terrain.generation.DefaultChunkGenerator;
import electrosphere.server.terrain.manager.ServerTerrainChunk;
import electrosphere.server.terrain.manager.ServerTerrainManager;
import electrosphere.util.FileUtils;

import org.joml.Vector3d;
import org.joml.Vector3f;
import org.joml.Vector3i;

/**
 * Server data about the world
 */
public class ServerWorldData {
    
    public static enum WorldType {
        GAME_WORLD,
        ARENA_WORLD,
        LEVEL,
    }
    
    WorldType type;
    
    
    /*
    
                       world max
    +---------------------+
    |                     |
    |                     |
    |                     |
    |                     |
    |                     |
    +---------------------+
 world min
    
    
    basically we're saying what the maximum and minimum x and z something can occupy are
    
    FOR THE TIME BEING DOES NOT ACCOUNT FOR Y
    */
    Vector3f worldMinPoint;
    Vector3f worldMaxPoint;
    
    int worldSizeDiscrete;
    int worldSizeDiscreteVertical;
    int dynamicInterpolationRatio;
    float randomDampener;
    boolean isArena = false;

    
    //terrain data
    private ServerTerrainManager serverTerrainManager;

    //fluid data
    private ServerFluidManager serverFluidManager;
    

    /**
     * Creates a server world data object based on a discrete world size
     * @param discreteWorldSize The discrete world size
     * @return The server world data object
     */
    public static ServerWorldData createGriddedRealmWorldData(int discreteWorldSize){
        ServerWorldData rVal = new ServerWorldData();
        rVal.type = WorldType.LEVEL;

        //min and max real points
        rVal.worldMinPoint = new Vector3f(0,0,0);
        int worldDim = discreteWorldSize * ServerTerrainChunk.CHUNK_DIMENSION;
        rVal.worldMaxPoint = new Vector3f(worldDim,worldDim, worldDim);

        //misc values
        rVal.dynamicInterpolationRatio = 1;
        rVal.worldSizeDiscrete = discreteWorldSize;
        rVal.worldSizeDiscreteVertical = discreteWorldSize;
        rVal.randomDampener = ServerTerrainManager.SERVER_TERRAIN_MANAGER_DAMPENER;

        return rVal;
    }
    
    // public static ServerWorldData createGameWorld(ServerTerrainManager terrainManager){
    //     ServerWorldData rVal = new ServerWorldData();
    //     rVal.type = WorldType.GAME_WORLD;
        
    //     rVal.worldMinPoint = new Vector3f(0,0,0);
    //     int worldDim = terrainManager.getWorldDiscreteSize() * ServerTerrainChunk.CHUNK_DIMENSION;
    //     rVal.worldMaxPoint = new Vector3f(worldDim,0,worldDim);
        
    //     rVal.dynamicInterpolationRatio = terrainManager.getDynamicInterpolationRatio();
    //     rVal.worldSizeDiscrete = terrainManager.getWorldDiscreteSize();
    //     rVal.worldSizeDiscreteVertical = 128;
    //     rVal.randomDampener = terrainManager.getRandomDampener();
        
    //     return rVal;
    // }

    /**
     * Loads world data from a scene or a save
     * @param sceneOrSaveName The name of the scene or save
     * @param isScene true if loading from a scene, false if loading from a save
     * @return The server world data
     */
    public static ServerWorldData loadWorldData(String sceneOrSaveName, boolean isScene){
        //
        //Read world data if it exists
        //
        ServerWorldData serverWorldData = null;
        ServerTerrainManager serverTerrainManager = null;
        ServerFluidManager serverFluidManager = null;
        if(isScene){
            serverWorldData = FileUtils.loadObjectFromSavePath(sceneOrSaveName, "world.json", ServerWorldData.class);
            serverTerrainManager = new ServerTerrainManager(serverWorldData, 0, new DefaultChunkGenerator());
            serverTerrainManager.load(sceneOrSaveName);
            serverFluidManager = new ServerFluidManager(serverWorldData, serverTerrainManager, 0, new DefaultFluidGenerator());
        } else {
            //TODO: Allow loading procedurally generated terrain from disk (the chunk generator is always default currently)
            serverWorldData = FileUtils.loadObjectFromSavePath(sceneOrSaveName, "world.json", ServerWorldData.class);
            serverTerrainManager = new ServerTerrainManager(serverWorldData, 0, new DefaultChunkGenerator());
            serverTerrainManager.load(sceneOrSaveName);
            serverFluidManager = new ServerFluidManager(serverWorldData, serverTerrainManager, 0, new DefaultFluidGenerator());
        }
        serverWorldData.setManagers(serverTerrainManager, serverFluidManager);
        return serverWorldData;
    }
    
    
    public Vector3f getWorldBoundMin(){
        return worldMinPoint;
    }
    
    public Vector3f getWorldBoundMax(){
        return worldMaxPoint;
    }

    public int getWorldSizeDiscrete() {
        return worldSizeDiscrete;
    }

    public int getDynamicInterpolationRatio() {
        return dynamicInterpolationRatio;
    }

    public float getRandomDampener() {
        return randomDampener;
    }
    
    public int convertRealToChunkSpace(double real){
        return (int)Math.floor(real / ServerTerrainChunk.CHUNK_DIMENSION);
    }

    public float convertChunkToRealSpace(int chunk){
        return chunk * ServerTerrainChunk.CHUNK_DIMENSION;
    }
    
    public double getRelativeLocation(double real, int world){
        return real - (world * dynamicInterpolationRatio);
    }

    public boolean isArena() {
        return isArena;
    }
    

    public int convertRealToWorld(double real){
        return convertRealToChunkSpace(real);
    }

    public double convertWorldToReal(int world){
        return convertChunkToRealSpace(world);
    }

    public Vector3i convertRealToChunkSpace(Vector3d position){
        return new Vector3i(
            convertRealToChunkSpace(position.x),
            convertRealToChunkSpace(position.y),
            convertRealToChunkSpace(position.z)
        );
    }

    /**
     * Converts a world space vector to a real space vector
     * @param position The world space vector
     * @return The real space vector
     */
    public Vector3d convertWorldToRealSpace(Vector3i position){
        return new Vector3d(
            convertWorldToReal(position.x),
            convertWorldToReal(position.y),
            convertWorldToReal(position.z)
        );
    }

    public Vector3i convertRealToVoxelSpace(Vector3d position){
        return new Vector3i(
            (int)Math.floor(position.x - convertChunkToRealSpace(convertRealToChunkSpace(position.x))),
            (int)Math.floor(position.y - convertChunkToRealSpace(convertRealToChunkSpace(position.y))),
            (int)Math.floor(position.z - convertChunkToRealSpace(convertRealToChunkSpace(position.z)))
        );
    }

    /**
     * Converts a real coordinate to a world space coordinate
     * @param position The real coordinate
     * @return The world space coordinate
     */
    public Vector3i convertRealToWorldSpace(Vector3d position){
        return new Vector3i(
            convertRealToChunkSpace(position.x),
            convertRealToChunkSpace(position.y),
            convertRealToChunkSpace(position.z)
        );
    }
    
    /**
     * Gets the terrain manager for this world
     * @return The terrain manager if it exists, null otherwise
     */
    public ServerTerrainManager getServerTerrainManager(){
        return this.serverTerrainManager;
    }

    /**
     * Gets the fluid manager for this world
     * @return The fluid manager if it exists, null otherwise
     */
    public ServerFluidManager getServerFluidManager(){
        return this.serverFluidManager;
    }

    /**
     * Sets the chunk managers
     * @param serverTerrainManager The terrain manager
     * @param serverFluidManager The fluid manager
     */
    public void setManagers(ServerTerrainManager serverTerrainManager, ServerFluidManager serverFluidManager){
        this.serverTerrainManager = serverTerrainManager;
        this.serverFluidManager = serverFluidManager;
        this.serverTerrainManager.setParent(this);
        this.serverFluidManager.setParent(this);
        if(this.serverTerrainManager == null || this.serverFluidManager == null){
            throw new IllegalStateException("Setting world data managers to a null manager " + this.serverTerrainManager + " " + this.serverFluidManager);
        }
    }

}
