package electrosphere.game.server.saves;

import electrosphere.game.server.db.DatabaseUtils;
import electrosphere.game.server.terrain.manager.ServerTerrainManager;
import electrosphere.game.server.world.ServerWorldData;
import electrosphere.game.server.datacell.DataCellManager;
import electrosphere.main.Globals;
import electrosphere.util.FileUtils;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author satellite
 */
public class SaveUtils {
    
    static String deriveSaveDirectoryPath(String saveName){
        return "./saves/" + saveName;
    }
    
    /**
     * Initializes a save directory
     * @param saveName
     * @return true if initialized save, false if couldn't initialize
     */
    public static boolean initSave(String saveName){
        String dirPath = deriveSaveDirectoryPath(saveName);
        //check if exists
        if(FileUtils.checkFileExists(dirPath)){
            return false;
        }
        //create dir
        if(!FileUtils.createDirectory(dirPath)){
            //we for some unknown reason, couldn't make the save dir
            return false;
        }
        //init db file
        if(!DatabaseUtils.initCentralDBFile(dirPath)){
            return false;
        }
        return true;
    }
    
    
    
    public static boolean overwriteSave(String saveName){
        boolean rVal = false;
        return rVal;
    }
    
    public static boolean loadTerrainAndDB(String saveName){
        String dirPath = deriveSaveDirectoryPath(saveName);
        String dbFilePath = FileUtils.sanitizeFilePath(dirPath) + "/central.db";
        Globals.dbController.connect(dbFilePath);
        Globals.serverTerrainManager.load(saveName);
        return true;
    }
    
    public static boolean loadSave(String saveName){
        String dirPath = deriveSaveDirectoryPath(saveName);
        String dbFilePath = FileUtils.sanitizeFilePath(dirPath) + "/central.db";
        Globals.dbController.connect(dbFilePath);
        Globals.serverTerrainManager.load(saveName);
        Globals.serverWorldData = FileUtils.loadObjectFromSavePath(saveName, "world.json", ServerWorldData.class);
        return true;
    }
    
    public static List<String> getSaves(){
        return FileUtils.listDirectory("./saves");
    }
    
    public static boolean worldHasSave(String saveName){
        String dirPath = deriveSaveDirectoryPath(saveName) + "/world.json";
        System.out.println("Exists? " + dirPath);
        return FileUtils.checkFileExists(dirPath);
    }
    
    public static boolean loadTerrainAndCreateWorldData(){
        Globals.serverTerrainManager = new ServerTerrainManager(2000,50,100,0.0f,0);
        SaveUtils.loadTerrainAndDB(Globals.currentSaveName);
        Globals.serverWorldData = ServerWorldData.createGameWorld(Globals.serverTerrainManager);
        Globals.dataCellManager = new DataCellManager(Globals.serverWorldData);
        return true;
    }
    
    public static boolean saveWorldDataToSave(String saveName){
        /*
        Globals.serverWorldData = ServerWorldData.createGameWorld(Globals.serverTerrainManager);
                    //TODO: Globals.dataCellManager = new DataCellManager(Globals.serverWorldData);
        */
        String dirPath = deriveSaveDirectoryPath(saveName);
        FileUtils.serializeObjectToFilePath(dirPath + "/world.json", Globals.serverWorldData);
        return true;
    }
    
}
