/*
 * ChargePointSelection.fx
 *
 * Created on May 11, 2010, 11:40:07 AM
 */
package mdot_gui;

import CommonFunctions.debugPrinter;
import mdot.Main;
import structures.Constants;
import database.Context;
import java.util.Enumeration;
import structures.ChargePointEntry;
import structures.ChargesEntry;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import structures.ChargeTracker;
import applications.ChargePointThread;
import java.text.DecimalFormat;
import client.TMCWebServiceClient;

/**
 * @author Student
 */
public class ChargePointSelection {

        }
public function CheckChargePoints() {
    var meLon: Double;
    var meLat: Double;
    var tmpLat: String;
    var tmpLon: String;
    //initialize variables
    tmpLat = Main.getMe().GetObjectAsString(Constants.HASH_LATITUDE);
    tmpLon = Main.getMe().GetObjectAsString(Constants.HASH_LONGITUDE);
    //RETURN BLANK?
    if (tmpLat != null and tmpLon != null) {
        meLat = Double.parseDouble(Main.getMe().GetObjectAsString(Constants.HASH_LATITUDE));
        meLon = Double.parseDouble(Main.getMe().GetObjectAsString(Constants.HASH_LONGITUDE));

        } else {
        debugPrinter.printLine("GPS Error: Null value of Latitude, Longitude, Heading or Bearing");
        return ;
        }
    //Check the ChargePoints
    var enChargePoints: Enumeration = Context.chargePointVector.elements();
    while (enChargePoints.hasMoreElements()) {

        //get the chargepoint
        var chargePoint: ChargePointEntry = (enChargePoints.nextElement() as ChargePointEntry);
        var chargePointLat: Double = chargePoint.getLat();
        var chargePointLon: Double = chargePoint.getLon();
        var chargePointID: String = chargePoint.getId();
        var chargePointType: String = chargePoint.getType();
        var chargePointPrice: String = chargePoint.getPrice();
        var chargePointSeq:String = chargePoint.getSequence();

        var key: String = chargePointID;
        var value: String = "{chargePointLat};{chargePointLon}";


        //check the distance between ME and the chargepoint
        var dist: Double = CommonFunctions.CommonFunctions.haversineDistance(meLat, meLon, chargePointLat, chargePointLon);

        //in a 50 foot radius
        if (dist <= 50) {
            //Check if this charge point entry is in the HashTable

            //if there is no entry, charge
            if (database.Context.chargedHashTable.get(key) == null) {

                //charge -- send to database AND store locally
                var charge: ChargesEntry = new ChargesEntry();

                //Set the date for the trip
                var DATE_FORMAT: String = "MM/dd/yyyy";
                var sdf: SimpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
                charge.setDate(sdf.format(Calendar.getInstance().getTime()));
                charge.setLat(chargePointLat);
                charge.setLon(chargePointLon);
                charge.setType(chargePointType);
                charge.setPrice(chargePointPrice);

                //Don't charge for TollRoads immediately
                //Have to calculate the final charge
                if (not chargePoint.getType().equals("Toll Road")) {
                    Context.chargedVector.add(charge);
                    try{
                    MainScreen.client.chargeVehicle(Main.meString, chargePointType, Double.parseDouble(chargePointPrice), 0.0, Double.parseDouble(chargePointPrice));
                    }
                    catch(Exception){
                            java.lang.System.out.println("ERROR SUBMITTING CHARGE!!");
                            }
                    java.lang.System.out.println("CHARGED!--------------");
                    database.Context.chargedHashTable.put(key, value);
                } else {
                    //Start keeping track of distance
                    if(chargePointSeq.equals("Start")){
                        var mycharge: ChargeTracker = new ChargeTracker(Double.parseDouble(tmpLat), Double.parseDouble(tmpLon), 0.0);
                        ChargePointThread.getTrackedCharges().put(key, mycharge);
                        database.Context.chargedHashTable.put(key, value);
                    }
                }

                }
            //There is already an entry with this key
            else {

                //Compare values to see if we are still be the same point or a different one
                var entry = database.Context.chargedHashTable.get(key);

                //if entry equals the value we are still at the same point
                if (not entry.equals(value)) {
                    //We have reached the corresponding end point
                    //remove entry from the hashtable
                    database.Context.chargedHashTable.remove(key);

                    var twoDForm:DecimalFormat = new DecimalFormat("#.##");
                    var chargeData: ChargeTracker = ChargePointThread.getTrackedCharges().get(key);
                    var tmpCharge: Double = Double.parseDouble(chargePoint.getPrice());
                    var totalCharge:String = twoDForm.format(chargeData.getDist() * tmpCharge);



                    //sum up the charge
                    //send to TMC
                    //put into chargedVector
                    var charge: ChargesEntry = new ChargesEntry();

                    //Set the date for the trip
                    var DATE_FORMAT: String = "MM/dd/yyyy";
                    var sdf: SimpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
                    charge.setDate(sdf.format(Calendar.getInstance().getTime()));
                    charge.setLat(chargePointLat);
                    charge.setLon(chargePointLon);
                    charge.setType(chargePoint.getType());
                    charge.setPrice(totalCharge);

                    Context.chargedVector.add(charge);
                    try{
                    MainScreen.client.chargeVehicle(Main.meString, chargePointType, Double.parseDouble(chargePointPrice), chargeData.getDist(), Double.parseDouble(totalCharge));
                    }
                    catch(Exception){
                            java.lang.System.out.println("ERROR SUBMITTING CHARGE!!");
                            }
                    ChargePointThread.getTrackedCharges().remove(key)

                    }
                }
                
            }
            //Distance > 50 type is NOT toll Road
            else{
                  if(not chargePointType.equals("Toll Road"))
                    database.Context.chargedHashTable.remove(key);
            }
        }
    }


