/**********************************
 * Assignment 5: Pacman
 * Date: 5/5/2006
 *
 * Jenna Holloway: jh382, 563324
 * Benji Shultz: bas67, 1786511
 **********************************/
package gui;

import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class HighScoreWindow extends JFrame{
 

private static final long serialVersionUID = -2559606705747491336L;
String[] scores= new String[4];
 final PlayerWindow pw=new PlayerWindow();
 public int score, index;
 File highScores=new File(System.getProperty("user.dir")+game.Constants.scoreDirectory+"/highscores.txt");
 JPanel jpan=new JPanel();
 JLabel[] labels=new JLabel[4];
 
 
 
 
 
 public HighScoreWindow(){
  jpan.setLayout(new FlowLayout(FlowLayout.CENTER));
  this.setLayout(new FlowLayout(FlowLayout.CENTER));
  scores= getEntries();
  setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  try{
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
  catch(Exception e){
   ;}
  setTitle("High Scores");
  int i=1;
  for(String score:scores){
   labels[i-1]=new JLabel("("+i+")"+score);
   jpan.add(labels[i-1]);
   i++;
  }
  jpan.setPreferredSize(new Dimension(150,100));
  //add(new ImagePane());
  add(jpan);
  pack();
  setResizable(false);
  setVisible(false);
  setAlwaysOnTop(true);
  setLocation(400,300);
 }
 
 public void setScore(int latest){
  //scores are sorted in ascending order
  //never more than ten, linear search is a ok
  score=latest;
  int[] highs=parseScores();
  int i=0; boolean high=true;
  do{
   high=(latest>highs[i]);
   i++;
   }
  while(i<highs.length&&!high);
  if(high||i<4){
   index=i-1;
   pw.setVisible(true);
  }
 }
 
 private int[] parseScores(){
  int highs[]=new int[4];
  try{
   if(highScores.exists()){
  
   String entry; int j=0;
   Scanner scanner=new Scanner(highScores);
   while (scanner.hasNextLine()){
   entry=scanner.nextLine(); 
   highs[j]=Integer.parseInt(entry.substring(entry.lastIndexOf(".")+1));
   j++;}}
  return highs;
  
  }
  catch(Exception e){
    return null;
  }}
 
 private String[] getEntries(){

  String[] entries=new String[4];
  try{if(highScores.exists()){
   Scanner scanner=new Scanner(highScores);
   int k=0;
   while (scanner.hasNextLine()&&k<entries.length){
    entries[k]=scanner.nextLine();
    k++;
   }
   
  }}
  catch (Exception e){
   ;}
  return entries;
 }
 
 void updateData(){
  jpan=new JPanel();
  scores= getEntries();
  int i=1;
  for (String score:scores){
   labels[i-1].setText("("+i+")"+score);
   i++;
  }
  repaint();
 }
 
 protected class PlayerWindow extends JFrame{
private static final long serialVersionUID = 3110039300315006287L;
final JTextField namegetter;
  public PlayerWindow(){
     namegetter=new JTextField(15);
  setLayout(new FlowLayout(FlowLayout.RIGHT));
  add(new JLabel("High Score!   Player Name: "));
  add(namegetter);
  JButton ok= new JButton("OK");
  ok.addActionListener(new AbstractAction(){
 private static final long serialVersionUID = 1L;

 public void actionPerformed(ActionEvent e){
      writeEntry(namegetter.getText());
      setVisible(false);      
      }
     });
  add(ok);
  setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  pack();
  setResizable(false);
  setLocation(200,500);
  setVisible(false);
  setAlwaysOnTop(true);
 }

 }
  private void writeEntry(String name){
   try{
   char[] entry= new char[20];
   int i=0;
   for(char c:name.toCharArray()){
    entry[i]=c;
    i++;}
   for(;i<20-((Integer)score).toString().toCharArray().length;i++){
    entry[i]='.';
   }
   for(char c:((Integer)score).toString().toCharArray()){
    entry[i]=c;
    i++;
   }
   String entr=new String(entry);
   insert(entr, index);
  
   File highScore=new File(System.getProperty("user.dir")+game.Constants.scoreDirectory+"/highscores.txt");
   FileOutputStream fos=new FileOutputStream(highScore);
   for(String score:scores){
    char[] chars=(score+"\n").toCharArray();
    byte[] bytes=new byte[chars.length];
    for (int k=0;k<chars.length;k++){
     bytes[k]=(byte)chars[k];
    }
    fos.write(bytes);
   }
   fos.close();
   updateData();
  }
   catch(Exception e){;}}
   
  private void insert(String entry, int in){
   int i=scores.length-1;
   while(i>in){
     scores[i]= scores[i-1];
    i--;}
   scores[in]= entry;
  
  }
  
}
