package csc454.net;
import java.awt.*;
import java.applet.*;
import java.util.*;

public class netApplet extends Applet {
  protected TextArea txt_;
  protected static double min_=3;
  protected static double max_=5;
  protected static double norm_=1000;
  protected net n_;
  protected static int stationCount_=8;
  protected static int stationWidth_=60;
  protected arrival r_[] = new arrival[8];
  protected queue q_[] = new queue[8];
  protected station s_[]=new station[8];
  protected boolean begin_=true;
  protected boolean terminate_=true;
  protected Button beginButton;
  protected Button terminateButton;
  protected time t_;
  public netApplet() {
    beginButton = new Button("START");
    terminateButton = new Button("TERMINATE");
    txt_ = new TextArea(40, 10);
    n_ = new net(stationCount_,stationWidth_,10,txt_);
    for (int i=0;i<=7;i=i+1) {
        q_[i] = new queue(txt_,i);
        r_[i] = new arrival(q_[i], txt_, i,min_,max_,norm_);
        s_[i] = new station(q_[i], txt_, i,n_,norm_,stationWidth_);
    }
  }
  public void init() {
    setLayout( new BorderLayout(0,0) );
    Panel p = new Panel();
    p.setLayout(new FlowLayout());
    for (int i=0;i<=7;i=i+1) p.add(s_[i]);
    p.add(beginButton);
    p.add(terminateButton);
    p.resize(800,62);
    add("North",p);
    add("West",n_);
    add("South",txt_); // add text area to applet display
  }

  public void start() {
    for (int i=0;i<=7;i=i+1) {
      new Thread(s_[i]).start();
      new Thread(r_[i]).start();
    }
  }

  public boolean action(Event event, Object arg) {
    if (event.target == beginButton & begin_) {
      txt_.setText("Simulation Starts");
      begin_=false;
      t_=new time(norm_);
      for (int i=0;i<=7;i=i+1) {
        r_[i].begin(t_);
        q_[i].begin(t_);
        s_[i].begin(t_);
      }
      n_.begin(t_);
      return true;
    }
    if (event.target == terminateButton & terminate_ & !begin_) {
      terminate_=false;
      for (int i=0;i<=7;i=i+1) r_[i].terminate();
      for (int i=0;i<=7;i=i+1) q_[i].terminate();
      for (int i=0;i<=7;i=i+1) s_[i].terminate();
      return true;
    }
    return(super.action(event,arg));
  }
}

