import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;


/**
 * @author DB_Team
 *
 * This class will contain the main functions, the listener and the db-connection
 */

public class DBGame implements ActionListener{

	// -------------------- Here you have to make changes and insert your own code -------------------
	

	/**
	 * Here the connection to the database is established
	 * Use the global variable db2Connection defined below
	 *
	 */
	private void connectToDB(){
		//TODO	
	}
	
	/**
	 * Make shure, the connection is closed
	 *
	 */
	private void exitGame(){
		// Disconnect from the data base
		// TODO
		// Close the gui
		// TODO
		// Exit
		System.exit(0);
	}
	
	/**
	 * Open a new Window or DialogBox and display the high score taken from the data base
	 *
	 */
	private void showHighScore(){
		// TODO
	}

	/**
	 * Write new high score to table if no highscore exists or 
	 * update if new highscore is better
	 *
	 */
	private void addHighscore(String userName, int score) {
	    // TODO
	}
	
	/**
	 * Open a new Window or DialogBox and ask for the users name
	 * Use the global variable userName definde below.
	 *
	 */
	private void askUserName(){
		//TODO  userName= ...
		userScore=0;
	}
	
	/**
	 * Start a new game session, get the user name and ask 20 questions, insert 
	 * result into database, if necessary.
	 *
	 */
	private void newGame(){
		// TODO
	}

	/**
	 * Called every time an anser was giveb and an other step is to be taken
	 *
	 */
	private void nextStep(){
		// TODO
	}

	/**
	 * Return a random question number that exists within the table
	 * 
	 * @return random number within 1 .. #questions in table
	 */
	private int getRandomQuestion(){
		// TODO
	   return 1; 
	}

	/**
	 * Fetch text for question with id from database
	 * 
	 * @param id for question
	 * @return
	 */
	private String fetchQuestion(int id) {
		// TODO
	    return "";
	}
	
	/**
	 * Remove the text from the two text fields, user methodes of DBGameGUI.
	 *
	 */
	private void clearInput(){
		// TODO
	}
	
	/**
	 * If a new Question is to be inserted, evaluate it's validity
	 * 
	 * @param newQuery Query, that produces the right answer(s)
	 */
	private boolean verifyQuestion(String newQuery){
		// TODO
	    return true;
	}

	/**
	 * Insert a new Question into database
	 * 
	 * @param newQuestion Question which is to be added to the system
	 * @param newQuery Query, that produces the right answer(s)
	 */
	private void insertQuestion(String newQuestion, String newQuery){
		// TODO
	}
	
	/**
	 * Take a given query, send it to the database and add the output to the gui's systemAnswerArea
	 * 
	 * @param query Query for the database
	 */
	private void sendQuery(String query){
		// TODO
	}
	
	/**
	 * Take the answer and look, if it is correct
	 * 
	 * @parameter answer The user's answer to a system question
	 * @parameter id The question id to check
	 *
	 */
	private boolean isCorrectAnswer(String answer, int id){
		// TODO
	    return false;
	}
	
	/**
	 * Check veracity of answer, update user score and 
	 * go to next question
	 * 
	 * @parameter answer The user's answer to a system question
	 *
	 */
	private void evaluateAnswer(String answer){
		// TODO
	}

	
	/**
	 * Send query to system and display results, update
	 * numbers of system queries asked
	 * 
	 * @parameter SQLquery the sql query for the system
	 *
	 */
	private void askSystem(String SQLquery){
		// TODO
	}
	
	
	// -------------------- The following you do not have to change ----------------------------------
	
	private DBGameGUI gui;
	private String userName;
	private int userScore;
	private int questionsAsked;
	private Connection db2Connection;
	private int stepCounter, questionID;


	/**
	 * Constructer, do not change :-)
	 *
	 */
	public DBGame(){
		//Connect to the database
		connectToDB();
		// Open the GUI
		gui=new DBGameGUI(this);
		gui.createAndShowGUI();
	}
	
	/**
	 * This is where the actions (like pressed buttom) are told to the class.
	 * You have to say, what you want to do, when a special action takes place.
	 * 
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Exit Game")) exitGame();
		else if(e.getActionCommand().equals("New Game")){
			gui.setGameModus();
			newGame();
		}
		else if(e.getActionCommand().equals("Insert Questions")){
			askUserName();
			gui.setInputModus();
		}
		else if(e.getActionCommand().equals("Highscore")){
			showHighScore();
		}
		else if(e.getActionCommand().equals("Clear")){
			clearInput();
		}
		else if(e.getActionCommand().equals("Answer")){
		    evaluateAnswer(gui.getAnswerFieldText());
		}
		else if(e.getActionCommand().equals("Query")){
			askSystem(gui.getQueryFieldText());
		}
		else if(e.getActionCommand().equals("Insert")){
			insertQuestion(gui.getAnswerFieldText(), gui.getQueryFieldText());
		}
	}

	
	/**
	 * Necessary to run the programm itself
	 * 
	 */
	public static void main(String[] args) {
	    DBGame game=new DBGame();
	}
}
