package com.algobase.share.geo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import java.util.Locale;

import android.location.Location;



   public class WayPoint {

      String name;
      String date;
      String time;
/*
      double lon;
      double lat;
      double alt;
*/
      Location loc;



      public WayPoint()
      { this.name = "";
        this.date = "";
        this.time = "";
/*
        this.lon = 0;
        this.lat = 0;
        this.alt = 0;
*/
        this.loc = new Location("waypoint");
      }

      public WayPoint(String name, String date, String time,
                      double lon, double lat, double alt)
      { this.name = name;
        this.date = date;
        this.time = time;
/*
        this.lon = lon;
        this.lat = lat;
        this.alt = alt;
*/
        this.loc = new Location("waypoint");
        this.loc.setLongitude(lon);
        this.loc.setLatitude(lat);
        this.loc.setAltitude(alt);
      }

      public String getName() { return name; }

      public Location getLocation()  { return loc; }
      public double   getAltitude()  { return loc.getAltitude(); }
      public double   getLongitude() { return loc.getLongitude(); }
      public double   getLatitude()  { return loc.getLatitude(); }


      public void read(File file)
      { try {
         BufferedReader reader = new BufferedReader(new FileReader(file));
         String line = reader.readLine();
         String parts[] = line.split(" ");
         date = parts[0];
         time = parts[1];
         name = parts[2].replaceAll("~"," ");
         double lon = Float.parseFloat(parts[3]);
         double lat = Float.parseFloat(parts[4]);
         double alt = Float.parseFloat(parts[5]);
         loc = new Location("waypoint");
         loc.setLongitude(lon);
         loc.setLatitude(lat);
         loc.setAltitude(alt);
         reader.close();
        } catch (Exception e) {}
      }

     public void write(File file)
     { 
       String s = name.replaceAll(" ","~");

       double lon = loc.getLongitude();
       double lat = loc.getLatitude();
       double alt = loc.getAltitude();
       String txt = String.format(Locale.US, "%s %s %s %.5f %.5f %.1f", 
                                             date, time, s, lon, lat, alt);
       try {  
         BufferedWriter writer = new BufferedWriter(new FileWriter(file,false));
         writer.write(txt);
         writer.newLine();
         writer.close();
       } catch(Exception e){}
     }

   };

