// ChartThread.java
//
// Copyright 1998 & 1999 Henrik Bjorkman. All rights reserved. 
// No responsibilities taken. Use on your own risk. Etc...
//
//
// History
//
// 1999-08-31 
// Created by Henrik Bjorkman
//
// 2000-06-04
// Added function "readTrace(String fileName)" 
// Henrik
//
// 2000-06-13
// Changed package name to chartplotter_package.

package chartplotter_package;

import java.io.*;
import java.awt.*;
import java.text.*;


public class ChartTrace
{
  Pos[] trace=new Pos[4320];
  int traceIndex=0;
  public String log_file_name="ChartPlotter.log";
  java.util.Date startDate=new java.util.Date();

  static void error(String s)
  {
    System.err.println("ChartThread: " + s);
    System.exit(0);
  }

  static void debug(String s)
  {
    //System.out.println("ChartThread: " + s);
  }

  public void plotTrace(ChartWindow w, ChartData d)
  {
    int index=traceIndex;

    // Send trace positions.
    for(int i=0;i<trace.length;i++)
    {
      if (++index>=trace.length) {index=0;}
      if (trace[index]!=null)
      {
        Point p=d.transPos(trace[index]);
        w.getChartComp().tracePos(p);
      }
    }
  }

  public void plotTrace(ChartComp w, ChartData d)
  {
    int index=traceIndex;

    // Send trace positions.
    for(int i=0;i<trace.length;i++)
    {
      if (++index>=trace.length) {index=0;}
      if (trace[index]!=null)
      {
        Point p=d.transPos(trace[index]);
        w.tracePos(p);
      }
      else
      {
        w.tracePos(null);
      }
    }
  }


  public void saveTrace()
  {
    java.util.Date stopDate=new java.util.Date();

    if (log_file_name.length()>0)
    {
      System.out.println("Saving log file: "+log_file_name);
      try
      {
        FileWriter out=new FileWriter(log_file_name,true);

        out.write("start "+startDate+"\n");

        // Save trace positions.
        for(int i=0;i<trace.length;i++)
        {
          if (++traceIndex>=trace.length) {traceIndex=0;}
          if (trace[traceIndex]!=null)
          {
            String s=trace[traceIndex].toString();
            out.write(s+"\n");
          }
        }
        out.write("stop "+stopDate+"\n");

        out.close();
      }
      catch (IOException e) {error(""+e); }
    }
  }


  public void readTrace(String fileName)
  {
    if (fileName.length()>0)
    {
      System.out.println("Loading log file: "+fileName);
      try
      {
        FileReader in=new FileReader(fileName);
        readTrace(new BufferedReader(in));
      }
      catch (IOException e) {error(""+e); }
    }
  }


  public void readTrace(BufferedReader in) throws IOException
  {
    debug("load trace file");

    while(true)
    {
      String str=in.readLine();
      if (str==null) break;

      //debug("read: "+str);

      if ((str.length()==0) || (str.startsWith("//"))) {;}
      else if ((str.startsWith("start"))||(str.startsWith("stop")))
      {
        debug("start/stop");
        logPos(null);
      }
      else
      {
        Pos pos=new Pos();
        pos.lat=ReferencePoint.parseLatLng(ChartWord.getWord(str,1),'N','S');
        pos.lng=ReferencePoint.parseLatLng(ChartWord.getWord(str,2),'E','W');

        logPos(pos);
      }
    }
  }


  public void logPos(Pos pos)
  {
    trace[traceIndex]=pos;
    if (++traceIndex>=trace.length) {traceIndex=0;}
    trace[traceIndex]=null;
  }
}




