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

import CommonFunctions.debugPrinter;
import database.Context;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import structures.Constants;
import structures.HashContext;
import structures.RoadSignEntry;

/**
 *
 * @author baba
 */
public interface ConditionInterface {
    //The user will have to implement these two functions
    public Vector<HashContext> CheckCondition(Hashtable<String, HashContext> context, HashContext mySelf);

    public String getName();
    //Take care in the second method "getName()". It returns you the name of the class not the method

    public class Condition1 implements ConditionInterface {

        public Vector<HashContext> CheckCondition(Hashtable<String, HashContext> context, HashContext mySelf) {
            Vector<HashContext> tmpVector = new Vector<HashContext>();
            HashContext tmp = null;
            Enumeration<HashContext> en = context.elements();

            while (en.hasMoreElements()) {
                HashContext tmpHash = en.nextElement();
                double me_Speed = mySelf.GetObjectAsDouble(Constants.HASH_SPEED);
                double vehicle_speed = tmpHash.GetObjectAsDouble(Constants.HASH_SPEED);
                if (me_Speed != -1 && vehicle_speed != -1) {
                    if (Math.abs(me_Speed - vehicle_speed) >= 50) {
                        debugPrinter.printLine("Condition1 is satisfied------------------------------------");
                        tmp = tmpHash;
                        tmpVector.add(tmpHash);
                    }
                }

            }
            return tmpVector;
        }

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

    public class Condition2 implements ConditionInterface {

        public Vector<HashContext> CheckCondition(Hashtable<String, HashContext> context, HashContext mySelf) {
            Vector<HashContext> tmpVector = new Vector<HashContext>();
            HashContext tmp = null;
            Enumeration<HashContext> en = context.elements();
            while (en.hasMoreElements()) {
                HashContext tmpHash = en.nextElement();
                double vehicle_speed = tmpHash.GetObjectAsDouble(Constants.HASH_SPEED);
                if (vehicle_speed != -1) {
                    if (vehicle_speed == 25) {
                        debugPrinter.printLine("Condition2 is satisfied------------------------------------");
                        tmp = tmpHash;
                        tmpVector.add(tmpHash);
                    }
                }
            }
            return tmpVector;
        }

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

    public class Condition3 implements ConditionInterface {

        public Vector<HashContext> CheckCondition(Hashtable<String, HashContext> context, HashContext mySelf) {
            Vector<HashContext> tmpVector = new Vector<HashContext>();
            HashContext tmp = null;
            Enumeration<HashContext> en = context.elements();
            while (en.hasMoreElements()) {
                HashContext tmpHash = en.nextElement();
                double vehicle_type = tmpHash.GetObjectAsDouble(Constants.HASH_TYPE);
                if (vehicle_type != -1) {

                    //If there is a vehicle is an ambulance
                    if (vehicle_type == Constants.TYPE_AMBULANCE) {
                        tmp = tmpHash;
                        tmpVector.add(tmpHash);
                    }
                }
            }
            if (tmpVector.size() != 0) {
                debugPrinter.printLine("Condition3 is satisfied------------------------------------");
            } else {
                debugPrinter.printLine("Condition3 is not satisfied------------------------------------");
            }
            return tmpVector;
        }

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

    public class Condition4 implements ConditionInterface {

        public Vector<HashContext> CheckCondition(Hashtable<String, HashContext> context, HashContext mySelf) {
            Vector<HashContext> tmpVector = new Vector<HashContext>();
            HashContext tmp = null;
            Enumeration<HashContext> en = context.elements();
            while (en.hasMoreElements()) {
                HashContext tmpHash = en.nextElement();
                double vehicle_loc = tmpHash.GetObjectAsDouble(Constants.HASH_LOCATION);
                if (vehicle_loc != -1) {

                    //If there is a vehicle to my back
                    if (vehicle_loc == Constants.BACK_ALL) {
                        tmp = tmpHash;
                        tmpVector.add(tmpHash);
                    }
                }
            }
            if (tmpVector.size() != 0) {
                debugPrinter.printLine("Condition4 is satisfied------------------------------------");
            } else {
                debugPrinter.printLine("Condition4 is not satisfied------------------------------------");
            }
            return tmpVector;
        }

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

    public class Condition5 implements ConditionInterface {

        public Vector<HashContext> CheckCondition(Hashtable<String, HashContext> context, HashContext mySelf) {
            Vector<HashContext> tmpVector = new Vector<HashContext>();
            HashContext tmp = null;
            Enumeration<HashContext> en = context.elements();
            while (en.hasMoreElements()) {
                HashContext tmpHash = en.nextElement();
                double vehicle_cond = tmpHash.GetObjectAsDouble(Constants.HASH_CONDITION);
                if (vehicle_cond != -1) {

                    //If there is a vehicle to my right
                    if (vehicle_cond == Constants.CONDITION_ABNORMAL) {

                        //System.out.println("Condition= " + vehicle_cond);
                        tmp = tmpHash;
                        tmpVector.add(tmpHash);
                    }
                }
            }
            if (tmpVector.size() != 0) {
                debugPrinter.printLine("Condition5 is satisfied------------------------------------");
            } else {
                debugPrinter.printLine("Condition5 is not satisfied------------------------------------");
            }
            return tmpVector;
        }

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

    public class Condition6 implements ConditionInterface {

        public Vector<HashContext> CheckCondition(Hashtable<String, HashContext> context, HashContext mySelf) {
            Vector<HashContext> tmpVector = new Vector<HashContext>();
            HashContext tmp = null;
            Enumeration<HashContext> en = context.elements();
            while (en.hasMoreElements()) {
                HashContext tmpHash = en.nextElement();
                double vehicle_loc = tmpHash.GetObjectAsDouble(Constants.HASH_LOCATION);
                if (vehicle_loc != -1) {

                    //If there is a vehicle to my right
                    if (vehicle_loc <= Constants.FRONT_ALL) {
                        tmp = tmpHash;
                        tmpVector.add(tmpHash);
                    }
                }
            }
            if (tmpVector.size() != 0) {
                debugPrinter.printLine("Condition6 is satisfied------------------------------------");
            } else {
                debugPrinter.printLine("Condition6 is not satisfied------------------------------------");
            }
            return tmpVector;
        }

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

    //Searches the context for vehicles with the type school bus or construction cone
    public class CheckDynamicRoadSign implements ConditionInterface {

        public Vector<HashContext> CheckCondition(Hashtable<String, HashContext> context, HashContext mySelf) {
            Vector<HashContext> tmpVector = new Vector<HashContext>();
            Enumeration<HashContext> en = context.elements();

            while (en.hasMoreElements()) {
                HashContext tmpHash = en.nextElement();

                String vehicle_type = tmpHash.GetObjectAsString(Constants.HASH_TYPE);
                if (vehicle_type != null) {
                    if (    vehicle_type.equals(Constants.TYPE_SCHOOL_STRING) ||
                            vehicle_type.equals(Constants.TYPE_CONSTRUCTION_STRING) ||
                            vehicle_type.equals(Constants.TYPE_AMBULANCE_STRING) ||
                            vehicle_type.equals(Constants.TYPE_POLICE_STRING) ||
                            vehicle_type.equals(Constants.TYPE_FIRE_STRING) ||
                            vehicle_type.equals(Constants.TYPE_PEDESTRIAN_STRING) ||
                            vehicle_type.equals(Constants.TYPE_TOW_STRING)) {
                        tmpVector.add(tmpHash);
                    }

                    //check dynamic signs
                    Enumeration<RoadSignEntry> enDynamic = Context.dynamicRoadSignVector.elements();
                    while(enDynamic.hasMoreElements())
                    {
                        RoadSignEntry tmpRoadSign = enDynamic.nextElement();
                        if(tmpRoadSign.getDynamicid().equals(tmpHash.GetObjectAsString(Constants.HASH_ID)))
                        {
                            Context.dynamicRoadSignVector.remove(tmpRoadSign);
                            break;
                        }

                    }
                }

            }
            return tmpVector;
        }

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

    public class CheckLocation implements ConditionInterface {

        public Vector<HashContext> CheckCondition(Hashtable<String, HashContext> context, HashContext mySelf) {
            Vector<HashContext> tmpVector = new Vector<HashContext>();
            Enumeration<HashContext> en = context.elements();

            while (en.hasMoreElements()) {
                HashContext tmpHash = en.nextElement();

                //String type = tmpHash.GetObjectAsString(Constants.HASH_TYPE);
                //we don't want pedestrians to blink on the screen
                //if(!type.equals(Constants.TYPE_PEDESTRIAN_STRING))
                    tmpVector.add(tmpHash);

            }
            return tmpVector;
        }

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

