package de.siphalor.was.visual.jfx.controller;

import de.siphalor.was.WhatAStorage;
import de.siphalor.was.content.lang.I18n;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;

import java.util.stream.Collectors;

public class MainMenuController {
	public ListView<String> questSelection;
	public Button optionsButton;

	@FXML
	private void  initialize() {
		I18n i18n = I18n.getInstance();
		questSelection.setCellFactory(param -> new ListCell<>(){
			@Override
			protected void updateItem(String item, boolean empty) {
				super.updateItem(item, empty);
				if (empty) {
					setText(null);
				} else {
					setText(i18n.getString("quests." + item) + " (" + i18n.getString("quests." + item + ".description") + ")");
				}
			}
		});

		ObservableList<String> items = questSelection.getItems();
		items.clear();
		items.addAll(WhatAStorage.getInstance().getQuestManager().ids().stream().sorted().collect(Collectors.toList()));
		questSelection.getSelectionModel().selectIndices(0);
		questSelection.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
	}

	@FXML
	private void startGame() {
		String selectedItem = questSelection.getSelectionModel().getSelectedItem();
		WhatAStorage.getInstance().startGame(selectedItem);
	}
}
