package electrosphere.menu;

import electrosphere.game.server.saves.SaveUtils;
import electrosphere.main.Globals;
import electrosphere.menu.Menu.MenuType;
import electrosphere.net.NetUtils;
import electrosphere.net.server.Server;
import electrosphere.renderer.ui.Widget;
import electrosphere.renderer.ui.WidgetUtils;
import electrosphere.renderer.ui.Window;
import electrosphere.renderer.ui.label.Label;
import electrosphere.renderer.ui.widgets.ImagePanel;
import java.util.List;
import org.joml.Vector3f;

/**
 *
 * @author amaterasu
 */
public class MenuUtils {
    
    
    public static Menu createTitleMenu(){
        Menu rVal = new Menu(MenuType.TITLE_MENU);
        int screenTop = Globals.WINDOW_HEIGHT - 150;
        rVal.addElement(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop,       "ORPG", true));
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 125, "SINGLEPLAYER", true));
        rVal.setMenuOptionColor(0, new Vector3f(0.2f,0.8f,0.5f));
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 200, "MULTIPLAYER", true));
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 275, "ARENA", true));
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 350, "OPTIONS", true));
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 425, "UI TESTING", true));
        return rVal;
    }
    
    public static Menu createWorldSelectMenu(){
        Menu rVal = new Menu(MenuType.WORLD_SELECT_MENU);
        int screenTop = Globals.WINDOW_HEIGHT - 150;
        List<String> saveNames = SaveUtils.getSaves();
        int verticalPosition = 125;
        for(String saveName : saveNames){
            if(!saveName.startsWith(".")){
                rVal.addOption(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - verticalPosition, saveName.toUpperCase(), true));
                verticalPosition = verticalPosition + 75;
            }
        }
        rVal.addOption(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - verticalPosition - 200, "CREATE WORLD", true));
        return rVal;
    }
    
    public static Menu createWorldCreationMenu(){
        Menu rVal = new Menu(MenuType.WORLD_CREATE_MENU);
        int screenTop = Globals.WINDOW_HEIGHT - 150;
        List<String> saveNames = SaveUtils.getSaves();
        int verticalPosition = 125;
        //TODO: add text input to name world
        rVal.addOption(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 125, "CREATE WORLD", true));
        return rVal;
    }
    
    public static Menu createSaveCreationMenu(){
        Menu rVal = new Menu(MenuType.SAVE_CREATE_MENU);
        int screenTop = Globals.WINDOW_HEIGHT - 150;
        rVal.addOption(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 125, "SAVE", true));
        rVal.addOption(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 200, "CANCEL", true));
        return rVal;
    }
    
    public static Menu createCharacterCreationMenu(){
        Menu rVal = new Menu(MenuType.CHARACTER_CREATE_MENU);
        int screenTop = Globals.WINDOW_HEIGHT - 150;
        List<String> saveNames = SaveUtils.getSaves();
        int verticalPosition = 125;
        //TODO: add text input to name world
        rVal.addOption(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - verticalPosition, "CREATE WORLD", true));
        return rVal;
    }
    
    public static Menu createFinalizeSaveCReationMenu(){
        Menu rVal = new Menu(MenuType.FINALIZE_SAVE_CREATION_MENU);
        int screenTop = Globals.WINDOW_HEIGHT - 150;
        List<String> saveNames = SaveUtils.getSaves();
        int verticalPosition = 125;
        //TODO: add text input to name world
        rVal.addOption(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - verticalPosition, "CREATE WORLD", true));
        return rVal;
    }
    
    public static Menu createMultiplayerMenu(){
        Menu rVal = new Menu(MenuType.MULTIPLAYER_MENU);
        int screenTop = Globals.WINDOW_HEIGHT - 150;
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 125, "HOST", true));
        rVal.setMenuOptionColor(0, new Vector3f(0.2f,0.8f,0.5f));
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 200, "JOIN", true));
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 275, "BACK", true));
        return rVal;
    }
    
    public static Menu createIPMenu(){
        Menu rVal = new Menu(MenuType.IP_MENU);
        int screenTop = Globals.WINDOW_HEIGHT - 150;
        
        rVal.addElement(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop -  50, "IP ADDRESS", true));
        rVal.addOption( WidgetUtils.createVerticallyAlignedEditableTextBox(40*15, 40, screenTop - 125, 15, 1, NetUtils.getAddress(), true));
        rVal.setMenuOptionColor(0, new Vector3f(0.2f,0.8f,0.5f));
        rVal.addElement(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 200, "PORT", true));
        rVal.addOption( WidgetUtils.createVerticallyAlignedEditableTextBox(40*5, 40, screenTop - 275, 5, 1, NetUtils.DEFAULT_PORT + "", true));
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 350, "CONNECT", true));
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 425, "BACK", true));
        return rVal;
    }
    
    public static Menu createOptionsMainMenu(){
        Menu rVal = new Menu(MenuType.OPTIONS_MAIN_MENU);
        int screenTop = Globals.WINDOW_HEIGHT - 150;
        
        rVal.addElement(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop -  50, "OPTIONS", true));
        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 275, "BACK", true));
        return rVal;
    }
    
    public static Menu createInGameMainMenu(){
        Menu rVal = new Menu(MenuType.IN_GAME_MAIN_MENU);
        int screenTop = Globals.WINDOW_HEIGHT - 150;
        int width = 500;
        int height = 500;
        int screenLeft = (Globals.WINDOW_WIDTH - width)/2;
        Window menuWindow = new Window(100, 100, 500, 500);
        
        //black texture background
        ImagePanel imagePanel = new ImagePanel(0,0,width,height);
        imagePanel.setWidth(width);
        imagePanel.setHeight(height);
        imagePanel.setTexture(Globals.assetManager.fetchTexture(Globals.blackTexture));
        menuWindow.addWidget(imagePanel);
        
        //label 1 (back)
        Label backLabel = new Label(100,50,1.0f);
        backLabel.setText("BACK");
        menuWindow.addWidget(backLabel);
        rVal.addOption(backLabel);
        
        
        //label 2 (quit)
        Label quitLabel = new Label(100,150,1.0f);
        quitLabel.setText("QUIT");
        menuWindow.addWidget(quitLabel);
        rVal.addOption(quitLabel);
        
        
        rVal.addElement(menuWindow);
        Globals.widgetManager.registerWidget(menuWindow);
//        rVal.addElement(WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop -  50, "BACK", true));
//        rVal.addOption( WidgetUtils.createVerticallyAlignedMinSizeTextBoxFromCharCount(40, 40, screenTop - 275, "QUIT", true));
        return rVal;
    }
    
    public static Menu createTestMainMenu(){
        Menu rVal = new Menu(MenuType.TEST);
        rVal.addElement(WidgetUtils.createWindowTEST());
        return rVal;
    }
    
    public static void makeMenuDrawable(Menu m){
        for(Widget w : m.widgetList){
            w.setVisible(true);
        }
    }
    
    public static void makeMenuUndrawable(Menu m){
        for(Widget w : m.widgetList){
            w.setVisible(false);
        }
    }
    
}
