/*
 * ImageSelectionCriteria.fx
 *
 * Created on Dec 5, 2009, 8:46:44 PM
 */

package mdot_gui;

import mdot.Main;
import structures.Constants;
import database.Context;
import structures.RoadSignEntry;
import java.util.Enumeration;
import javafx.util.Sequences;
import CommonFunctions.debugPrinter;


/**
 * @author student
 */

public class ImageSelectionCriteria {


}

public function ReturnSigns(){

    var selectedSigns:String[];
    var tmpStaticSigns:RoadSignEntry[];
    var tmpDynamicSigns:RoadSignEntry[];

    var meLon:Double;
    var meLat:Double;
    var meHeading:String;
    var meBearing:String;
    var tmpLat:String;
    var tmpLon:String;
    var tmpHeading:String;
    var tmpBearing:String;

    //initialize variables
    tmpLat = Main.getMe().GetObjectAsString(Constants.HASH_LATITUDE);
    tmpLon = Main.getMe().GetObjectAsString(Constants.HASH_LONGITUDE);
    tmpHeading = Main.getMe().GetObjectAsString(Constants.HASH_HEADING);
    tmpBearing = Main.getMe().GetObjectAsString(Constants.HASH_BEARING);


    //RETURN BLANK?
    if(tmpLat != null and tmpLon!= null and tmpHeading != null and tmpBearing != null)
    {
        meLat = Double.parseDouble(Main.getMe().GetObjectAsString(Constants.HASH_LATITUDE));
        meLon = Double.parseDouble(Main.getMe().GetObjectAsString(Constants.HASH_LONGITUDE));
        meHeading = Main.getMe().GetObjectAsString(Constants.HASH_HEADING);
        meBearing = Main.getMe().GetObjectAsString(Constants.HASH_BEARING);
    }
    else{
        debugPrinter.printLine("GPS Error: Null value of Latitude, Longitude, Heading or Bearing");
        //meLat = 42.251896533;
        //meLon = -85.641268281;
        //meBearing = "0.0";

        return ["blank_lg","blank_lg","blank_lg","blank_lg"];
    }

    

    //Check the Dynamic Signs
    var enDynamic:Enumeration = Context.dynamicRoadSignVector.elements();
    while (enDynamic.hasMoreElements())
    {
         //get the sign
         var dynamicSign:RoadSignEntry = (enDynamic.nextElement() as RoadSignEntry);
         var dynamicSignLat = dynamicSign.getLat();
         var dynamicSignLon = dynamicSign.getLon();


         //check the distance between ME and the sign
         var dist:Double = CommonFunctions.CommonFunctions.haversineDistance(meLat, meLon, dynamicSignLat, dynamicSignLon);

         //in a 400 foot radius
         if(dist < 400)
            insert dynamicSign into tmpDynamicSigns;


    }

    //Sort signs by priority and insert into selectedSigns
    if(tmpDynamicSigns.size != 0)
    {
        var sortedDynamicSigns = Sequences.sort(tmpDynamicSigns);
        for(x in sortedDynamicSigns)
            insert (x as RoadSignEntry).getType().toString().concat("_lg") into selectedSigns;
    }

    //Check the static signs
    var en:Enumeration = Context.staticRoadSignVector.elements();
    while (en.hasMoreElements())
    {

        var sign:RoadSignEntry = (en.nextElement() as RoadSignEntry);

        //get the lat and lon of the sign
        var signLat:Double = sign.getLat();
        var signLon:Double = sign.getLon();
        var signHeading:String = sign.getHeading();
        var signBearing:Double = Double.parseDouble(sign.getBearing());

        //only get signs that are facing the same way as us...
        //this saves us from looking through ALL the signs

        //get a degree range to look in
        var bearingRangeMin = ((Double.parseDouble(meBearing) - Constants.GPS_DEGREE_RANGE) + 360) mod 360;
        var bearingRangeMax = ((Double.parseDouble(meBearing) + Constants.GPS_DEGREE_RANGE) + 360) mod 360;

        //example: if MAX = 10 and Min = 350
        if(bearingRangeMax < bearingRangeMin)
        {
            bearingRangeMax += 360;

            if(signBearing <= Constants.GPS_DEGREE_RANGE)
                signBearing += 360;
        }

/*
        java.lang.System.out.println("------------------------------------------");
        java.lang.System.out.println("MyBearing: {meBearing}");
        java.lang.System.out.println("BearingMIN: {bearingRangeMin}       BearingMAX: {bearingRangeMax}");
        debugPrinter.printLine("{sign.getType()}.....{signBearing}");
        java.lang.System.out.println("------------------------------------------");*/
       

        if(signBearing <= bearingRangeMax and signBearing >= bearingRangeMin){
            //get the bearing from ME to the sign
            var bearing:Double = CommonFunctions.CommonFunctions.bearing(meLat, meLon, signLat, signLon);

            //if the sign is in front of us
            var loc = CommonFunctions.CommonFunctions.findGridLocation(bearing, meHeading);

            if(loc == 8 or loc == 1 or loc==2){

                //find the distance between ME and the sign
                var dist2:Double = CommonFunctions.CommonFunctions.haversineDistance(meLat, meLon, signLat, signLon);

                var mySpeed = Main.getMe().GetObjectAsDouble(Constants.HASH_SPEED);
                if(mySpeed == -1)
                    mySpeed = 30;   //If we don't know the car's speed,
                                    //assume se are going x MPH

                //Make the sign range a function of speed:
                //mySpeed * (1.46 changes mph to fps) * 5 seconds
                var range = mySpeed * 1.46 * 5;

                //debugPrinter.printLine("{sign.getType()}.....{signHeading}..............{dist2}:{range}");

                if(dist2 < range)
                    insert sign into tmpStaticSigns;
            }
        }
    }

    /*
     for(x in tmpStaticSigns)
     {
             debugPrinter.printLine("{x.getPriority()}{x.getType()}");

     }*/

    //Sort signs by priority and insert into selectedSigns
     var sortedStaticSigns = Sequences.sort(tmpStaticSigns);
     for(x in sortedStaticSigns)
     {
        if((x as RoadSignEntry).getType().equals("speedlimit"))
        {
            var tmp:RoadSignEntry = x as RoadSignEntry;
            var speed = tmp.getSpeedlimit();
            var speedsign = "speedlimit{speed}_lg";
            insert speedsign into selectedSigns;
        }
        else
            insert (x as RoadSignEntry).getType().toString().concat("_lg") into selectedSigns;
     }

     //If there aren't enough signs selected, fill opening with blanks
     if(sizeof selectedSigns < 4)
     {
        var size = sizeof selectedSigns;
        var numtoInsert = 4-size;
        for(x in [1..numtoInsert])
        {
            insert "blank_lg" into selectedSigns;
        }
     }

    //selectedSigns[0] = "stop_lg";
    //selectedSigns[1] = "speedlimit15_lg";
    //return the list of signs
    return selectedSigns;
    }

