package pathgame.gameplay.menu;

import me.hammerle.snuviengine.api.KeyBinding;
import pathgame.gameplay.Gamestate;
import pathgame.gameplay.Keys;
import pathgame.gameplay.Level;

public abstract class BaseMenu
{
    private final int id;
    private int index = 0;
    public abstract MenuButton[] getOptions();
    private int returnId;
    
    public BaseMenu(int id)
    {
        this.id = id;
    }
    
    public int getActiveIndex()
    {
        return index;
    }
    
    public boolean isOptionMenu()
    {
        return false;
    }
    
    public int tick(Gamestate gamestate, Level level)
    {
        returnId = id;
        if(isKeyPressed(Keys.UP_KEY) || isKeyPressed(Keys.CAM_UP_KEY))
        {
            int length = getOptions().length;
            index = ((index - 1) + length) % length;
        }
        else if(isKeyPressed(Keys.DOWN_KEY) || isKeyPressed(Keys.CAM_DOWN_KEY))
        {
            index = (index + 1) % getOptions().length;
        }
        else if(isKeyPressed(Keys.CONFIRM_KEY))
        {
            getOptions()[index].run(gamestate, level);
        }
        if(returnId != id)
        {
            index = 0;
        }
        return returnId;
    }
    
    public void setReturnId(int id)
    {
        returnId = id;
    }
            
    public void resetIndex()
    {
        index = 0;
    }
    
    private boolean isKeyPressed(KeyBinding key)
    {
        return key.getTime() == 1 || (key.getTime() >= 20 && key.getTime() % 5 == 1);
    }
    
    
}