//******************************************
//CAI.java
//Team E.T.:Eric Tedjamulia and Edward Tan:.
//12/30/03
//
//Demostrates blah blah blah
//******************************************

import java.awt.*; 
import java.applet.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

public class CAI extends Applet implements ActionListener, MouseListener
{ 
	
	private final int NUM_QUESTIONS = 10;
	private Font titleFont, smallFont, commentFont;
	private TextField usrInput;	
	private Label scoreLbl;
	private String equation, comment, scoreStr, name, choice, level;
	private String[] prblmsWrong;
	private Image img, end;
	private AudioClip correctSound, incorrectSound, applause;
	private int score, scoreTot, questionsAsked, numUsr, guess;
	private boolean quiz=true;
	
	private Calculate calc;
		
	public void init() 
	{ 
	    setLayout(null); 
	    setSize(400,350);//Size of screen 
	    setBackground (Color.yellow);

	    //integers
	    score = 10;
	    scoreTot=0;
	    questionsAsked = 0;
	    
	    //Strings
	    comment = "  Enter in Answer and hit <Enter>";
	    choice = "";
	    level = "";
	    
	    //String Array
	    prblmsWrong = new String[10];
	    for (int count=0; count<10; count++)
	    {
	    	prblmsWrong[count]="";
	    }
	    
	    //fonts
	    titleFont = new Font ("Arial", Font.BOLD, 28);
		smallFont = new Font ("Arial", Font.BOLD, 18);
		commentFont = new Font("Arial", Font.PLAIN, 14);
		
		//images
		img = getImage(getCodeBase(), "cai.GIF"); 
		
		//sounds
		correctSound = getAudioClip (getCodeBase(), "correct.wav");
		incorrectSound = getAudioClip (getCodeBase(), "incorrect.wav");
		applause = getAudioClip (getCodeBase(), "applause.wav");
		
		//User Input Box
		usrInput = new TextField("");
		usrInput.setBounds(240,273,60,26);
		usrInput.addActionListener(this);
		add(usrInput);
		
		//initialise score and add to layout 
		scoreStr = "Score: " + scoreTot + " / " + (questionsAsked*10); 
		scoreLbl = new Label(scoreStr); 
		scoreLbl.setBounds(20,360,140,28); 
		scoreLbl.setFont(smallFont); 
		add(scoreLbl);
		
		//Gets name, and choices
		name = JOptionPane.showInputDialog ("Enter in your Name: ");
		while (!(choice.equals("1") || choice.equals("2") || choice.equals("3")))
		{
			choice = JOptionPane.showInputDialog ("Choose One (1,2, or 3): \n1) Addition\n2) Subtraction\n3) Multiplication");
		}
		while (!(level.equals("1") || level.equals("2") || level.equals("3"))) 
		{
			level = JOptionPane.showInputDialog ("Choose One (1,2, or 3): \n1) Beginner\n2) Intermediate\n3) Expert"); 
		}
		calc = new Calculate(choice, level);
		equation = calc.getEq(); //gets new equation
		
		addMouseListener(this);		
	} 
	
	public void paint(Graphics g) 
	{ 
		if (quiz) //displays the "quiz"
		{
			// display title 
			g.setColor(Color.black); 
			g.drawRect (1, 1, 398, 398);
			g.setColor(Color.blue); 
			g.setFont(titleFont); 
			g.drawString("Computer Aided Instruction", 15, 40);
			g.drawImage(img, 100, 110, this); 
		  
			//Draws equation
			g.drawString(equation, 95, 295);
		  
			g.setFont(smallFont);
			g.drawString("By Team E.T.", 125, 70);
			g.setColor(Color.black);
			g.drawString("User: " + name, 345 - (name.length()*11), 380);
		  
			//Adds Comment
			g.setColor(Color.red);
			g.setFont(commentFont);
			g.drawString(comment, 90, 330);
			if (questionsAsked == NUM_QUESTIONS)
		  	g.drawString("QUIZ OVER: Click Here to Continue", 80, 350);
		}
		else // Displays Results (quiz==false)
		{
			remove(usrInput);
			remove(scoreLbl);
			g.setColor(Color.blue); 
			g.setFont(titleFont); 
			g.drawString("Results", 145, 40);
			
			//gets image based on score
			if (scoreTot >= 85)
				end = getImage(getCodeBase(), "happy.GIF");
			else
			if (scoreTot >= 70)
				end = getImage(getCodeBase(), "so-so.GIF");
			else
				end = getImage(getCodeBase(), "sad.GIF");
				
			//displays image
			g.drawImage(end, 125, 60, this);
			
			g.drawString("" + scoreTot + "% Correct", 120, 240);
			g.setColor(Color.white);
			g.fillRect (50, 250, 300, 140);
			g.setColor(Color.black);
			if (scoreTot != 100)
			{
				g.drawRect (50, 250, 300, 140);
				g.setFont(smallFont);
				g.drawString("Problems you need to work on", 65, 270);
				g.setFont(commentFont);
				int distY=270;
				for (int count=0; count<10; count++)
				{
					if (!(prblmsWrong[count].equals("")))
					{
						distY+=12;
						g.drawString(prblmsWrong[count], 65, distY);
					}
				}
			}
			else
				g.drawString("YOU DID AWESOME!", 60, 320);
						
		}
			
	}
	
	//preforms when answer is entered
	public void actionPerformed(ActionEvent event) 
	{ 
		String command = event.getActionCommand(); 
		numUsr = Integer.parseInt(command);
		
		//Runs for 10 questions
		if (questionsAsked<NUM_QUESTIONS)
		{
			if (calc.test(numUsr)) //tests for correct answer
			{
				comment = "      CORRECT! Good Job!!!";
				correctSound.play();
				questionsAsked++;
				scoreTot += score;
				scoreStr = "Score: " + scoreTot + " / " + (questionsAsked*10); 
				scoreLbl.setText(scoreStr);
				score = 10;
				if (questionsAsked!=NUM_QUESTIONS)
					equation = calc.getEq(); //gets new equation
			}
			else //User misses
			{
				score -= 4; 
				incorrectSound.play();
				if (score<=0) // if three guesses are gone moves on
				{
					comment = "Incorrect, moving to next Equation";
					questionsAsked++;
					scoreStr = "Score: " + scoreTot + " / " + (questionsAsked*10); 
					scoreLbl.setText(scoreStr);
					score = 10;
					if (questionsAsked!=NUM_QUESTIONS)
						equation = calc.getEq(); //gets new equation
				}
				else
				{	
					comment = "    Try Again, " + (score/4+1) + " more guesses";
					if (score == 6)
						prblmsWrong[questionsAsked] = equation;
				}
			}
		}
		
		usrInput.setText(""); //clears textfield
		repaint();	
	}
	
	public void mouseClicked(MouseEvent e)
	{
		if (questionsAsked>=NUM_QUESTIONS)
		{
			quiz = false;
			repaint();
		}
	}
	
	//Unused methods required by implementing MouseListener
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	
}