// Johann Shudlick
// April 29, 1999
// Final Project
// Create an appicaiton for tutoring and testing elemtentary school kids

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class testFrame extends Frame implements ActionListener
{
   private String stateName[] = { "GeoWhiz", "Arizona", "California", "Colorado", "Idaho", "Kansas", "Montana", "Nebraska", "Nevada", "New Mexico", "North Dakota", "Oklahoma", "Oregon", "South Dakota", "Texas", "Utah", "Washington", "Wyoming" };
   private String capitolName[] = { "", "Phoenix", "Sacramento", "Denver", "Boise", "Topeka", "Helena", "Lincoln", "Carson City", "Santa Fe", "Bismark", "Oklahoma City", "Salem", "Pierre", "Austin", "Salt Lake City", "Olympia", "Cheyenne" };
   private String userStates[], userCapitols[];
   private Label num[], name, instr1, instr2, instr3, st1, st2, st3, ca1, ca2, ca3;
   private TextField state[], capitol[], nameInput;
   private GeoWhiz gw;
   private int stateX = 30, stateY = 245, capX = 120, capY = 245, index = 1, correct = 0, incorrect = 0;
   private Button forward, back, done, review, study;
   private MyCanvas canny;
   private resultsFrame score;
   private reviewFrame r;
   private Font f;
   public boolean sta[], cap[];

   testFrame( GeoWhiz parent )
   {
      super( "Test Yourself!!!" );
      setSize( 640, 435 );
      gw = parent;
      addWindowListener( new WindowHide(this) );
      setLayout( null );

      f = new Font( "Arial Black", Font.BOLD, 32);

      canny = new MyCanvas( this );
      add( canny);
      canny.setLocation( 3, 23 );

      forward = new Button ( "Next State?" );
      add( forward );
      forward.addActionListener( this );
      forward.setBounds( 225, 80, 90, 30 );

      back = new Button ( "Last State?" );
      add( back );
      back.addActionListener( this );
      back.setBounds( 325, 80, 90, 30 );

      done = new Button ( "Grade Now!" );
      add( done );
      done.addActionListener( this );
      done.setBounds( 275, 120, 90, 30 );

      study = new Button ( "Study Again!" );
      add( study );
      study.addActionListener( this );
      study.setBounds( 375, 120, 90, 30 );

      review = new Button ( "Review More!" );
      add( review );
      review.addActionListener( this );
      review.setBounds( 475, 120, 90, 30 );

      name = new Label( "Enter Your Name:" );
      add( name );
      name.setBounds( 450, 70, 120, 18 );

      nameInput = new TextField( "Unknown User" );
      add( nameInput );
      nameInput.setBounds( 460, 90, 140, 20 ); 
      
      instr1 = new Label( "INSTRUCTIONS: Click through the states using the buttons above." );
      add( instr1 );
      instr1.setBounds( 225, 160, 400, 20 );

      instr2 = new Label( "Enter the name and capital of each state in the appropriate box below." );
      add( instr2 );
      instr2.setBounds( 235, 175, 400, 20 );

      instr3 = new Label( "Click the 'Grade Now!' button when finished." );
      add( instr3 );
      instr3.setBounds( 235, 190, 400, 20 );

      num = new Label[ 18 ];

      state = new TextField[ 18 ];
      userStates = new String[ 18 ];
      userCapitols = new String[ 18 ];

      sta = new boolean[ 18 ];
      cap = new boolean[ 18 ];

      st1 = new Label( "State" );
      add( st1);
      st1.setBounds( 59, 222, 50, 20);
      st2 = new Label( "State" );
      add( st2);
      st2.setBounds( 269, 222, 50, 20);
      st3 = new Label( "State" );
      add( st3);
      st3.setBounds( 479, 222, 50, 20);
      ca1 = new Label( "Capital" );
      add( ca1);
      ca1.setBounds( 139, 222, 50, 20);
      ca2 = new Label( "Capital" );
      add( ca2);
      ca2.setBounds( 349, 222, 50, 20);
      ca3 = new Label( "Capital" );
      add( ca3);
      ca3.setBounds( 559, 222, 50, 20);

      for( int i = 1; i < 18; i++ )
      {
         state[ i ] =  new TextField( 15 );
         add( state[ i ] );
         state[ i ].setBounds( stateX, stateY, 80, 20);

         num[i] = new Label( "#" + i );
         add( num[ i ] );
         num[ i ].setBounds( stateX - 22, stateY, 80, 20 );

         stateY = stateY + 25;
         if( i == 6)
         {
            stateX = 240;
            stateY = 245;
         }
         if( i ==  12 )
         {
            stateX = 450;
            stateY = 245;
         }
      }

      capitol = new TextField[ 18 ];
      for( int i = 1; i < 18; i++ )
      {
         capitol[ i ] =  new TextField( 15 );
         add( capitol[ i ] );
         capitol[ i ].setBounds( capX, capY, 80, 20);
         capY = capY + 25;
         if( i == 6 )
         {
            capX = 330;
            capY = 245;
         }
         if( i == 12 )
         {
            capX = 540;
            capY = 245;
         }
      }
   }

   public void paint(Graphics g )
   {
         g.setFont( f );
         g.drawString( "This is state number " + index, 225, 60 );
   }
   public int getIndex() {return index;}

   public Image loadPics( int a )
   {
      return gw.getImage( gw.getDocumentBase(), "state" + a + ".jpg" );
   }

   public void actionPerformed( ActionEvent e )
   {

      if( e.getSource() == review )
      {
         r = new reviewFrame( gw );
         r.setVisible( true );
         gw.getTestFrame().setVisible(false);
      }
      if( e.getSource() == study )
      {
         gw.getTestFrame().setVisible(false);
         gw.getStudyFrame().setVisible(true);
      }
      if( e.getSource() == forward )
      {
         index = (index + 1) % 18;
         canny.repaint();
         repaint();
      }
      if( e.getSource() == back && index > 1 )
      {
         index--;
         canny.repaint();
         repaint();
      }
      if( e.getSource() == done)
      {
         for( int i = 1; i < 18; i++ )
         {
            userStates[i] = state[ i ].getText();
            userCapitols[ i ] = capitol[ i ].getText();
         }

         for( int i = 1; i < 18; i++ )
         {
            if( userStates[ i ].equalsIgnoreCase( stateName[ i ]) )
               correct++;
            else
            {
               incorrect++;
               sta[ i ] = true;
            }

            if( userCapitols[ i ].equalsIgnoreCase( capitolName[ i ]) )
               correct++;
            else
            {
               incorrect++;
               cap[ i ] = true;
            }
         }
         score = new resultsFrame( correct, incorrect, gw );
         score.setVisible(true);
      }
   }
}

class MyCanvas extends Canvas
{
   private Image statePic[];
   private Font fun;
   private testFrame s;
   private int q;

   MyCanvas( testFrame r  )
   {
       setSize( 202, 202 );
       setVisible( true );

       s = r;

       fun = new Font( "Arial Black", Font.BOLD, 32);

       statePic = new Image[ 18 ];
       for( int i = 1; i < 18; i++)
         statePic[ i ] = s.loadPics( i );
   }

   public void paint( Graphics g )
   {
      q = s.getIndex();

      g.drawRect( 1, 1, 200, 200 );
      g.drawImage( statePic[ q ], 5, 5, 195, 195, this );
      g.setFont( fun );
      g.drawString( "This is state number " + q, 225, 60 );
    }
}