import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
import Dot;

public class Radar extends Frame implements Runnable {
  Vector dots;
  FileInputStream data;
  FileOutputStream flow;
  StreamTokenizer datafile;
  int state, gotostate;

  public static void main (String [] args) {
    Radar radar = new Radar();
    radar.show();
    new Thread(radar).start();
  }

  public Radar() {
    setSize(800, 410);
    setTitle("Radar");
    addWindowListener(new CloseListener());
    dots = new Vector();
    state = 2;
    gotostate = 2;

    Button toggle = new Button("Toggle On/Off");
    toggle.addActionListener(new ToggleButtonListener());
    add("North", toggle);

    try {
      data = new FileInputStream("/dev/cua0");
      flow = new FileOutputStream("/dev/cua0");
      //data = new FileInputStream("test.data");
      datafile = new StreamTokenizer(data);
    } catch (Exception e) {
      System.exit(-1);
    }
  }

  private void doNextLine() {
    int angle, dist;
    //System.out.println("doNextLine");
    try {
      datafile.nextToken();
      angle = (int)datafile.nval;
      datafile.nextToken();
      dist = (int)datafile.nval;
      //System.out.println(angle + " " + dist);
      if (dist > 0)
	dots.addElement(new Dot(angle, dist));
    } catch (IOException e) {
      System.exit(-1);
    }
   }

  public void paint (Graphics g) {
    Dot curDot;
    int curAngle;
    int curDist;
    int isDead = 0;
    double rad = 0;

    g.setColor(Color.black);
    g.fillRect(0, 0, 800, 410);
    Color shade = new Color(0, 255, 0);
    g.setColor(shade);
    g.fillOval(390, 400, 20, 20);

    for (int x = 0; x < dots.size(); x++) {
      curDot = (Dot)dots.elementAt(x);
      curAngle = curDot.getAngle();
      curDist = curDot.getDist();
      rad = Math.PI - ((double)curAngle * Math.PI / 40.0);
      g.setColor(curDot.getShade());
      g.fillOval(398+(int)((curDist/25)*Math.cos(rad)), 408-(int)((curDist/25)*Math.sin(rad)), 4, 4);
      if (curDot.isOld())
	isDead = 1;
    }
    if (rad != 0)
      g.drawLine(400, 410, 398+(int)(570*Math.cos(rad)), 408-(int)(570*Math.sin(rad)));
    if (isDead == 1)
      dots.removeElementAt(0);
  }

  public void run() {
    while (true) {
      switch (state) {
      case 0:
	System.exit(0);
	break;
      case 1:
	doNextLine();
	//System.out.println("  signal cont");
	try {
	  Thread.sleep(30);
	} catch (InterruptedException e) { 
	  System.exit(-1); 
	}
	try {
	  flow.write('1');
	} catch (IOException e) {
	  System.exit(-1);
	}
	repaint();
	break;
      default:
	try {
	  Thread.sleep(1);
	} catch (InterruptedException e) { 
	  System.exit(-1); 
	}
      }

      if (state != gotostate) {
	//System.out.println("Switching states");
	switch (gotostate) {
	case 0:
	  //System.out.println("-> 0");
	  state = 0;
	  try {
	    flow.write('2');
	  } catch (IOException exp) {
	    System.exit(-1);
	  }
	  break;
	case 1:
	  //System.out.println("-> 1");
	  state = 1;
	  try {
	    flow.write('1');
	  } catch (IOException exp) {
	    System.exit(-1);
	  }
	  break;
	case 2:
	  //System.out.println("-> 2");
	  state = 2;
	  //while (dots.size() != 0)
	  //dots.removeElementAt(0);
	  try {
	    flow.write('2');
	  } catch (IOException exp) {
	    System.exit(-1);
	  }
	  break;
	}
      }
    }
  }

  private class CloseListener extends WindowAdapter {   
    public void windowClosing (WindowEvent e) {
      gotostate = 0;
    }
  }

  private class ToggleButtonListener implements ActionListener {
    public void actionPerformed (ActionEvent e) {
      switch (state) {
      case 1:
	gotostate = 2;
	break;
      default:
	gotostate = 1;
      }
    }
  }
}






