package pathgame.gameplay.menu;

import java.io.File;
import java.io.FileOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import me.hammerle.snuviengine.api.KeyBinding;
import me.hammerle.snuviengine.api.KeyHandler;
import pathgame.gameplay.Keys;

public class OptionMenu extends BaseMenu
{

    private final MenuButton[] options;

    public OptionMenu(int id, int mainId)
    {
        super(id);
        int menuLength = Keys.KEYS.length;
        options = new MenuButton[menuLength + 1];
        for(int i = 0; i < menuLength; ++i)
        {
            options[i] = getButton(Keys.KEYNAMES[i], Keys.KEYS[i]);
        }
        options[menuLength] = new MenuButton("Back to Main Menu", (gamestate) ->
        {
            File f = new File("resources/config.bin");
            try(DataOutputStream writer = new DataOutputStream(new FileOutputStream(f)))
            {
                for(int i = 0; i < Keys.KEYS.length; ++i)
                {
                    writer.writeInt(Keys.KEYS[i].getKey());
                }
            }
            catch(IOException ex)
            {
            }
            setReturnId(mainId);
        });
    }

    private MenuButton getButton(String name, KeyBinding key)
    {
        return new MenuButton(name, (gamestate) ->
        {
            KeyHandler.rebind(key);
        });
    }

    @Override
    public MenuButton[] getOptions()
    {
        return options;
    }

    @Override
    public boolean isOptionMenu()
    {
        return true;
    }
}
