/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package applications;

import java.util.Enumeration;
import java.util.Hashtable;
import mdot.Main;
import structures.ChargeTracker;
import structures.Constants;

/**
 *
 * @author Student
 */
public class ChargePointThread extends Thread {

    private static Hashtable<String, ChargeTracker> trackedCharges = new Hashtable<String, ChargeTracker>();

    /**
     * @return the trackedCharges
     */
    public static synchronized Hashtable<String, ChargeTracker> getTrackedCharges() {
        return trackedCharges;
    }

    /**
     * @param aTrackedCharges the trackedCharges to set
     */
    public static synchronized void setTrackedCharges(Hashtable<String, ChargeTracker> aTrackedCharges) {
        trackedCharges = aTrackedCharges;
    }

    @Override
    public void run() {

        while (true) {

            Enumeration en = trackedCharges.keys();
            while (en.hasMoreElements()) {
                String key = en.nextElement().toString();
                ChargeTracker entry = trackedCharges.get(key);

                double oldLat = entry.getLat();
                double oldLon = entry.getLon();
                double dist = entry.getDist();

                double newLat = Double.parseDouble(Main.getMe().GetObjectAsString(Constants.HASH_LATITUDE));
                double newLon = Double.parseDouble(Main.getMe().GetObjectAsString(Constants.HASH_LONGITUDE));

                double tmpDist = CommonFunctions.CommonFunctions.haversineDistance(oldLat, oldLon, newLat, newLon) / 5280;

                //add the distance
                dist += tmpDist;



                entry.setLat(newLat);
                entry.setLon(newLon);
                entry.setDist(dist);

                System.out.println("........................ENTRY: " + key + " DISTANCE: " + dist);

            }

            try {
                this.sleep(10000); //sleep 10 seconds
                System.out.println("CHARGEPOINTTHREAD ----SLEEPING FOR 10 SECONDS!");
            } catch (InterruptedException ex) {
            }
        }
    }
}
