/*
	lookup applet reads a text file
	with grade data and reports grade info
	based on the login and password
	typed.
*/

import java.awt.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;
import java.lang.Math;
import java.util.*;
import java.io.*;
import java.io.StreamTokenizer;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;

public class lookup extends Applet
{
	//global variables
	int		MAX=35; // maximum number of students per course
	Choice 	daColor = new Choice();
	int		n=1;
	int		classSize;
	String	courseName[];
	String	courseFile[];
	String	studentName[];
	String	login[];
	String	password[];
	String	studentReport[];
	String	message;
	TextField loginField = new TextField (12);
	TextField passwordField = new TextField (12);
	Button	getItButton = new Button("Look Up Grade");
	TextArea	results = new TextArea("Select Class, and login to get your grade report",20,50);
	
		
	public void init() {
		String rs;
		// get params
		// see how many courses to setup (n)
		rs = getParameter("n");
		if (rs == null) {
	    	n = 1;
		} else {
	    	n = Integer.parseInt(rs);
		}
		// set up ararys for the different course
		courseName = new String[n];
		courseFile = new String[n];
		studentName = new String[MAX];
		login = new String[MAX];
		password = new String[MAX];
		studentReport = new String [MAX];
		
		passwordField.setEchoCharacter('*');
		
		//read params for each course
		for (int i=0; i < n; i++) {
			courseName[i] = getParameter("ClassName"+(i+1));
			courseFile[i] = getParameter("ClassFile"+(i+1));
			daColor.addItem(courseName[i]); // add pop-up menu item
		}
		
		add(daColor); //complete the pop-up menu
		add(loginField);
		add(passwordField);
		add(getItButton);
		add(results);
		getReports (courseFile[0]);

        setBackground(new Color(0xF0F080));
        message="1.  Select your class, \r2.  Type in your login (initials) in the first box\r3.  Type your password in the next box\r4.  Press button to get your grade report";
		repaint();
	}
	
	public void paint( Graphics g ) {
		/* This would draw the datafile for daignostic purposes
		g.drawString(courseFile[daColor.getSelectedIndex()], 130, 45);
		g.drawString( "login:  "+loginField.getText(), 10, 45 );
	
		for (int i = 0; i<classSize; i++ ) {
			g.drawString(  studentName[i]  , 5, 60+i*12);
			g.drawString(  login [i]  , 150, 60+i*12);
			g.drawString(  password[i], 200, 60+i*12);
			g.drawString(  studentReport[i], 250, 60+i*12);
		}
		*/
		g.setColor(Color.blue);
		//g.drawString( "login:", 150, 15 );
		//g.drawString( "password:", 250, 15 );
		results.setText(message);
	}
	public void getReports (String dataFileName) {
	
		 try {
            BufferedReader dis = new BufferedReader( new InputStreamReader(new URL(getDocumentBase(), dataFileName).openStream()));
            int i=0;
            try {
            	String s = dis.readLine();
               while (s != null) {
              // while ( i < 10 ) {
                	if ( s.indexOf(":")>0){ // check for an empty line
                		studentName[i]=s.substring(0,s.indexOf(":")); 
                		s = s.substring(s.indexOf(":")+1); //clip
                		
                		if (s.indexOf(":")>0) {
                			login[i]=s.substring(0,s.indexOf(":")); 
                			s = s.substring(s.indexOf(":")+1); //clip
                		}
                		if (s.indexOf(":")>0){
                			password[i] =s.substring(0,s.indexOf(":")); 
                			studentReport[i]= s.substring(s.indexOf(":")+1); //clip
                		}
	  					i++;
      	 			}
      	 			s = dis.readLine();		
                }
            } catch (EOFException err) { message = "Ooops "+err.toString();
            }
            dis.close();
            classSize=i;
        } catch (FileNotFoundException err) {
            message = "Ooops "+err.toString();
        } catch (IOException err) {
            message = "Ooops "+err.toString();
        }
		
	
	}
	public String convertFromHTML( String S) {
		while (S.indexOf("<BR>") >=0 ) {
			S = S.substring(0,S.indexOf("<BR>")) + "\r" +
				S.substring(S.indexOf("<BR>")+4);
		}
		return S;
	}
	public boolean action(Event e, Object arg) {
        if (e.target instanceof Choice) {
        // read in the data of the course selected
        	String dataFileName = courseFile[daColor.getSelectedIndex()];
        	getReports( dataFileName );
            repaint();
        	return true;
        } if (e.target instanceof Button) { 
        	//get user login info
        	String l=loginField.getText();
        	String pw=passwordField.getText();
        	message="User login not found.  Check course name and spelling";
        	int i=0;
       		while ( (i<classSize)&&(!l.equals(login[i]))) i++;
       		if (  l.equals(login[i]) ){
        		message=studentName[i]+" found, password not valid.  \rCheck for CapsLock, spelling\ror extra spaces in the box";
        		if (pw.equals(password[i])){
        			message="Grade report for "+studentName[i]+"\r"+studentReport[i];
        			message=convertFromHTML(message);
        			results.setText(message);
        		}
        	}
        	
        	repaint();
        	return true;
        }else { return false;}
               
    }
    
    public boolean handleEvent(Event e) {
                  return super.handleEvent(e);
    }
     
	 /*
     * Mouse methods
     */
    public boolean mouseDown(java.awt.Event evt, int x, int y) {
      

        repaint();
        return true;
    }
	
  	
  	
    public boolean mouseMove(java.awt.Event evt, int x, int y) {
        
        return true;
    }

    public void mouseEnter() {
        repaint();
    }

    public void mouseExit() {
       
        repaint();
    }

    /**
     * Focus methods
     */
    public void keyDown(int key) {
    }
}//end of applet