package electrosphere.menu;

import electrosphere.audio.AudioSource;
import electrosphere.audio.AudioUtils;
import electrosphere.controls.ControlHandler;
import electrosphere.controls.ControlHandler.ControlsState;
import electrosphere.engine.LoadingThread;
import electrosphere.game.server.saves.SaveUtils;
import electrosphere.main.Globals;
import electrosphere.main.Main;
import electrosphere.net.NetUtils;
import electrosphere.net.client.ClientNetworking;
import electrosphere.renderer.ui.Widget;
import electrosphere.renderer.ui.font.TextBox;
import electrosphere.renderer.ui.label.Label;
import org.joml.Vector3f;

/**
 *
 * @author amaterasu
 */
public class MenuTransition {
    public static void selectOption(Menu m){
        switch(m.getType()){
            case TITLE_MENU:
                switch(((TextBox)m.getCurrentOption()).getText()){
                    //single player
                    case "SINGLEPLAYER":
//                        m.dispose();
//                        Globals.loadingThread = new LoadingThread(LoadingThread.LOAD_MAIN_GAME);
//                        Globals.RUN_CLIENT = true;
//                        Globals.RUN_SERVER = true;
//                        Globals.loadingThread.start();
                        m.dispose();
                        Globals.currentMenu = MenuUtils.createWorldSelectMenu();
                        break;
                    //multi player
                    case "MULTIPLAYER":
                        m.dispose();
                        Globals.currentMenu = MenuUtils.createMultiplayerMenu();
                        break;
                    //arena
                    case "ARENA":
                        m.dispose();
                        Globals.loadingThread = new LoadingThread(LoadingThread.LOAD_ARENA);
                        Globals.RUN_CLIENT = true;
                        Globals.RUN_SERVER = true;
                        Globals.loadingThread.start();
                        break;
                    //options
                    case "OPTIONS":
                        m.dispose();
                        Globals.currentMenu = MenuUtils.createOptionsMainMenu();
                        break;
                    //test
                    case "UI TESTING":
                        m.dispose();
                        Globals.currentMenu = MenuUtils.createTestMainMenu();
                        break;
                }
                break;
            case WORLD_SELECT_MENU:
                switch(((TextBox)m.getCurrentOption()).getText()){
                    case "CREATE WORLD":
                        m.dispose();
                        Globals.currentMenu = MenuUtils.createWorldCreationMenu();
                        break;
                    default:
                        String worldName = ((TextBox)m.getCurrentOption()).getText();
                        m.dispose();
                        //check if has save or not
                        if(SaveUtils.worldHasSave(worldName.toLowerCase())){
                            Globals.loadingThread = new LoadingThread(LoadingThread.LOAD_MAIN_GAME);
                            Globals.RUN_CLIENT = true;
                            Globals.RUN_SERVER = true;
                            Globals.loadingThread.start();
                        } else {
                            Globals.currentSaveName = worldName.toLowerCase();
                            SaveUtils.loadTerrainAndCreateWorldData();
                            Globals.currentMenu = MenuUtils.createSaveCreationMenu();
                        }
                        break;
                }
                break;
            case WORLD_CREATE_MENU:
                break;
            case SAVE_CREATE_MENU:
                switch(((TextBox)m.getCurrentOption()).getText()){
                    case "SAVE":
                        m.dispose();
                        SaveUtils.saveWorldDataToSave(Globals.currentSaveName);
                        Globals.currentMenu = MenuUtils.createWorldSelectMenu();
                        break;
                    case "CANCEL":
                        m.dispose();
                        Globals.currentMenu = MenuUtils.createWorldSelectMenu();
                        break;
                }
                break;
            case CHARACTER_CREATE_MENU:
                break;
            case FINALIZE_SAVE_CREATION_MENU:
                break;
            case MULTIPLAYER_MENU:
                switch(((TextBox)m.getCurrentOption()).getText()){
                    //HOST
                    case "HOST":
                        m.dispose();
                        Globals.loadingThread = new LoadingThread(LoadingThread.LOAD_MAIN_GAME);
                        Globals.RUN_CLIENT = true;
                        Globals.RUN_SERVER = true;
                        Globals.loadingThread.start();
                        break;
                    //JOIN
                    case "JOIN":
                        m.dispose();
                        Globals.currentMenu = MenuUtils.createIPMenu();
                        break;
                    //back
                    case "BACK":
                        backout(m);
                        break;
                        
                }
                break;
            case IP_MENU:
                switch(((TextBox)m.getCurrentOption()).getText()){
                    //connect
                    case "CONNECT":
                        NetUtils.setAddress( ( (TextBox)m.getOptions().get(0) ).getText() );
                        NetUtils.setPort( Integer.parseInt( ( (TextBox)m.getOptions().get(1) ).getText() ) );
                        m.dispose();
                        Globals.loadingThread = new LoadingThread(LoadingThread.LOAD_MAIN_GAME);
                        Globals.RUN_CLIENT = true;
                        Globals.RUN_SERVER = false;
                        Globals.loadingThread.start();
                        break;
                    //back
                    case "BACK":
                        backout(m);
                        break;
                }
                break;
            case IN_GAME_MAIN_MENU:
                switch(((Label)m.getCurrentOption()).getText()){
                    //connect
                    case "QUIT":
                        //TODO: actually shut down program
                        Globals.ENGINE_SHUTDOWN_FLAG = true;
                        break;
                    //back
                    case "BACK":
                        MenuUtils.makeMenuUndrawable(Globals.currentMenu);
                        Globals.currentMenu.dispose();
                        Globals.controlHandler.setHandlerState(ControlsState.MAIN_GAME);
                        break;
                }
                break;
        }
    }
    
    public static void backout(Menu m){
        AudioSource startupSound = AudioUtils.playAudioAtLocation("/Audio/MenuBackspace.ogg", new Vector3f(0,0,0));
        switch(m.getType()){
            case TITLE_MENU:
                Main.running = false;
                break;
            case WORLD_SELECT_MENU:
                m.dispose();
                Globals.currentMenu = MenuUtils.createTitleMenu();
                break;
            case WORLD_CREATE_MENU:
                m.dispose();
                Globals.currentMenu = MenuUtils.createWorldSelectMenu();
                break;
            case SAVE_CREATE_MENU:
                m.dispose();
                Globals.currentMenu = MenuUtils.createWorldSelectMenu();
                break;
            case CHARACTER_CREATE_MENU:
                m.dispose();
                Globals.currentMenu = MenuUtils.createSaveCreationMenu();
                break;
            case FINALIZE_SAVE_CREATION_MENU:
                m.dispose();
                Globals.currentMenu = MenuUtils.createCharacterCreationMenu();
                break;
            case MULTIPLAYER_MENU:
                m.dispose();
                Globals.currentMenu = MenuUtils.createTitleMenu();
                break;
            case IP_MENU:
                m.dispose();
                Globals.currentMenu = MenuUtils.createMultiplayerMenu();
                break;
            case OPTIONS_MAIN_MENU:
                m.dispose();
                Globals.currentMenu = MenuUtils.createTitleMenu();
                break;
            case TEST:
                m.dispose();
                Globals.currentMenu = MenuUtils.createTitleMenu();
                break;
            case IN_GAME_MAIN_MENU:
                MenuUtils.makeMenuUndrawable(Globals.currentMenu);
                Globals.currentMenu.dispose();
                Globals.controlHandler.setHandlerState(ControlsState.MAIN_GAME);
                break;
        }
    }
    
    public static void menuHandleKeypress(Menu m, String keycode){
        char toWrite = ' ';
        Widget currentOption = m.getCurrentOption();
        boolean backspace = false;
        switch(keycode){
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_BACKSPACE:
                if(currentOption instanceof TextBox){
                    TextBox currentTextBox = (TextBox)currentOption;
                    String currentText = currentTextBox.getText();
                    if(currentText.length() > 0){
                        currentTextBox.setText(currentText.substring(0, currentText.length() - 1));
                        AudioSource startupSound = AudioUtils.playAudioAtLocation("/Audio/MenuBackspace.ogg", new Vector3f(0,0,0));
                    }
                }
                backspace = true;
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_0:
                toWrite = '0';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_1:
                toWrite = '1';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_2:
                toWrite = '2';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_3:
                toWrite = '3';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_4:
                toWrite = '4';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_5:
                toWrite = '5';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_6:
                toWrite = '6';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_7:
                toWrite = '7';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_8:
                toWrite = '8';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_9:
                toWrite = '9';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_A:
                toWrite = 'A';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_B:
                toWrite = 'B';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_C:
                toWrite = 'C';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_D:
                toWrite = 'D';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_E:
                toWrite = 'E';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_F:
                toWrite = 'F';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_G:
                toWrite = 'G';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_H:
                toWrite = 'H';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_I:
                toWrite = 'I';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_J:
                toWrite = 'J';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_K:
                toWrite = 'K';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_L:
                toWrite = 'L';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_M:
                toWrite = 'M';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_N:
                toWrite = 'N';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_O:
                toWrite = 'O';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_P:
                toWrite = 'P';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_Q:
                toWrite = 'Q';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_R:
                toWrite = 'R';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_S:
                toWrite = 'S';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_T:
                toWrite = 'T';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_U:
                toWrite = 'U';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_V:
                toWrite = 'V';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_W:
                toWrite = 'W';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_X:
                toWrite = 'X';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_Y:
                toWrite = 'Y';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_Z:
                toWrite = 'Z';
                break;
            case ControlHandler.DATA_STRING_INPUT_CODE_MENU_TYPE_PERIOD:
                toWrite = '.';
                break;
        }
        if(!backspace){
            if(currentOption instanceof TextBox){
                TextBox currentTextBox = (TextBox)currentOption;
                String currentText = currentTextBox.getText();
                if(currentText.length() < currentTextBox.getCols()){
                    currentTextBox.setText(currentText + toWrite);
                    AudioSource startupSound = AudioUtils.playAudioAtLocation("/Audio/MenuType.ogg", new Vector3f(0,0,0));
                }
            }
        }
    }
}
