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 RecentFrame extends JFrame {
  String recentpath1;
  String recentpath2;
  String recenttitle;
  int refresh;

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


  public static recent1 t1;
  private Image offScreenImg;
  private Graphics offScreenG;
  Toolkit toolkit = Toolkit.getDefaultToolkit();
  boolean done=false;
  boolean offline=false;
  Image image1;
  Image image2;
  Image scaled1;
  Image scaled2;
  MediaTracker tracker1 = new MediaTracker(this);
  MediaTracker tracker2 = new MediaTracker(this);
  String inpline=" ";
  String newpath;
  String data;
  String date;
  String tid;
  String latitude;
  String longitude;
  String depth;
  String magnitude;

  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 RecentFrame(String recentpath1,String recentpath2,String recenttitle, int ref) {
    this.recentpath1=recentpath1;
    this.recentpath2=recentpath2;
    this.recenttitle=recenttitle;
    this.refresh=ref;

    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);
       }
    });

    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }


  }
  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("LAST10 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("LAST10 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.RECENT=false;
  }
  public void CrtImg()
  {
    offScreenImg = createImage(getSize().width, getSize().height);
    offScreenG = offScreenImg.getGraphics();
  }
  public void StartRECENT()
  {
    t1=new recent1();
    t1.start();
  }

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

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



  }
  public void paint(Graphics g)
  {

    if(scaled1 !=null)
    {
      g.drawImage(scaled1, 0, ytitle+20, this);
      image1.flush();
    }
      xx=scrwidth/2-210;
      yy= ytitle+20;
      g.draw3DRect(xx,yy,210-2*xmarg,scrheight/2-70,false);
      g.setColor(Color.white);
      g.fill3DRect(xx+2,yy+2,210-2*xmarg-2,scrheight/2-70-2,false);
      g.setColor(Color.BLACK);
      g.drawString("Source: Department of Earth Science",10,42);

      g.drawString("Date:             "+date,xx+xmarg,80);
      g.drawString("Time (GMT): "+tid,xx+xmarg,100);
      g.drawString("Latitude:      "+latitude,xx+xmarg,120);
      g.drawString("Longitude:  "+longitude,xx+xmarg,140);
      g.drawString("Depth:         "+depth+" km",xx+xmarg,160);
      g.drawString("Magnitude:  "+magnitude,xx+xmarg,180);

  }
  class recent1 extends Thread
  {
    public void run()
    {
      while(!done)
      {
        while(!offline)
        {

        try
        {
          URL recentURL = new URL(recentpath1);
          InputStreamReader inpStream = new InputStreamReader(recentURL.openStream());
          BufferedReader br = new BufferedReader(inpStream);
          for(int i=0;i<12;i++)
          {
            inpline = br.readLine();
//            System.out.println(i+"  "+inpline);
          }
          br.close();
        } catch (IOException ex2)
        {
          System.out.println(" Received Exception IOException: " + ex2);
        }
        int p=0;
        for(p=0;p<60;p++)
        {
          String find=inpline.substring(p,p+5);
          int ret=find.compareTo("href=");
          if(ret==0)
            break;
        }
        String ref=inpline.substring(p+6,p+24);
//System.out.println("ref : "+ref);


        data=inpline.substring(p+30,inpline.length());
//System.out.println("data: "+data);
        date=data.substring(0,10);
        tid=data.substring(11,21);
        latitude=data.substring(23,30);
        longitude=data.substring(33,40);
        depth=data.substring(41,46);
        magnitude=data.substring(46,50);
        newpath=recentpath2+ref+"gif";
//System.out.println("NEWPATH: "+newpath);
          newpic();
          repaint();
          try
          {
            sleep(refresh*1000);
          }catch(InterruptedException e){}
          System.out.println("MESSAGE: RECENT thread active.");
        }
        try
        {
          sleep(20000);
        }catch(InterruptedException e){}
        offline=false;
      }
      System.out.println("MESSAGE: RECENT thread stopped.");
    }
  }

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