package electrosphere.server.content;

import java.util.List;

import org.joml.Vector3i;

import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.server.content.serialization.ContentSerialization;
import electrosphere.server.datacell.Realm;
import electrosphere.server.datacell.ServerDataCell;
import electrosphere.server.saves.SaveUtils;
import electrosphere.util.FileUtils;

public class ServerContentManager {

    //controls whether the manager should generate content on loading a new scene
    boolean generateContent = false;

    /**
     * Constructor
     */
    private ServerContentManager(boolean generateContent){
        this.generateContent = generateContent;
    }

    /**
     * Creates a server content manager
     * @param generateContent if true, will generate content on loading a new scene, otherwise will not
     * @return The server content manager
     */
    public static ServerContentManager createServerContentManager(boolean generateContent){
        return new ServerContentManager(generateContent);
    }
    
    /**
     * Generates content for a given data cell
     * @param realm The realm
     * @param worldPos The world position
     * @param cell The cell
     */
    public void generateContentForDataCell(Realm realm, Vector3i worldPos, ServerDataCell cell, String cellKey){
        String fullPath = "/content/" + cellKey + ".dat";
        if(generateContent){ //in other words, if not arena mode
            if(FileUtils.checkSavePathExists(Globals.currentSave.getName(), fullPath)){
                //if on disk (has already been generated)
                ContentSerialization contentRaw = FileUtils.loadObjectFromSavePath(Globals.currentSave.getName(), fullPath, ContentSerialization.class);
                contentRaw.hydrateRawContent(realm,cell);
            } else {
                //else create from scratch
                //UNCOMMENT THIS WHEN YOU WANT CONTENT GENERATED FOR WORLDS AGAIN
                // EnvironmentGenerator.generateForest(realm, cell, worldPos, 0);
            }
        } else {
            //just because content wasn't generated doesn't mean there isn't data saved under that key
            if(FileUtils.checkSavePathExists(Globals.currentSave.getName(), fullPath)){
                //if on disk (has already been generated)
                ContentSerialization contentRaw = FileUtils.loadObjectFromSavePath(Globals.currentSave.getName(), fullPath, ContentSerialization.class);
                contentRaw.hydrateRawContent(realm,cell);
            }
        }
        //TODO: generate navmesh
        // cell.setNavMesh(
        //     NavMeshUtils.createMeshFromChunk(Globals.serverTerrainManager.getChunk(
        //         worldPos.x,
        //         worldPos.y,
        //         worldPos.z
        //     ),
        //     Globals.navMeshManager.getBlockerCache().getBlocker(worldPos.x, worldPos.z))
        // );
    }

    /**
     * Saves entity content to disk
     * @param locationKey the location key to save under
     * @param entities the list of entities to save
     */
    public void saveContentToDisk(String locationKey, List<Entity> entities){
        ContentSerialization serialization = ContentSerialization.constructContentSerialization(entities);
        String dirPath = SaveUtils.deriveSaveDirectoryPath(Globals.currentSave.getName());
        String fullPath = dirPath + "/content/" + locationKey + ".dat";
        FileUtils.serializeObjectToFilePath(fullPath, serialization);
    }


}
