package mkgray.gui;

import mkgray.gui.util.*;
import java.awt.*;

// Will implement a telemetry consumer
public class SimpleGraph extends Canvas {
  DoubleBuffer db = new DoubleBuffer();
  GraphPoint head = null;
  GraphPoint cur = null;
  double windowWidth = 100.0;
  double tickSeperation = 10.0;
  boolean sliding;

  //  addData(int who, item[] )
  public void addPoint(double x, double y){
    if(head == null){
      head = new GraphPoint(x, y);
      head.xmax = head.xmin = x;
      head.ymax = head.ymin = y;
      cur = head;
    }
    cur.next = new GraphPoint(x, y);
    cur = cur.next;
    while(((cur.x-head.x) > windowWidth) && sliding){
      if(head.xmin == head.x)
	head.xmin = head.next.x;
      head = head.next;
      //      System.gc();
    }
  }

  public void update(Graphics g){
    long startt, endt;

    startt = System.currentTimeMillis();
    db.update(this, g);
    endt = System.currentTimeMillis();
    System.out.println("update took: "+(endt-startt));
  }

  public void sliding(boolean s){
    sliding = s;
  }

  public void windowWidth(double v){
    windowWidth = v;
  }

  public void tickSeperation(double v){
    tickSeperation = v;
  }

  public void paint (Graphics g){
    GraphPoint p=null, pp=null;
    long startt, endt;

    startt = System.currentTimeMillis();
    // Draw the scales
    g.setColor(Color.darkGray);
    g.drawLine(25, this.getSize().height-25, 25, 0);
    g.drawLine(25, this.getSize().height-25,
	       this.getSize().width, this.getSize().height-25);
    g.setColor(Color.gray);
    g.drawLine(24, this.getSize().height-25, 24, 0);
    g.drawLine(26, this.getSize().height-26,
	       this.getSize().width, this.getSize().height-26);

    // Draw ticks
    for(double ct=((head.xmin+tickSeperation)%tickSeperation)*tickSeperation;
	ct < head.xmax; ct+= tickSeperation){
      g.setColor(Color.darkGray);
      g.drawLine(x(ct,0), this.getSize().height-24, x(ct,0), this.getSize().height-15);
      g.drawLine(x(ct,0), this.getSize().height-15, x(ct,0)+1, this.getSize().height-15);
      g.setColor(Color.gray);
      g.drawLine(x(ct,0)+1, this.getSize().height-24, x(ct,0)+1, this.getSize().height-16);
      g.setColor(Color.black);
      g.drawString(""+((int)ct), x(ct,0)-7, this.getSize().height-4);
    }

    g.drawString(""+head.ymax, 3, 20);

    // Draw the lines
    for(p = head; p.next != null; p = p.next){
      if(pp != null){
	g.setColor(Color.black);
	g.drawLine(x(pp.x, pp.y), y(pp.x, pp.y), x(p.x, p.y), y(p.x, p.y));
      }
      pp = p;
    }
    g.setColor(Color.lightGray);
    endt = System.currentTimeMillis();
    System.out.println("paint took: "+(endt-startt));
  }

  public int x(double x, double y){
    int height, width;
    height = this.getSize().height;
    width = this.getSize().width;
    
    if(head != null){
      return (int) (25+(width-35)*(x-head.xmin)/(head.xmax-head.xmin));
    }
    else{
      return 0;
    }
  }
  public int y(double x, double y){
    int height, width;
    height = this.getSize().height;
    width = this.getSize().width;
    
    if(head != null){
      return (int) ((height-25)-((height-35)*(y-head.ymin)/(head.ymax-head.ymin)));
    }
    else{
      return 0;
    }
  }

}

class GraphPoint {
  static double xmax, ymax, xmin, ymin;
  static int length;
  double x, y;
  GraphPoint next;

  GraphPoint(double nx, double ny){
    if(nx > xmax)
      xmax = nx;
    if(nx < xmin)
      xmin = nx;

    if(ny > ymax)
      ymax = ny;
    if(ny < ymin)
      ymin = ny;

    x = nx;
    y = ny;
    next = null;
  }
}

