import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.util.StringTokenizer;

 public class disc extends Applet
  implements Runnable {
  
  Thread runner1;
   EventDisplay evdisp;
   public static SelectPanel sp;
   public static boolean calo[] = new boolean[36];
   Panel controlPanel;
   public Font font;
   URL theURL;
   public static URL base ;
   public static int initp [] = new int[36];
   public static int nphc [] = new int[200];
   public static int lphc [] = new int[200];
   public static double xphc[] = new double [1000];      
   public static double yphc[] = new double [1000];      
   public static boolean debug = false;
   public static int event = 1;
   public static int eventd = 0;
   public static int maxev = 26;
   public static int runf = 42682;
   public static int run = 42682;
   public static TextField runinput;
   public static Checkbox clearev;
   public static Checkbox loopmode;
   public static Label workdone;
 

  public static double NLeft = -100;
   public static double     NRight = 4500.;
    public static double    NTop = 3000.;
     public static double   NBottom =-2000.;
  public static double Left = -100;
   public static double     Right = 4500.;
    public static double    Top = 3000.;
     public static double   Bottom =-2000.;
    public static int   Nxpixel = 900;
    public static int    Nypixel = 700;
    public static boolean zoom = true;
    public static boolean same = false;
    public static int maxhits = 2000;
    public static int zoomfactor = 1;
    public static  int maxcol = 160;
    int ncolr = 30;
    public static byte redm[] = new byte[maxcol];
    public static byte greenm[] = new byte[maxcol];
    public static byte bluem[] = new byte[maxcol];
    byte b255 = (byte)255;
    
 
        public void init(){

        URLConnection conn;
        DataInputStream data;
       base = getDocumentBase();
       String url = "disc.dat";
       String line;
       StringTokenizer st;

       UserParameters();
       
       CreatePalette();

             setForeground(Color.black);
             font = new Font("Helvetica", Font.BOLD, 12);
             setLayout(new BorderLayout());
              evdisp = new EventDisplay();
              evdisp.resize(900,700);
              resize(950,750);
              add("Center",evdisp);            
              controlPanel = new Panel();
              add("South", controlPanel);
              loopmode  = new Checkbox("Loop");
              loopmode.setState(false);
              controlPanel.add(loopmode);
              clearev  = new Checkbox("Clear");
              clearev.setState(true);
              controlPanel.add(clearev);
               controlPanel.add(new Button("Help"));
               controlPanel.add(new Button("<<"));
               controlPanel.add(new Button("Same event"));
               controlPanel.add(new Button(">>"));
               workdone = new Label("Status display");
               controlPanel.add(workdone);
               controlPanel.add(new Button("Selection"));
               controlPanel.add(new Button("Reset"));
               controlPanel.add(new Button("Available runs"));
               runinput = new TextField(String.valueOf(run));
               controlPanel.add(runinput);
               sp = new SelectPanel("Module selection");
               sp.resize(500,400);
               sp.move(0,0);
               sp.hide();
       try { this.theURL = new URL(disc.base,url); }
    catch ( MalformedURLException e) {
      System.out.println("Bad URL: " + theURL);
    }
     try { 
      conn = this.theURL.openConnection();
      conn.connect();


      data = new DataInputStream(new BufferedInputStream(
                     conn.getInputStream()));
                 int indpol = 0;
                 int indpoi = 0;
                 int npol;
                 for (int i=0; i<36; i++)
                 { 
                     calo[i] = true;
                     initp [i] = indpol ;
                     npol= 10;
                     if(i > 5 && i <30)npol = 3;
                       line = data.readLine();
                       if(debug)System.out.println(line);
                    
                     for (int j=0; j<npol; j++)
                       { 
                       line = data.readLine();
                       st = new StringTokenizer(line);
                       lphc[j+indpol]=Integer.valueOf(st.nextToken()).intValue();
                       nphc[j+indpol] = indpoi ;
                       for (int k=0; k < lphc[j+indpol]; k++)
                             { 
                               line = data.readLine();
                               st = new StringTokenizer(line);
                               xphc[indpoi+k] =Double.valueOf(st.nextToken()).doubleValue();
                               yphc[indpoi+k] =Double.valueOf(st.nextToken()).doubleValue();
                              }
                              indpoi = indpoi + lphc[j+indpol];
                         }
                        indpol = indpol + npol; 
                       
                  }
                  if(debug)System.out.println ("indpoi"+indpoi+" indpol "+indpol);
            }
      catch (IOException e) {
      System.out.println("IO Error:" + e.getMessage());
       }


       }
private void UserParameters () {
            String parameter;

            // Get user parameters
            parameter = getParameter ("debug");
            if (parameter != null) debug = parameter.equals("true");
    }
  private byte interp(int a,int b,double f) {
                return (byte)(a*(1-f)+b*f);
       }
private void CreatePalette() {
   int i;
   redm[0] = 0; greenm[0] = 0; bluem[0] = 0;
   redm[1] = b255; greenm[1] = b255; bluem[1] = b255;
   redm[2] = b255; greenm[2] = 0; bluem[2] = 0;
   redm[3] = 0; greenm[3] = b255; bluem[3] = 0;
   redm[4] = 0; greenm[4] = 0; bluem[4] = b255;
   redm[5] = b255; greenm[5] = b255; bluem[5] = 0;
   redm[6] = b255; greenm[6] = 0; bluem[6] = b255;
   redm[7] = 0; greenm[7] = b255; bluem[7] = b255;
   redm[8] = 0; greenm[8] = (byte)254; bluem[8] = (byte)254;
   redm[9] = 0; greenm[9] = (byte)253; bluem[9] = (byte)253;
            for (i=0;i<ncolr;i++) {
                    redm[i+10] = interp(0,0,i/(double)(ncolr-1.));
                    greenm[i+10] = interp(0,0,i/(double)(ncolr-1.));
                    bluem[i+10] = interp(0,255,i/(double)(ncolr-1.));
            }
            for (i=0;i<ncolr;i++) {
                    redm[i+ncolr+10] = interp(0,0,i/(double)(ncolr-1.));
                    greenm[i+ncolr+10] = interp(0,255,i/(double)(ncolr-1.));
                    bluem[i+ncolr+10] = interp(255,0,i/(double)(ncolr-1.));
            }
            for (i=0;i<ncolr;i++) {
                    redm[i+2*ncolr+10] = interp(0,255,i/(double)(ncolr-1.));
                    greenm[i+2*ncolr+10] = interp(255,0,i/(double)(ncolr-1.));
                    bluem[i+2*ncolr+10] = interp(0,0,i/(double)(ncolr-1.));
            }
            for (i=0;i<ncolr;i++) {
                    redm[i+3*ncolr+10] = interp(255,255,i/(double)(ncolr-1.));
                    greenm[i+3*ncolr+10] = interp(0,255,i/(double)(ncolr-1.));
                    bluem[i+3*ncolr+10] = interp(0,0,i/(double)(ncolr-1.));
            }
            for (i=0;i<ncolr;i++) {
                    redm[i+4*ncolr+10] = interp(255,255,i/(double)(ncolr-1.));
                    greenm[i+4*ncolr+10] = interp(255,255,i/(double)(ncolr-1.));
                    bluem[i+4*ncolr+10] = interp(0,255,i/(double)(ncolr-1.));
            }



 }
   public void start() {
    if (runner1 == null) {
        runner1 = new Thread(this);
        runner1.start();
    }
   }

  public void stop() {
     if (runner1 != null) {
       runner1.stop();
       runner1 = null;
     }
   }
  public void run() {
       evdisp.repaint();
       while(loopmode.getState()){
            while(disc.event != disc.eventd){
                    try { Thread.sleep(100);}
                   catch (InterruptedException e) { };
                        }
                        disc.event = disc.event + 1;
                        if(disc.event > disc.maxev){  
                          disc.event=1;
                          getnext();
                          } 
                        evdisp.repaint();
       }
 }
  public boolean mouseUp(Event e, int x, int y) {
    double zoom_x,zoom_y,width,height; 
    if (disc.zoom && y < 667) {
       zoomfactor = zoomfactor + 1;
       zoom_x = disc.Left + (disc.Right-disc.Left)/disc.Nxpixel*x;
       zoom_y = disc.Bottom + (disc.Top-disc.Bottom)/disc.Nypixel*(Nypixel-y);
       width = disc.Right-disc.Left;
       height = disc.Top - disc.Bottom; 
       disc.Left = zoom_x -width/4.;
       disc.Right = zoom_x + width/4.;
       disc.Bottom = zoom_y -height/4.;
       disc.Top = zoom_y + height/4.;
       System.out.println(x+" "+y);
       System.out.println(disc.Left+" "+disc.Right+" "+disc.Bottom+" "+disc.Top);
       stop();
       start();
      }
    return true;
  }
  public void getnext() {
        URL loadnext;
        URLConnection conn;
        DataInputStream data;
        String line;
        StringBuffer buf = new StringBuffer();
                          try {loadnext= new URL(disc.base,"/dron_com/event.com?next");
//                          getAppletContext().showDocument(loadnext);
                             try { 
                              conn = loadnext.openConnection();
                              conn.connect();


                              data = new DataInputStream(new BufferedInputStream( conn.getInputStream()));

                              while ((line = data.readLine()) != null) {
                               buf.append(line + "\n");
                              } 

                              }
                              catch (IOException e5) {
                               System.out.println("IO Error:" + e5.getMessage());
                               }
                              }
                          catch ( MalformedURLException e1) {
                             System.out.println("Bad URL:event.com " );
                              }
 }
public boolean action(Event e, Object arg)
        {
        URL loadnext;
                if("Same event".equals(arg)){
                        same = true;
                        stop();
                        start();
                        return true;
                }
                if(">>".equals(arg)){
                        disc.event = disc.event + 1;
                        if(disc.event > disc.maxev){  
                          disc.event=1;
                          getnext();
                          } 
                        stop();
                        start();
                        return true;
                }
                if("Help".equals(arg)){
                          try {loadnext= new URL(disc.base,"dronhelp.html");
                          getAppletContext().showDocument(loadnext);
                              }
                          catch ( MalformedURLException e2) {
                             System.out.println("Bad URL:dronhelp.html " );
                              }
                        return true;
                }
                if("<<".equals(arg)){
                        disc.event = disc.event - 1;
                        stop();
                        start();
                        return true;
                }
                if("Selection".equals(arg)){
                        sp.show();
                        return true;
                }
                if("Reset".equals(arg)){
                        Left = NLeft;Right=NRight;Bottom=NBottom;Top=NTop;
                        zoomfactor = 1;
                        stop();
                        start();
                        return true;
                }
                if("Available runs".equals(arg)){
                        disc.event = 1;
                          try {loadnext= new URL(disc.base,"/dron_com/runlist.com");
                          getAppletContext().showDocument(loadnext);
                              }
                          catch ( MalformedURLException e2) {
                             System.out.println("Bad URL:runlist.com " );
                              }
                        stop();
                        start();
                        return true;
                }
               if(e.target instanceof TextField) {
                        if(e.target == runinput) {
                        run =Integer.valueOf(runinput.getText()).intValue();
                        disc.event = 1;
                        try {loadnext= new URL(disc.base,"/dron_com/event.com?m00"+run+".dat");
                          getAppletContext().showDocument(loadnext);
                              }
                          catch ( MalformedURLException e3) {
                             System.out.println("Bad URL:event.com " );
                              }
                        stop();
                        start();
                          }
                 return true;
                  }
                return true;
        }
}
  class  EventDisplay extends Canvas {
 URL theURL;    
        public void update(Graphics g)
        {
        if(disc.clearev.getState())
        {
          g.setColor(Color.black);
          g.fillRect(0,0,900,665);
        } 
        else
        {
          g.setColor(Color.black);
          g.fillRect(0,600,900,665);
        }
        paint(g);
        } 
       public void paint(Graphics g)
        {
       URLConnection conn;
        DataInputStream data;
        int nseg,numto,numwi,npo,ien;

       String url ;
       String line;
       StringTokenizer st;  
       double xp[] = new double [20];
       double yp[] = new double [20];
       int i,j,k;

              for (i=0; i<36; i++)
                 {
                        if(disc.sp != null)disc.calo[i]=disc.sp.getsel(i);
                  }     
             setBackground(Color.black);

                  g.setColor(new Color(150,150,150));

                 for (i=0; i<36; i++)
                 { 
                        if(disc.calo[i])dsmosc(g,i); 
                  }
                   disc.workdone.setText(" reading new event ");
        url =  "r"+Integer.toString(disc.runf)+"e"+Integer.toString(disc.event)+".dat";                                
       try { this.theURL = new URL(disc.base, url); }

    catch ( MalformedURLException e) {
      System.out.println("Bad URL: " + theURL);    
             }
   if(disc.debug)System.out.println("next event "+theURL);  
     try {
      conn = this.theURL.openConnection();
      conn.connect();

      int ntotel = 0;
      data = new DataInputStream(new BufferedInputStream(
                     conn.getInputStream()));
                     line = data.readLine();
                     g.drawString(line,5,625);
                       if(disc.debug)System.out.println(line);  
                   disc.workdone.setText(" loading towers");
                       for (i=0; i<36; i++)
                        {
                       if(!disc.same && (ntotel>disc.maxhits)) break;
                       line = data.readLine();
                       st = new StringTokenizer(line);
                       nseg=Integer.valueOf(st.nextToken()).intValue();
                       numto=Integer.valueOf(st.nextToken()).intValue();
                       if(disc.debug)System.out.println(line);  
                       if(numto > 0)
                         {
                         for(j=0; j<numto; j++)
                              {
                              line = data.readLine();
                              if(disc.debug)System.out.println(line);  
                              st = new StringTokenizer(line);
                              npo=Integer.valueOf(st.nextToken()).intValue();
                              ntotel = ntotel + npo;
                              ien=Integer.valueOf(st.nextToken()).intValue();
                              line = data.readLine();
                              st = new StringTokenizer(line);
                              for (k=0; k<npo; k++)
                                  {
                                   xp[k] =Double.valueOf(st.nextToken()).doubleValue();
                                   yp[k] =Double.valueOf(st.nextToken()).doubleValue();
                                   }      
                                   ien = ien / 20;
                                   ien = ien + 10;
                                   if(ien > (disc.maxcol -1))ien = disc.maxcol -1;
                                    if(disc.calo[i])dgfa(g,xp,yp,npo,ien);
                                }
                   if(numto > 10) try { Thread.sleep(100);}
                   catch (InterruptedException e) { };
                          }
                         }

                   disc.workdone.setText(" loading wires");
                  g.setColor(new Color(0,255,0));

                       for (i=0; i<36; i++)
                        {
                       if(!disc.same && (ntotel>disc.maxhits)) break;
                       line = data.readLine();
                       st = new StringTokenizer(line);
                       nseg=Integer.valueOf(st.nextToken()).intValue();
                       numwi=Integer.valueOf(st.nextToken()).intValue();
                       if(disc.debug)System.out.println(line);
                       if(numwi > 0)
                         {
                         if(i>5 && i <30)numwi=numwi*2; 
                         for(j=0; j<numwi; j++)
                              {
                              line = data.readLine();
                              if(disc.debug)System.out.println(line);
                              st = new StringTokenizer(line);
                              npo=Integer.valueOf(st.nextToken()).intValue();
                              ntotel = ntotel + npo;
                              line = data.readLine();
                              st = new StringTokenizer(line);
                              for (k=0; k<npo; k++)
                                  {
                                   xp[k] =Double.valueOf(st.nextToken()).doubleValue();     
                                   yp[k] =Double.valueOf(st.nextToken()).doubleValue();
                                   }
                                    if(disc.calo[i])dgpl(g,xp,yp,0,npo);
                             }
                   if(numwi > 30)try { Thread.sleep(100);}
                   catch (InterruptedException e) { };
                          }
                         }                                                                   
                   if(ntotel>disc.maxhits)disc.workdone.setText(" too many hits!");
                     else disc.workdone.setText(" event complete");
                     disc.eventd=disc.event;
                   if(disc.same)disc.same=false;
       
            }
      catch (IOException e) {
      System.out.println("IO Error:" + e.getMessage());
       }
       }
     

 void dgpl(Graphics g,double xp[],double yp[], int ip, int np)  {
  int x[] = new int[np];
  int y[] = new int[np];
  int allequal = 1;
  for (int k=0;  k < np; k++)
   {
       x[k] = (int)((xp[k+ip]-disc.Left)/(disc.Right-disc.Left)*disc.Nxpixel);
       y[k] = (int)((yp[k+ip]-disc.Bottom)/(disc.Top-disc.Bottom)*disc.Nypixel);                           
       y[k] = disc.Nypixel - y[k];       
       if(k > 1 && (x[k] != x[k-1]) ) allequal=0;
       if(k > 1 && (y[k] != y[k-1]) ) allequal=0;
    }
  if(allequal == 1 && np == 5) 
            {
                x[1] = x[0] + 1;
                x[2] = x[0] + 1;
                y[2] = y[0] + 1;
                y[3] = y[0] + 1;
            }
 
   g.drawPolygon(x,y,np);
} 

 void dgfa(Graphics g,double xp[],double yp[], int np,int ic)  {
  int x[] = new int[np];
  int y[] = new int[np];
  for (int k=0;  k < np; k++)
   {
       x[k] = (int)((xp[k]-disc.Left)/(disc.Right-disc.Left)*disc.Nxpixel);
       y[k] = (int)((yp[k]-disc.Bottom)/(disc.Top-disc.Bottom)*disc.Nypixel);                           
       y[k] = disc.Nypixel -y[k];
 //      if(disc.debug)System.out.println(xp[k]+" "+x[k]+" "+yp[k]+" "+y[k]);
    }
   g.setColor(new Color(disc.redm[ic],disc.greenm[ic],disc.bluem[ic]));
   g.fillPolygon(x,y,np);
} 
         void dsmosc(Graphics g, int nseg)
        {
                double x,y,prevx,prevy;
                int x1,y1;
                int x0 = 150;
                int y0 = 150;
                int indpo;

                 int ind = disc.initp[nseg];
                 int npol = 10;
                 if(nseg >5 && nseg <30) npol = 3;
                 if(disc.debug)System.out.println("ind "+ind+" npol "+npol);  
                 for (int j=0; j<npol; j++)
                 {
                  indpo = disc.nphc[ind+j];
                  dgpl(g,disc.xphc,disc.yphc,indpo,disc.lphc[ind+j]);
                   } 
                   } 
                  
                                                                               
 }
class SelectPanel extends Frame {


  Checkbox calo[] = new Checkbox[36];

  public SelectPanel(String s){
  super(s);
  MenuBar mb = new MenuBar();
  setMenuBar(mb);
  Menu m1 = new Menu("File");
  mb.add(m1);
  m1.add(new MenuItem("Quit"));
  setLayout(new BorderLayout());
  Panel p = new Panel();
  Panel p1 = new Panel();
  Panel p2 = new Panel();
  Panel p3 = new Panel();
  String sdetname = " ";
  int i;
  p1.setLayout(new GridLayout(1,6));
  for ( i = 0; i < 6; i++) {
    sdetname="Enda"+(i+1);
    calo[i] = new Checkbox(sdetname);
    calo[i].setState(true);
    p1.add(calo[i]);
    }
  p.add(p1);
  p2.setLayout(new GridLayout(4,6));
  for ( i = 6; i < 30; i++) {
    sdetname="Bar"+(i-5);
    calo[i] = new Checkbox(sdetname);
    calo[i].setState(true);
    p2.add(calo[i]);
    }
  p.add(p2);
  p3.setLayout(new GridLayout(1,6));
  for (i = 30; i < 36; i++) {
    sdetname="Endb"+(i-29);
    calo[i] = new Checkbox(sdetname);
    calo[i].setState(true);
    p3.add(calo[i]);
    }
  p.add(p3);
  add("Center",p);
}

  public boolean action(Event e, Object o) {
 if (e.target instanceof MenuItem) {
      String label = (String)o;
      if (label.equals("Quit")) hide();
      return true;
      }
    return false;
  
}
 public boolean getsel (int i) {
    return calo[i].getState();
    }   
}

