package pathgame.gameplay.menu;

import pathgame.gameplay.Gamestate;
import pathgame.gameplay.Gamestates;
import pathgame.gameplay.Keys;
import pathgame.gameplay.Level;

/**
 * A container for holding and manages all kinds of menus
 *
 * @author julia
 */
public class Menu
{
    private final static int MAIN_ID;
    private final static int ESCAPE_ID;
    private final static int OPTION_ID;
    private final static int CHARACTER_ID;
    private final static int AFTER_SCORE_ID;

    static
    {
        int id = 0;
        MAIN_ID = id++;
        ESCAPE_ID = id++;
        OPTION_ID = id++;
        CHARACTER_ID = id++;
        AFTER_SCORE_ID = id++;
    }

    private final BaseMenu[] menus = new BaseMenu[]
    {
        new MainMenu(MAIN_ID, OPTION_ID, CHARACTER_ID),
        new EscMenu(ESCAPE_ID, MAIN_ID),
        new OptionMenu(OPTION_ID, MAIN_ID),
        new CharacterMenu(CHARACTER_ID, MAIN_ID),
        new AfterScoreMenu(AFTER_SCORE_ID, MAIN_ID)
    };

    private int currentIndex = 0;

    /**
     * Updates the menu state every gametick based on user input
     *
     * @param gamestate the gamestate
     * @param level a level containing the current map and the current player
     */
    public void tick(Gamestate gamestate, Level level)
    {
        if(gamestate.getState() == Gamestates.MENU)
        {
            currentIndex = menus[currentIndex].tick(gamestate, level);
            if(currentIndex == ESCAPE_ID && Keys.ESCAPE_KEY.getTime() == 1)
            {
                gamestate.setState(Gamestates.GAMEPLAY);
            }
            else if((currentIndex == OPTION_ID || currentIndex == CHARACTER_ID)
                    && Keys.ESCAPE_KEY.getTime() == 1)
            {
                currentIndex = MAIN_ID;
            }
        }
        else if(gamestate.getState() == Gamestates.GAMEPLAY && Keys.ESCAPE_KEY.getTime() == 1)
        {
            currentIndex = ESCAPE_ID;
            gamestate.setState(Gamestates.MENU);
            menus[currentIndex].resetIndex();
        }
        else if(level.isShowingAfterScore())
        {
            currentIndex = AFTER_SCORE_ID;
            gamestate.setState(Gamestates.MENU);
            menus[currentIndex].resetIndex();
        }
    }

    /**
     * Returns the array of menu buttons of the menu
     *
     * @return the array of menu buttons of the menu
     */
    public MenuButton[] getOptions()
    {
        return menus[currentIndex].getOptions();
    }

    /**
     * Returns the index of the active menu button
     *
     * @return the index of the active menu button
     */
    public int getActiveIndex()
    {
        return menus[currentIndex].getActiveIndex();
    }

    /**
     * Returns if this is the OptionMenu
     *
     * @return if this is the OptionMenu
     */
    public boolean isOptionMenu()
    {
        return menus[currentIndex].isOptionMenu();
    }

    /**
     * Opens the escape menu
     *
     */
    public void showEscapeMenu()
    {
        currentIndex = ESCAPE_ID;
    }
}
