package FOExperiment;

import java.awt.*; 
import java.util.*;
import ThirdParty.BarChart.*;
import DataMonitor.*;

public class G_Detector extends Panel implements Runnable{

  Thread analyzeThread;
  Histogram hist = null;

  float [] data=null;
  public int numberBins = 10;// default

  int ySensorTop,ySensorBot,shootWidth,yGap;
  boolean passedTop,passedBot;

  int tClock=0;
  int topTime=0;
  int botTime=0;
  double TimeResolution = 10.0;
  int lowerLimit = 180;// default
  int upperLimit = 340;// default
  
  int panelWidth  = 175;// default
  int panelHeight = 200;// default
  Label histLabel = null;

  Random ran = new Random();

//----------------------------------------------------------------
  public G_Detector(int yTop, int yBot,int detWidth){
    // Add sub-panels with readouts, histograms and controls

    setBackground(Color.cyan);


    Panel p1 = new Panel();
    p1.setLayout(new BorderLayout());

    p1.add("North",new Label("Drop Delta-T times"));
    histLabel = new Label("Bin Range(ms)",Label.CENTER);

    makeHist();
    p1.add("Center",hist.chart._barChart);
    p1.add("South",histLabel);
     
    add(p1);

    ySensorTop = yTop;
    ySensorBot = yBot;
    shootWidth = detWidth;

    yGap = ySensorTop - yTop;

  }
//----------------------------------------------------------------
  public void makeHist(){
    hist = 
      new Histogram(numberBins,lowerLimit,upperLimit,
                               panelWidth,panelHeight-50);
    int dt = upperLimit - lowerLimit;
    histLabel.setText("Bin Range:"+dt+"ms");
  }
//----------------------------------------------------------------
// Offer option of remaking histogram
  public void makeHist(int numBins, int lower, int upper, 
                       int _wt,int _ht){

    numberBins = numBins;
    lowerLimit = lower;
    upperLimit = upper;
    panelWidth = _wt;
    panelHeight= _ht;

    hist = 
      new Histogram(numberBins,lowerLimit,upperLimit,
                               panelWidth,panelHeight-50);
    int dt = upperLimit - lowerLimit;
    histLabel.setText("Bin Range:"+dt+"ms");
  }
//----------------------------------------------------------------
// Get the number of data bins 
  public int getNumData(){
     return numberBins;
  }
//----------------------------------------------------------------
  public void init(){
    passedTop = false;
    passedBot = false;
    hist.reset();
    repaint();
  }
//----------------------------------------------------------------
  public void start() { 
    analyzeThread = new Thread(this);    
    analyzeThread.start();
  }
//----------------------------------------------------------------
  public void stop() { 
    analyzeThread = null;
  }
//----------------------------------------------------------------
// 
  public void eventStart(){
    passedTop = false;
    passedBot = false;
    tClock = 0;
  }
//----------------------------------------------------------------
// 
  public void eventDone(){
    repaint();
  }
//----------------------------------------------------------------
// 
  public void sensor(double y, int t){
    tClock += t;
    if(!passedTop){
      if( y >= ySensorTop){
        topTime = tClock;
        passedTop=true;
      }
      return;
    }
    if(!passedBot){
      if( y >= ySensorBot){
        botTime = tClock;
        passedBot=true;

        // Data is ready so start detector thread
        start();
      }
      return;
    }
  }
//----------------------------------------------------------------
// 
  public void analyze(){
    double dt = botTime - topTime;
    dt += (int)(TimeResolution * ran.nextGaussian());

    recordData(dt);

    stop();
  }
  //----------------------------------------------------------------
  public void run() { 
    // Set at lower priority so that the animation will
    // be at top priority.
    analyzeThread.setPriority(Thread.NORM_PRIORITY - 1);

    // Do analysis when animation no busy.
    while (analyzeThread != null) { 
      analyze();
    }
  }
//----------------------------------------------------------------
// Display the message, data, data event and histogram displays
  public void paint(Graphics g){
    hist.graphIt();
  }
//----------------------------------------------------------------
// Draw the sensors on the drop shoot
  public void draw(Graphics g){
    Color c = g.getColor();
    g.setColor(Color.green);
    g.drawLine(0,ySensorTop,shootWidth,ySensorTop);
    g.drawLine(0,ySensorBot,shootWidth,ySensorBot);
    g.setColor(c);
  }
//----------------------------------------------------------------
  public void recordData(double dataPt){
    hist.setData(dataPt);
  }
  //----------------------------------------------------------------
  public float [] getData(){
    float [] dat = new float[numberBins];
    for (int i=0; i<numberBins; i++){
      dat[i] = hist.bins[i];
    }
    return dat;
  }
}