
package edu.hws.GA;

import java.awt.*;
import java.awt.event.*;

class ReportWin extends Frame {

   private int nextAvg = 100;
   private double[] avgList = new double[101];
   private double best;
   
   private String[] stats = new String[500];
   private int statsCt;
   
   private TextArea list;
   
   private static String blanks = "                    ";

   ReportWin() {
       super("Statistics");
       list = new TextArea("",20,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
       add(list, BorderLayout.CENTER);
       Font f = new Font("Monospaced", Font.PLAIN, 12);
       Label lb = new Label("*  YEAR   AVERAGE SCORE   HIGH SCORE   100-YEAR AVERAGE  *");
       lb.setFont(f);
       add(lb,BorderLayout.NORTH);
       list.setFont(f);
       list.setEditable(false);
       list.setBackground(Color.white);
       setBackground(Color.lightGray);
       pack();
       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
       setLocation(screenSize.width - 10 - getSize().width, 25);
       show();
   }
   
  void doReport(int year, double avgScore, int highScore) {
      boolean isBest = false;
      if (avgScore > best) {
         isBest = true;
         best = avgScore;
      }
      String cs = "";
      if (year < 100)
         avgList[year] = avgScore;
      else {
         avgList[nextAvg] = avgScore;
         nextAvg++;
         if (nextAvg > 100)
            nextAvg = 1;
         double d = 0;
         for (int i = 1; i <= 100; i++)
            d += avgList[i];
         cs = "" + ( ((int)(d*100))/10000.0 );
      }
      if (isBest || year <= 100 || (year <= 1000 && year % 10 == 0) || year % 100 == 0) {
         String ys = "" + year;
         String ds = "" + ( ((int)(avgScore*1000))/1000.0 );
         String hs = "" + highScore;
         ys = blanks.substring( 0, 6 - ys.length() ) + ys;
         ds = blanks.substring( 0, 14 - ds.length() ) + ds;
         if (isBest)
            hs = " *" + blanks.substring( 0, 11 - hs.length() ) + hs;
         else
            hs = blanks.substring(0, 13 - hs.length() ) + hs;
         String s = ys + ds + hs + "            " + cs;
         list.append(s+"\n");
      }
   }
   
   void clear() {
      list.setText("");
      nextAvg = 100;
      best = 0;
   }
      
}

