package electrosphere.menu;

import electrosphere.audio.AudioSource;
import electrosphere.audio.AudioUtils;
import electrosphere.main.Globals;
import electrosphere.renderer.ui.Widget;
import electrosphere.renderer.ui.font.TextBox;
import java.util.ArrayList;
import java.util.List;
import org.joml.Vector3f;

/**
 *
 * @author amaterasu
 */
public class Menu {
    
    public enum MenuType {
        TITLE_MENU,
        WORLD_SELECT_MENU, //select preexisting world (Char already created)
        WORLD_CREATE_MENU, //create world
        SAVE_CREATE_MENU, //select basic save settings
        CHARACTER_CREATE_MENU, //create character
        FINALIZE_SAVE_CREATION_MENU, //finalize the save before we commit
        MULTIPLAYER_MENU,
        IP_MENU,
        OPTIONS_MAIN_MENU,
        IN_GAME_MAIN_MENU,
        TEST,
    }
    
    MenuType type;
    
    int option = 0;
    int optionMax = 0;
    
    List<Widget> widgetList = new ArrayList();
    List<Widget> optionList = new ArrayList();
    
    public Menu(MenuType type){
        this.type = type;
    }
    
    public void addOption(Widget option){
        widgetList.add(option);
        optionList.add(option);
        optionMax++;
    }
    
    public void addElement(Widget element){
        widgetList.add(element);
    }
    
    public void dispose(){
        for(Widget w : widgetList){
            Globals.widgetManager.unregisterWidget(w);
        }
        widgetList.clear();
        optionList.clear();
    }
 
    public void incrementMenuOption(){
        setMenuOptionColor(option,new Vector3f(1.0f,1.0f,1.0f));
        option++;
        if(option>=optionMax){
            option = 0;
        }
        setMenuOptionColor(option,new Vector3f(0.2f,0.8f,0.5f));
        AudioSource startupSound = AudioUtils.playAudioAtLocation("/Audio/MenuChangeOption.ogg", new Vector3f(0,0,0));
    }
    
    public void decrementMenuOption(){
        setMenuOptionColor(option,new Vector3f(1.0f,1.0f,1.0f));
        option--;
        if(option < 0){
            option = optionMax - 1;
        }
        setMenuOptionColor(option,new Vector3f(0.2f,0.8f,0.5f));
        AudioSource startupSound = AudioUtils.playAudioAtLocation("/Audio/MenuChangeOption.ogg", new Vector3f(0,0,0));
    }
    
    public Widget getCurrentOption(){
        return optionList.get(option);
    }
    
    public MenuType getType(){
        return type;
    }
    
    public void setMenuOptionColor(int option, Vector3f color){
        if(optionList.get(option) instanceof TextBox){
            ((TextBox)optionList.get(option)).setColor(color);
        }
    }
    
    public List<Widget> getOptions(){
        return optionList;
    }
    
    public List<Widget> getWidgets(){
        return widgetList;
    }
}
