package electrosphere.engine.loadingthreads;

import java.util.concurrent.TimeUnit;

import org.joml.Vector3d;

import electrosphere.auth.AuthenticationManager;
import electrosphere.client.ui.menu.MenuGenerators;
import electrosphere.client.ui.menu.WindowStrings;
import electrosphere.client.ui.menu.WindowUtils;
import electrosphere.engine.Globals;
import electrosphere.engine.signal.Signal.SignalType;
import electrosphere.logger.LoggerInterface;
import electrosphere.net.parser.net.message.TerrainMessage;
import electrosphere.net.server.player.Player;
import electrosphere.renderer.ui.elements.Window;
import electrosphere.server.db.DatabaseController;

/**
 * Loads the viewport
 */
public class ViewportLoading {
    
    /**
     * Loads the viewport
     */
    protected static void loadViewport(Object[] params){
        //
        //show loading
        Globals.signalSystem.post(SignalType.UI_MODIFICATION, ()->{
            Window loadingWindow = (Window)Globals.elementService.getWindow(WindowStrings.WINDOW_LOADING);
            WindowUtils.recursiveSetVisible(Globals.elementService.getWindow(WindowStrings.WINDOW_MENU_MAIN), false);
            WindowUtils.replaceMainMenuContents(MenuGenerators.createEmptyMainMenu());
            loadingWindow.setVisible(true);
        });

        //
        //init realm manager with viewport realm
        Globals.realmManager.createViewportRealm(new Vector3d(0,0,0), new Vector3d(16,16,16));

        //
        //connect client to server
        LoggerInterface.loggerEngine.INFO("run server: " + Globals.RUN_SERVER + " run client: " + Globals.RUN_CLIENT);
        Globals.dbController = new DatabaseController();
        LoadingUtils.initAuthenticationManager(true);
        Globals.clientUsername = "leveleditor";
        Globals.clientPassword = AuthenticationManager.getHashedString("leveleditor");
        LoadingUtils.initLocalConnection(true);
        //wait for player object creation
        while(Globals.playerManager.getPlayers().size() < 1){
            try {
                TimeUnit.MILLISECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //add player to viewport realm
        Player localPlayer = Globals.playerManager.getFirstPlayer();
        Globals.realmManager.first().getDataCellManager().addPlayerToRealm(localPlayer);

        //initialize the "real" objects simulation
        LoadingUtils.initMicroSimulation();
        LoadingUtils.setSimulationsToReady();
        LoggerInterface.loggerEngine.INFO("[Server]Finished loading level editor");

        //request terrain data
        Globals.clientConnection.queueOutgoingMessage(TerrainMessage.constructRequestMetadataMessage());

        //block for client world data
        while(Globals.clientWorldData == null){
            try {
                TimeUnit.MILLISECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //Run client startup process
        ClientLoading.loadViewport(params);
    }

}
