package seisinfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.io.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class WLastFrame extends JFrame {
  String wlastpath1;
  String wlastpath2;
  String wlasttitle;
  int refresh;

  double XX;
  double YY;
  double MaxX;
  double MinX;
  double MaxY;
  double MinY;


  boolean done=false;
  boolean offline=false;
  private Image offScreenImg;
  private Graphics offScreenG;
  public static wlast1 t1;
  Image image1;
  Image image2;
  Image scaled1;
  Image scaled2;
  MediaTracker tracker1 = new MediaTracker(this);
  MediaTracker tracker2 = new MediaTracker(this);
  String newpath;
  Toolkit toolkit = Toolkit.getDefaultToolkit();
  String inpline=" ";
  int length;

  int scrheight;                   // screen height
  int scrwidth;                    // screen width
  int xmarg=10;                    // x-margin
  int ymarg=10;                    // y-margin
  int xx;                          // picture width
  int yy;                          // picture height
  int ysubtitle=70;                // height subtitle
  int ytitle=30;                   // height frame title


  public WLastFrame(String wlastpath1,String wlastpath2,String wlasttitle, int ref) {
    this.wlastpath1=wlastpath1;
    this.wlastpath2=wlastpath2;
    this.wlasttitle=wlasttitle;
    this.refresh=ref;

    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    scrheight=screenSize.height;  // get screen height
    scrwidth=screenSize.width;    // get screen width
    System.out.println(scrheight+"  "+scrwidth);

    this.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        done=true;
        offline=true;
        System.out.println("CLOSE");
        Close();
      }
    });
    addComponentListener(new java.awt.event.ComponentAdapter()
    {
      public void componentMoved(java.awt.event.ComponentEvent evt)
      {
        formComponentMoved(evt);
      }
       public void componentResized(java.awt.event.ComponentEvent evt)
       {
         formComponentResized(evt);
       }
    });

  }
  private void formComponentMoved(java.awt.event.ComponentEvent evt) {
    // Add your handling code here:
//        System.out.println("I was moved to "+this.getLocation());
    XX=this.getLocation().getX();
    YY=this.getLocation().getY();
    System.out.println("WLAST is here: "+XX+","+YY);
}
private void formComponentResized(java.awt.event.ComponentEvent evt)
{
    // Add your handling code here:
//      System.out.println("TU "+this.getBounds());
    XX=this.getBounds().getMinX();
    YY=this.getBounds().getMinY();
    System.out.println("WLAST is here: "+XX+","+YY);
    MaxX=this.getBounds().getMaxX();
    MinX=this.getBounds().getMinX();
    MaxY=this.getBounds().getMaxY();
    MinY=this.getBounds().getMinY();
//        System.out.println((int)MinX+","+(int)MinY+"  "+(int)MaxX+","+(int)MaxY);
}
  public int LocX()
  {
      return((int)XX);
  }
  public int LocY()
  {
      return((int)YY);
  }

  public void Close()
  {
    this.setVisible(false);
    done=true;
    SeisInfoFrame.WLAST=false;
  }
  public void CrtImg()
  {
    offScreenImg = createImage(getSize().width, getSize().height);
    offScreenG = offScreenImg.getGraphics();
  }
  public void StartWLAST()
  {
    t1=new wlast1();
    t1.start();
  }

  public void update(Graphics g)
  {
    offScreenG = offScreenImg.getGraphics();
    paint(offScreenG);
    g.drawImage(offScreenImg,0,0,this);
  }

  public int newpic()
  {
    System.out.println("WLAST: "+newpath);
    try
    {
      image1 = toolkit.getImage(new URL(newpath));
    }
    catch (MalformedURLException e)
    {
    }
/*
    tracker1.addImage(image1,1);
    try
    {
        if(!tracker1.waitForID(1,6000))
        {
            System.out.println("WLASTFrame: Error Loading Image 1");
            offline=true;
            return(-1);
        }
    }catch(InterruptedException e){System.out.println(e);}
*/
    xx=scrwidth/2 - 2*xmarg;
    yy=scrheight/2 - 2*ymarg - ytitle - ymarg;
    scaled1=image1.getScaledInstance(xx,yy,Image.SCALE_AREA_AVERAGING);
    return(1);

  }
  public void paint(Graphics g)
  {

    if(scaled1 !=null)
    {
      g.drawImage(scaled1, 13, 50, this);
      image1.flush();
    }
      g.drawString("Source: USGS",10,40);
  }
  class wlast1 extends Thread
  {
    int found=0;
    public void run()
    {
      while(!done)
      {
        while(!offline)
        {
        try
        {
          URL recentURL = new URL(wlastpath1);
          InputStreamReader inpStream = new InputStreamReader(recentURL.openStream());
          BufferedReader br = new BufferedReader(inpStream);
//          System.out.println("URL: "+wlastpath1);
          for(int i=0;i<400;i++)
          {
            inpline = br.readLine();
            length=inpline.length();
            String search="onMouseOver";
            for(int j=0;j<(length-15);j++)
            {
              int ret=inpline.substring(j,j+11).trim().compareTo(search);
              if(ret == 0)
              {
//                System.out.println("onmouse.."+inpline);
                found=1;
                break;
              }
            }
            if(found==1)
              break;
          }
          br.close();
          } catch (IOException ex2)
          {
            System.out.println(" Received Exception IOException: " + ex2);
          }
          if(found==1)
          {
            found = 0;
            int p = 0;
            for (p = 0; p < (length - 15); p++) {
              String find = inpline.substring(p, p + 5);
              int ret = find.compareTo("href=");
              if (ret == 0)
                break;
            }
//          System.out.println("REF= "+inpline);
            String ref = inpline.substring(p + 6, p + 6 + 10).trim();
            newpath = wlastpath2 + ref + "jpg";
            int ret = newpic();
            repaint();
            try {
              sleep(refresh * 1000);
            }
            catch (InterruptedException e) {}
            System.out.println("MESSAGE: WLASTFrame thread active.");
          }
        }     // offline
        try
        {
          sleep(2000);
        }catch(InterruptedException e){}
//        offline=false;
      }      // done
      System.out.println("MESSAGE: WLASTFrame thread stopped.");
    }
  }

  void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
  }
}
