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;

/**
 * A container for holding all options of the OptionMenu
 *
 * @author julia
 */
public class OptionMenu extends BaseMenu
{
    private final MenuButton[] options;

    /**
     * Contructor generating and initializing the OptionMenu
     *
     * @param id the id of the OptionMenu
     * @param mainId the id to the MainMenu
     */
    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);
        });
    }

    /**
     * Returns the options of the OptionMenu
     *
     * @return the options of the OptionMenu
     */
    @Override
    public MenuButton[] getOptions()
    {
        return options;
    }

    /**
     * Returns if this is the OptionMenu
     *
     * @return if this is the OptionMenu
     */
    @Override
    public boolean isOptionMenu()
    {
        return true;
    }
}
