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

import CommonFunctions.CheckFunctions;
import CommonFunctions.debugPrinter;
import database.Context;
import java.util.Enumeration;
import mdot.Main;
import structures.Constants;
import structures.HashContext;
import structures.RoadSignEntry;

/**
 *
 * @author baba
 */
public interface ActionInterface {

    public String[] innerClasses = {"InsertDynamicRoadSign", "Action2",
        "Action3", "InsertLocation"};

    public void doWork(HashContext entity);

    public String getName();

    public class InsertDynamicRoadSign implements ActionInterface {

        public void doWork(HashContext entity) {

            double dynamicSignLat = Double.parseDouble(entity.GetObjectAsString(Constants.HASH_LATITUDE));
            double dynamicSignLon = Double.parseDouble(entity.GetObjectAsString(Constants.HASH_LONGITUDE));
            String dynamicSignHeading = entity.GetObjectAsString(Constants.HASH_HEADING);
            String dynamicSignBearing = entity.GetObjectAsString(Constants.HASH_BEARING);
            String dynamicSignID = entity.GetObjectAsString(Constants.HASH_ID);


            RoadSignEntry tmp = null;

            //if the dynamic sign flag is present
            if (entity.GetObjectAsString(Constants.HASH_ROADSIGN_ENABLE) != null) {

                //check if this vehicle id is already in the vector
                Enumeration en = Context.dynamicRoadSignVector.elements();
                while (en.hasMoreElements()) {
                    RoadSignEntry tmpRoadSign = (RoadSignEntry) en.nextElement();
                    if (tmpRoadSign.getDynamicid().equals(entity.GetObjectAsString(Constants.HASH_ID))) {
                        return;
                    }
                }


                if (entity.GetObjectAsString(Constants.HASH_TYPE).equals(Constants.TYPE_SCHOOL_STRING)) {
                    tmp = new RoadSignEntry(Constants.ROADSIGN_TYPE_SCHOOL_STOP, dynamicSignLat, dynamicSignLon, dynamicSignHeading, dynamicSignBearing, "5", null, dynamicSignID);
                    Context.dynamicRoadSignVector.addElement(tmp);

                } else if (entity.GetObjectAsString(Constants.HASH_TYPE).equals(Constants.TYPE_CONSTRUCTION_STRING)) {
                    tmp = new RoadSignEntry(Constants.ROADSIGN_TYPE_CONSTRUCTION, dynamicSignLat, dynamicSignLon, dynamicSignHeading, dynamicSignBearing, "6", null, dynamicSignID);
                    Context.dynamicRoadSignVector.addElement(tmp);

                } else if (entity.GetObjectAsString(Constants.HASH_TYPE).equals(Constants.TYPE_PEDESTRIAN_STRING)) {
                    tmp = new RoadSignEntry(Constants.ROADSIGN_TYPE_PEDESTRIAN, dynamicSignLat, dynamicSignLon, dynamicSignHeading, dynamicSignBearing, "7", null, dynamicSignID);
                    Context.dynamicRoadSignVector.addElement(tmp);

                } else if (entity.GetObjectAsString(Constants.HASH_TYPE).equals(Constants.TYPE_AMBULANCE_STRING)) {
                    tmp = new RoadSignEntry(Constants.ROADSIGN_TYPE_AMBULANCE, dynamicSignLat, dynamicSignLon, dynamicSignHeading, dynamicSignBearing, "8", null, dynamicSignID);
                    Context.dynamicRoadSignVector.addElement(tmp);

                } else if (entity.GetObjectAsString(Constants.HASH_TYPE).equals(Constants.TYPE_POLICE_STRING)) {
                    tmp = new RoadSignEntry(Constants.ROADSIGN_TYPE_POLICE, dynamicSignLat, dynamicSignLon, dynamicSignHeading, dynamicSignBearing, "9", null, dynamicSignID);
                    Context.dynamicRoadSignVector.addElement(tmp);

                } else if (entity.GetObjectAsString(Constants.HASH_TYPE).equals(Constants.TYPE_FIRE_STRING)) {
                    tmp = new RoadSignEntry(Constants.ROADSIGN_TYPE_FIRE, dynamicSignLat, dynamicSignLon, dynamicSignHeading, dynamicSignBearing, "10", null, dynamicSignID);
                    Context.dynamicRoadSignVector.addElement(tmp);

                } else if (entity.GetObjectAsString(Constants.HASH_TYPE).equals(Constants.TYPE_TOW_STRING)) {
                    tmp = new RoadSignEntry(Constants.ROADSIGN_TYPE_TOW, dynamicSignLat, dynamicSignLon, dynamicSignHeading, dynamicSignBearing, "11", null, dynamicSignID);
                    Context.dynamicRoadSignVector.addElement(tmp);

                }
            }
        }

        public String getName() {
            return "InsertDynamicRoadSign";
        }
    }

    public class Action2 implements ActionInterface {

        public void doWork(HashContext entity) {
            debugPrinter.printLine("working !!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            entity.setObjectAsString("result", "emergency vehicle approaching");
        }

        public String getName() {
            return "Action2";
        }
    }

    public class Action3 implements ActionInterface {

        public void doWork(HashContext entity) {
            entity.setObjectAsString("result", "disabled vehicle ahead");
        }

        public String getName() {
            return "Action3";
        }
    }

    public class InsertLocation implements ActionInterface {

        public void doWork(HashContext tmpHash) {
            try {

                //REMOVE BEFORE STARTING
                tmpHash.removeObject(Constants.HASH_LOCATION);
                tmpHash.removeObject(Constants.HASH_LOCATION1);

                //1. Get my latitude, longitude and head. Get the other car's
                //latitude and longitude.

                String strThemLatitude = tmpHash.GetObjectAsString(Constants.HASH_LATITUDE);
                String strThemLongitude = tmpHash.GetObjectAsString(Constants.HASH_LONGITUDE);
                String strMeLatitude = Main.getMe().GetObjectAsString(Constants.HASH_LATITUDE);
                String strMeLongitude = Main.getMe().GetObjectAsString(Constants.HASH_LONGITUDE);
                String meHeading = Main.getMe().GetObjectAsString(Constants.HASH_HEADING);

                if (strThemLatitude != null && strThemLongitude != null && strMeLatitude != null && strMeLongitude != null && meHeading != null) {

                    //2. Calculate the bearing(degrees) and distance(feet)
                    double dblThemLatitude = Double.parseDouble(strThemLatitude);
                    double dblThemLongitude = Double.parseDouble(strThemLongitude);
                    double dblMeLatitude = Double.parseDouble(strMeLatitude);
                    double dblMeLongitude = Double.parseDouble(strMeLongitude);

                    double bearing = CheckFunctions.bearing(dblMeLatitude, dblMeLongitude, dblThemLatitude, dblThemLongitude);
                    double distance = CheckFunctions.haversineDistance(dblMeLatitude, dblMeLongitude, dblThemLatitude, dblThemLongitude);


                    //if the distance between 2 vehicles, we have a bigger problem than making the square red!
                    if (distance == 0) {
                        return;
                    }

                    double newGridLocation = CheckFunctions.findGridLocation(bearing, meHeading);

                    //Decide if it should be red or blue
                    String locANDcolor = null;

                    //IF I AM IN DANGER OF HITTING THE CAR IN FRONT OF ME
                    if (newGridLocation == 1) {
                        String themSpeed = tmpHash.GetObjectAsString(Constants.HASH_SPEED);
                        String meSpeed = Main.getMe().GetObjectAsString(Constants.HASH_SPEED);

                        if (themSpeed != null && meSpeed != null) {
                            double themStoppingDistance = CheckFunctions.stoppingDistance(Double.parseDouble(themSpeed));
                            double myStoppingDistance = CheckFunctions.stoppingDistance(Double.parseDouble(meSpeed));

                            debugPrinter.printLine("MyStoppingDistance: " + myStoppingDistance + "......WhereTheyStop: " + themStoppingDistance + distance);
                            if (myStoppingDistance >= (themStoppingDistance + distance)) {
                                //SHOW COLOR RED!
                                locANDcolor = newGridLocation + Constants.DELIMITER + Constants.COLOR_RED;
                            } else {
                                locANDcolor = newGridLocation + Constants.DELIMITER + Constants.COLOR_GREEN;
                            }
                        } else {
                            locANDcolor = newGridLocation + Constants.DELIMITER + Constants.COLOR_GREEN;
                        }

                    } //IF I AM IN DANGER OF BEING HIT BY THE CAR BEHIND ME
                    else if (newGridLocation == 5) {
                        String themSpeed = tmpHash.GetObjectAsString(Constants.HASH_SPEED);
                        String meSpeed = Main.getMe().GetObjectAsString(Constants.HASH_SPEED);

                        if (themSpeed != null && meSpeed != null) {
                            double themStoppingDistance = CheckFunctions.stoppingDistance(Double.parseDouble(themSpeed));
                            double myStoppingDistance = CheckFunctions.stoppingDistance(Double.parseDouble(meSpeed));

                            if (themStoppingDistance >= (myStoppingDistance + distance)) {
                                //SHOW COLOR RED!
                                locANDcolor = newGridLocation + Constants.DELIMITER + Constants.COLOR_RED;
                            } else {
                                locANDcolor = newGridLocation + Constants.DELIMITER + Constants.COLOR_GREEN;
                            }
                        } else {
                            locANDcolor = newGridLocation + Constants.DELIMITER + Constants.COLOR_GREEN;
                        }
                    } //FOR ALL OTHER LOCATIONS WE DON'T RUN INTO THIS SITUATION
                    else {
                        locANDcolor = newGridLocation + Constants.DELIMITER + Constants.COLOR_GREEN;
                    }

                    //5. Insert the location into the hashtable of THEM (COURSE and FINE)
                    if (distance < 200) {
                        tmpHash.setObjectAsString(Constants.HASH_LOCATION, locANDcolor);
                        tmpHash.setObjectAsString(Constants.HASH_LOCATION1, locANDcolor);
                    } else if (distance < 400) {
                        newGridLocation += 8;
                        locANDcolor = newGridLocation + Constants.DELIMITER + Constants.COLOR_GREEN;
                        tmpHash.setObjectAsString(Constants.HASH_LOCATION1, locANDcolor);
                    } else if (distance < 600) {
                        newGridLocation += 16;
                        locANDcolor = newGridLocation + Constants.DELIMITER + Constants.COLOR_GREEN;
                        tmpHash.setObjectAsString(Constants.HASH_LOCATION1, locANDcolor);
                    } //distance is too far, remove the keys
                    else {
                        tmpHash.removeObject(Constants.HASH_LOCATION);
                        tmpHash.removeObject(Constants.HASH_LOCATION1);
                    }

                } //Null values, remove HASH_LOCATION
                else {
                    tmpHash.removeObject(Constants.HASH_LOCATION);
                    tmpHash.removeObject(Constants.HASH_LOCATION1);
                }

            } catch (Exception ex) {
            }
        }

        public String getName() {
            return "InsertLocation";
        }
    }
}
