import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.Formatter;
import java.util.Scanner;


///////////////////
// Class MyCanvas
///////////////////
class MyCanvas extends Canvas implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, Runnable
{
	// Size of current client region
	private int currentWidth=0;
	private int currentHeight=0;
	
	// Calender data
	private String strDate=null, strTime=null;
	
	// Color & Title settings of different modes
	private Color bgColor, fontColor;
	private String headTitle;
	private int mode=0;
	
	// File management
	private PrintWriter onTime, comeLate, getPoint;
	
	// Storage for key input
	private StringBuffer keyInput;
	
	// Storage for students information
	private String[][] StuPrn;
	private String stuName;
	
	// Flags
	private boolean dispKeyInput=false;
	private boolean stopKeyInput=false;


	////////////////////////////
	// Constructing a MyCanvas
	////////////////////////////
	public MyCanvas() throws IOException{
		super();
		
		System.out.println("[MyCanvas] Registering event handlers..");
		addMouseListener(this);
		addMouseMotionListener(this);
		addKeyListener(this);
		addComponentListener(this);
		
		System.out.println("[MyCanvas] Setting properties of the canvas..");
		setBackground(new Color(0.0f, 0.0f, 0.0f));
		
		System.out.println("[MyCanvas] Loading student list..");
		System.out.println("[MyCanvas] ..Input the file listing students");
		System.out.print("[MyCanvas] ..>");
		Scanner file=new Scanner(System.in);
		Scanner scnr=new Scanner(new File(file.next()));
		int len=scnr.nextInt();
		StuPrn = new String[len][3];
		for(int i=0;i<StuPrn.length;i++){
			StuPrn[i][0] = scnr.next(); // Registered Name
			StuPrn[i][1] = scnr.next(); // Family Name
			StuPrn[i][2] = scnr.next(); // Given Name
			System.out.println("[MyCanvas] .."+StuPrn[i][0]+" "+StuPrn[i][1]+" "+StuPrn[i][2]);
		}
		System.out.println("[MyCanvas] .."+len+" students");
		scnr.close();
		
		System.out.println("[MyCanvas] Processing objects..");
		Thread th = new Thread(this);
		th.start();
		keyInput = new StringBuffer();
		keyInput.delete(0,keyInput.length());
		onTime = new PrintWriter(new BufferedWriter(new FileWriter(new File("ontime.txt"),true)));
		comeLate = new PrintWriter(new BufferedWriter(new FileWriter(new File("comelate.txt"),true)));
		getPoint = new PrintWriter(new BufferedWriter(new FileWriter(new File("getpoint.txt"),true)));
		
		System.out.println("[MyCanvas] Successfully completed!!");
	}


	////////////////////////////
	// Destructing the MyCanvas
	////////////////////////////
	public void finalize(){
		onTime.close();
		comeLate.close();
		getPoint.close();
	}


	/////////////////////////
	// Implimenting methods
	/////////////////////////
	public void paint(Graphics g){
		Calendar clndr=Calendar.getInstance();
		Formatter fmt;

		// Settings of Color ant Title
		switch(mode){
			case 0:	bgColor=new Color(0.0f, 0.0f, 0.2f);
					fontColor=new Color(0.5f, 0.5f, 1.0f);
					headTitle="oÈ“ü—Íƒ‚[ƒh’†...   S(œLÍMœ)Good Morning!! ";
					break;
			case 1:	bgColor=new Color(0.2f, 0.0f, 0.0f);
					fontColor=new Color(1.0f, 0.5f, 0.5f);
					headTitle="’x“ü—Íƒ‚[ƒh’†...   iƒm„tM—j±²À° ";
					break;
			case 2:	bgColor=new Color(0.0f, 0.2f, 0.0f);
					fontColor=new Color(0.5f, 1.0f, 0.5f);
					headTitle="ƒvƒ‰ƒXƒ¿ƒ|ƒCƒ“ƒg“ü—Íƒ‚[ƒh’†...   (=P¤P=)‚u ‚â‚Á‚½‚Ë";
					break;
		}
		setBackground(bgColor);
		g.setColor(fontColor);
		
		// Drawing messages and calender
		g.setFont(new Font("HGŠÛºÞ¼¯¸M-PRO",Font.BOLD,10));
		g.drawString("W:"+currentWidth+",H:"+currentHeight,10,currentHeight-1);
		
		g.setFont(new Font("HGŠÛºÞ¼¯¸M-PRO",Font.BOLD,20));
		g.drawString("šoÈŠÇ—ƒVƒXƒeƒ€š",10,50);
		
		g.setFont(new Font("HGŠÛºÞ¼¯¸M-PRO",Font.BOLD,40));
		g.drawString(headTitle,10,100);
		
		fmt = new Formatter();
		fmt.format("%04d/%02d/%02d",clndr.get(Calendar.YEAR),clndr.get(Calendar.MONTH),clndr.get(Calendar.DATE));
		strDate = fmt.toString();
		
		fmt = new Formatter();
		fmt.format("%02d:",clndr.get(Calendar.HOUR_OF_DAY));
		fmt.format("%02d:",clndr.get(Calendar.MINUTE));
		fmt.format("%02d",clndr.get(Calendar.SECOND));
		strTime = fmt.toString();
		
		g.setFont(new Font("HGŠÛºÞ¼¯¸M-PRO",Font.BOLD,20));
		g.drawString(strDate+"  "+strTime,10,130);
		
		// Drawing the current studnet information
		if(dispKeyInput==true){
			g.setFont(new Font("HGŠÛºÞ¼¯¸M-PRO",Font.BOLD,150));
			g.drawString(keyInput.toString(),50,currentHeight/2);
			g.drawString(stuName,50,currentHeight/2+150);
			stopKeyInput=false;
		}
	}
	
	public void run(){
		long preTimeMillis=0;
		long crntTimeMillis;
		Calendar clndr;
		while(true){
			// Update of the second
			clndr = Calendar.getInstance();
			crntTimeMillis = clndr.getTimeInMillis();
			if(crntTimeMillis - preTimeMillis >= 1000){
				repaint();
				preTimeMillis = crntTimeMillis;
				
				requestFocus();
			}
			
			// Focus of the mouse onto this client region
			try{
				Thread.sleep(100);
			}catch(Exception e){}
		}
	}


	////////////////////////////////////////
	// Setting Event-handlers for the Canvas
	////////////////////////////////////////
	/// For Canvas ///
	public void componentHidden(ComponentEvent e){
	}
	public void componentMoved(ComponentEvent e){
	}
	public void componentResized(ComponentEvent e){
		currentWidth = e.getComponent().getSize().width;
		currentHeight = e.getComponent().getSize().height;
	}
	public void componentShown(ComponentEvent e){
	}

	/// For Mouse Events ///
	public void mouseEntered(MouseEvent e){	
	}
	public void mouseExited(MouseEvent e){
	}
	public void mousePressed(MouseEvent e){
		// Chagne between the modes
		mode++;
		if(mode>2)mode=0;
		
		// Stop drawing the current student information
		dispKeyInput=false;
		keyInput.delete(0,keyInput.length());
		repaint();
	}
	public void mouseReleased(MouseEvent e){
	}
	public void mouseClicked(MouseEvent e){
	}

	/// For Mouse Motion Events ///
    public void mouseDragged(MouseEvent e){
	}
	public void mouseMoved(MouseEvent e){
	}

	/// For Key Events ///
	public void keyPressed(KeyEvent e){
	}
	public void keyReleased(KeyEvent e){
	}
	public void keyTyped(KeyEvent e){
		char key = e.getKeyChar();

		if((key>='0' && key <='9') || (key>='A' && key<='Z') || (key>='a' && key<='z') || key==10){
			if(stopKeyInput==false){
				// Stop drawing the current student information
				if(dispKeyInput==true){
					dispKeyInput=false;
					keyInput.delete(0,keyInput.length());
					repaint();
				}
				if(key!=10){
				// Storing the current letter onto the storage for key input
					keyInput.append(key);
				}else if(keyInput.length()<3){
				// Erasing the storage if the length of the string terminated by Enter is less than 3
					keyInput.delete(0,keyInput.length());
				}else{
				// Saving the string terminated by Enter onto the corresponding file
					keyInput.delete(keyInput.length()-2,keyInput.length());
					stopKeyInput=true;
					
					// Searching the student name
					stuName = "–¢“o˜^";
					for(int i=0;i<StuPrn.length;i++){
						if(StuPrn[i][0].compareTo(keyInput.toString()) == 0){
							stuName = StuPrn[i][1]+" "+StuPrn[i][2];
							break;
						}
					}
					stuName += "san";
					
					// Saving the string
					if(mode==0){
						File onTimeFile = new File("ontime", keyInput.toString()+".flg");
						if(onTimeFile.exists()){
							keyInput.append("(Ï)");
						}else{
							try{
								onTimeFile.createNewFile();
							}catch(IOException ie){}
							onTime.println(keyInput.toString()+", "+strDate+", 0, "+strTime);
							onTime.flush();
						}
					}
					if(mode==1){
						File comeLateFile = new File("comelate", keyInput.toString()+".flg");
						if(comeLateFile.exists()){
							keyInput.append("(Ï)");
						}else{
							try{
								comeLateFile.createNewFile();
							}catch(IOException ie){}
							comeLate.println(keyInput.toString()+", "+strDate+", 2, "+strTime);
							comeLate.flush();
						}
					}
					if(mode==2){
						getPoint.println(keyInput.toString()+", "+strDate+", "+strTime);
						getPoint.flush();
					}
					
					// Drawing the current student information
					dispKeyInput=true;
					repaint();
				}
			}//if(stopKeyInput==false)
		}//if((key>='0' && key <='9') || (key>='A' && key<='Z') || (key>='a' && key<='z') || key==10)
	}
}
