package ps1;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Graphics;
import java.util.Vector;
import java.applet.*;
import java.awt.*;

         
public class ps1 extends Applet implements MouseListener {

/**
 * The ps1 class extends Applet and implements MouseListener
 * This class initializes the applet and acts as the 
 * image observer.
 * <P> 6.837 Problem set #1
 * @author Christina Chu (sepherke@mit.edu)
 */


  String backgrd = "Images/back.gif";
  String clk = "Images/clock/clock.gif";
  Raster raster;    // raster that is used for displaying pixels. 

  AnimatedSprite acat;
  AnimatedSprite rcat;
  AnimatedSprite akatt;
  AnimatedSprite akatt2;
  AnimatedSprite chands;
  Sprite clock;

  Playfield pf;
  Image back;
  int numsprites =0;  
  // the only point of numsprites being to keep track
  // of how many sprites we have without having to go look it up
  // in the play field

  /**
   * This method initializes the applet, creates sprites and 
   * adds them to the playfield. 
   *
   */

  public void init() {
    Image back = getImage(getDocumentBase(),backgrd);
    Image c = getImage(getDocumentBase(),clk);

    // create background and playfield
    pf = new Playfield(back,10);
    raster = new Raster(back);

    // Static clock sprite
    clock = new Sprite(c);
    clock.setLocation(93, 350);
    clock.Draw(this,raster);

    Vector seq = new Vector();
    Vector seq2 = new Vector();
    for (int i=11; i<40; i++) {
      seq.addElement(getImage(getDocumentBase(),"Images/acat/a-cat01."+i+".gif")); 
      seq2.addElement(getImage(getDocumentBase(),"Images/rcats/catrun."+i+".gif")); 
    }

    // Animated Sprites    
    acat = new AnimatedSprite(seq,28);    
    acat.addState(0,0,28,10,0);    
    acat.setLocation(150,470);
    pf.addSprite(numsprites,acat);   
            
    rcat = new AnimatedSprite(seq2,23);
    rcat.addState(0,0,23,10,0);
    numsprites++;
    rcat.setLocation(0,470);
    pf.addSprite(numsprites,rcat);
    
    Vector seq3 = new Vector();
    for (int j =0; j<10; j++){ 
      seq3.addElement(getImage(getDocumentBase(),"Images/icon/a_icons."+j+".gif")); 
    }    
    for (int p =0; p <10; p++) {
      seq3.addElement(getImage(getDocumentBase(),"Images/akatt/a-katt."+p+".gif")); 
    }

    akatt = new AnimatedSprite(seq3,18);
    akatt.addState(0,0,9,0,0);        // static, track 1, frame 0, tick 3, dx, dy
    akatt.addState(1,10,8,20,3);       
    akatt.addState(2,10,8,20,3);       
    akatt.setLocation(240, 405);
    numsprites++;
    pf.addSprite(numsprites,akatt);

    akatt2= new AnimatedSprite(seq3,18);
    akatt2.addState(0,0,9,0,0);        // static, track 1, frame 0, tick 3, dx, dy
    akatt2.addState(1,10,8,20,0);   
    akatt2.addState(2,10,8,20,0);   
    akatt2.setLocation(120,100);
    numsprites++;
    pf.addSprite(numsprites,akatt2);

    Vector hands = new Vector();
    for (int q =1; q<12; q++){ 
      hands.addElement(getImage(getDocumentBase(),"Images/clock/"+q+"oclock.gif")); 
    }    
     chands = new AnimatedSprite(hands,11);
     chands.addState(0,0,10,0,0);
     chands.setLocation(112,370);
     numsprites++;
     pf.addSprite(numsprites,chands);

    addMouseListener(this);    
    //    System.out.println("end of init");
  }

  /**
   *   paints graphics on the applet
   */
     
  public void paint(Graphics g) {          
    Image output = raster.toImage(this);    
    pf.Draw(this,raster);
    g.drawImage(output,0,0,this);     
  }

  /**
   *  updates the graphics 
   */
  
  public void update(Graphics g) {
    paint(g);
  }

  /**
   * This method handles mouse click events. If the mouse is clicked
   * on an existing sprite, it will either move to the next track
   * for that animated sprite, or restart the current track.
   * If the mouse is not clicked on an existing sprite, then 
   * a running cat is created at the location where the mouse
   * was clicked.    
   * 
   */

  public void mouseClicked(MouseEvent event) {
    Point pt = event.getPoint();       
    int index= -1;
    try {
      index = pf.findIndexOfSprite(pt.x,pt.y);     
      pf.switchTrack(index, pt.x, pt.y);      
    } catch(ArrayIndexOutOfBoundsException e) {
      // System.err.println("Array index out of bounds" +e);
      // Don't display to user. This means that there isn't
      // a sprite at location. Create a new cat sprite here.
    }
    if (index == -1) {
      numsprites++;
      Vector sequ = new Vector();
      for (int j =0; j<10; j++){ 
	sequ.addElement(getImage(getDocumentBase(),"Images/icon/a_icons."+j+".gif"));           
      }
      for (int h =0; h<10; h++){ 
	sequ.addElement(getImage(getDocumentBase(),"Images/akatt/a-katt."+h+".gif")); 
      }

      AnimatedSprite f = new AnimatedSprite(sequ,18);
      f.addState(0,0,9,0,0);        // static, track 1, frame 0, tick 3, dx, dy
      f.addState(1,10,8,20,3);       
      f.addState(2,10,8,20,3);       
      numsprites++;
      f.setLocation(pt.x, pt.y);
      pf.addSprite(numsprites, f);                
    }
  }


  /**
   *  This method handles mouse pressed events. 
   */
  
  public void mousePressed(MouseEvent event) {
  }

  /**
   * This method handles the event when the mouse enters
   * the side of the applet
   */

  public void mouseEntered(MouseEvent event) {
  }

  /**
   * This method handles the event when the mouse enters
   * the side of the applet
   */

  public void mouseExited(MouseEvent event) {
  }

  /**
   * This method handles the event when the mouse 
   * is released
   */

  public void mouseReleased(MouseEvent event) {
  }
}

