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

import CommonFunctions.CommonFunctions;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import mdot.Main;
import structures.Constants;

/**
 *
 * @author student
 */
public class CarbonEmissions extends Thread {

    
    public boolean start = false;
    private Trip myTrip = new Trip();

    @Override
    public void run() {


        int tick;
        double newLat = 0;
        double newLon = 0;
        double milesTraveled = 0;
        double distance = 0;
        double totalMPG = 0;
        double averageMPG = 0;
        double count = 1;
        double gallonsused = 0;
        double carbonemission = 0;
        double lat = Main.getMe().GetObjectAsDouble(Constants.HASH_LATITUDE);
        double lon = Main.getMe().GetObjectAsDouble(Constants.HASH_LONGITUDE);

        //Set the date for the trip
        String DATE_FORMAT = "MM/dd/yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        myTrip.setDate(sdf.format(Calendar.getInstance().getTime()));

        DecimalFormat twoDForm = new DecimalFormat("#.###");

        while (start) {
        //System.out.println("RUNNING");
            for (tick = 1; tick < 10; tick++) {

                totalMPG += CommonFunctions.calculateMPG();
                averageMPG = totalMPG / count;
                myTrip.setAverageMPG(Double.parseDouble(twoDForm.format(averageMPG)));
                count++;

                try {
                    this.sleep(1000); //sleep 1 second
                } catch (InterruptedException ex) {
                }
            }

            //do gps stuff there - once every 10 seconds
            newLat = Main.getMe().GetObjectAsDouble(Constants.HASH_LATITUDE);
            newLon = Main.getMe().GetObjectAsDouble(Constants.HASH_LONGITUDE);

           
            distance = (CommonFunctions.haversineDistance(lat, lon, newLat, newLon))/5280;
            System.out.println("Distance: " + distance);
            milesTraveled += distance;
            myTrip.setMilesTraveled(Double.parseDouble(twoDForm.format(milesTraveled)));

            lat = newLat;
            lon = newLon;

            //calculate gallons used
            System.out.println("MilesTraveled: " + milesTraveled + " averageMPG: " + averageMPG);
            gallonsused = milesTraveled/averageMPG;
            if(Double.isNaN(gallonsused) || Double.isInfinite(gallonsused))
                gallonsused = 0.0;

            System.out.println("Gallons Used: " + gallonsused);
            myTrip.setGallonsused(Double.parseDouble(twoDForm.format(gallonsused)));

            //calculate carbon footprint
            carbonemission = gallonsused * 19.4;
            myTrip.setCarbonemission(Double.parseDouble(twoDForm.format(carbonemission)));


        }
    }

    /**
     * @return the myTrip
     */
    public Trip getMyTrip() {
        return myTrip;
    }

    /**
     * @param myTrip the myTrip to set
     */
    public void setMyTrip(Trip myTrip) {
        this.myTrip = myTrip;
    }
}
