package pathgame.gameplay.menu;

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

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;

    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.getShowAfterScore())
        {
            currentIndex = AFTER_SCORE_ID;
            gamestate.setState(Gamestates.MENU);
            menus[currentIndex].resetIndex();
        }
    }

    public MenuButton[] getOptions()
    {
        return menus[currentIndex].getOptions();
    }

    public int getActiveIndex()
    {
        return menus[currentIndex].getActiveIndex();
    }

    public boolean isOptionMenu()
    {
        return menus[currentIndex].isOptionMenu();
    }

    public void showEscapeMenu()
    {
        currentIndex = ESCAPE_ID;
    }
}
