package com.rowery;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

import org.drools.runtime.StatefulKnowledgeSession;
import org.yaml.snakeyaml.Yaml;

public class Engine {
	private String name;
	private String fact;
	private String text;
	private String type;
	private HashMap<String, String> answers;
	private static StatefulKnowledgeSession ks;
	private static Map<String, Map<String, Object>> map;
	private static ArrayList<String> ANSWERS = new ArrayList<String>();

	@SuppressWarnings("unchecked")
	public Engine(String filename,StatefulKnowledgeSession ksession){
		ks = ksession;
		Yaml yaml = new Yaml();
		 try {
		 map = (Map<String, Map<String, Object>>)yaml.load((InputStream)(new FileInputStream(new File(filename))));

		  } catch (FileNotFoundException e) {
	          e.printStackTrace();
	        }
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFact() {
		return fact;
	}

	public void setFact(String fact) {
		this.fact = fact;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public HashMap<String, String> getAnswers() {
		return answers;
	}

	public void setAnswers(HashMap<String, String> answers) {
		this.answers = answers;
	}

	@SuppressWarnings("unchecked")
	public static void Question(String name){

		Map<String, Object> entry = map.get(name);
		ShowQuestion( (String)(entry.get("fact_name")),
				(String)(entry.get("type")),
				(String)(entry.get("text")),
				(HashMap<Integer, String>)(entry.get("answers")));

	}

	@SuppressWarnings("unchecked")
	private static void ShowQuestion(String fact, String type, String text,HashMap<Integer, String> ans) {

		ArrayList<String> answer = new ArrayList<String>();
		ArrayList<Integer> labels = new ArrayList<Integer>();

		for (Integer key : ans.keySet()) {
			answer.add(ans.get(key));

			labels.add(key);
		}

		@SuppressWarnings("rawtypes")
		JList list = new JList(answer.toArray());
		 if(type.equals("single"))
			 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		 Object[] options = {text, " ", list};
		 JOptionPane.showMessageDialog(null, options, "Zaznacz", JOptionPane.PLAIN_MESSAGE);

		while (list.getSelectedIndices().length < 1 && type.equals("single")) {
			options = new Object[] {text, " ", "Zaznacz odpowiedź!", " ", list };
			JOptionPane.showMessageDialog(null, options, "Zaznacz", JOptionPane.PLAIN_MESSAGE);
		}

	    for(Integer x : list.getSelectedIndices()){
	    	ks.insert(new Fact(fact, ans.get(x)));

	    }

	}

	@SuppressWarnings("unchecked")
	public void ShowAnswer() {
		Map<String, Object> entry = map.get("answer");
		String text = (String)(entry.get("text"));

		@SuppressWarnings("rawtypes")
		JList list = new JList(ANSWERS.toArray());
		list.setEnabled(false);
		Object[] options = {text, " ", list};
		JOptionPane.showMessageDialog(null, options, "Odpowiedź", JOptionPane.PLAIN_MESSAGE);
	}

	public static void Answer(String name){
		ANSWERS.add(name);
	}

}
@@ -13,16 +13,89 @@
 rowery;%0A
+import java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0A
 import j
@@ -282,16 +282,44 @@
 l.Map;%0A%0A
+import javax.swing.JButton;%0A
 import j
@@ -814,16 +814,52 @@
 ng%3E();%0A%0A
+%09static boolean terminate = false;%0A%0A
 %09@Suppre
@@ -2588,16 +2588,274 @@
 TION);%0A%0A
+%09%09 JButton button = new JButton();%0A%09%09 button.setText(%22Zamknij program%22);%0A%09%09 button.addActionListener(new ActionListener() %7B%0A%0A%09            public void actionPerformed(ActionEvent e)%0A%09            %7B%0A%09                System.exit(0);%0A%09            %7D%0A%09        %7D);%0A%0A
 %09%09 Objec
@@ -2880,24 +2880,32 @@
 t, %22 %22, list
+, button
 %7D;%0A%09%09 JOptio
@@ -3126,16 +3126,24 @@
  %22, list
+, button
  %7D;%0A%09%09%09J
@@ -3397,16 +3397,37 @@
 wer() %7B%0A
+%09%09if (!terminate) %7B%0A%09
 %09%09Map%3CSt
@@ -3467,16 +3467,17 @@
 swer%22);%0A
+%09
 %09%09String
@@ -3512,24 +3512,25 @@
 text%22));%0A%0A%09%09
+%09
 @SuppressWar
@@ -3539,32 +3539,33 @@
 ngs(%22rawtypes%22)%0A
+%09
 %09%09JList list = n
@@ -3595,16 +3595,17 @@
 y());%0A%09%09
+%09
 list.set
@@ -3620,16 +3620,17 @@
 false);%0A
+%09
 %09%09Object
@@ -3661,16 +3661,17 @@
  list%7D;%0A
+%09
 %09%09JOptio
@@ -3750,16 +3750,20 @@
 SSAGE);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09pub

quit app button