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

import CommonFunctions.debugPrinter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 *
 * @author baba
 */
public class HashContext {

    public Hashtable<String, Object> contextEntry = new Hashtable<String, Object>();

    public boolean ContainsKey(String key) {
        if (contextEntry.containsKey(key)) {
            return true;
        }
        return false;
    }

    //NEW!
    public Object GetObject(String key) {
        Object tmp = null;
        Object tmpObj = contextEntry.get(key);
        if (tmpObj == null) {
        } else {
            tmp = tmpObj;
        }
        return tmp;
    }

    public String GetObjectAsString(String key) {
        String tmp = null;
        Object tmpObj = contextEntry.get(key);
        if (tmpObj == null) {
        } else {
            tmp = String.valueOf(tmpObj);
            if (tmp.equals("null")) {
                tmp = null;
            }
        }
        return tmp;
    }

    public double GetObjectAsDouble(String key) {
        double tmp = -1;
        Object tmpObj = contextEntry.get(key);
        if (tmpObj == null) {
        } else {
            try {
                tmp = Double.parseDouble(String.valueOf(tmpObj));
            } catch (Exception ex) {
                //ex.printStackTrace();
                return tmp;
            }
        }

        return tmp;
    }

//    public GPSCoordinates GetObjectAsGPSCoOrdinates(String key) {
//        GPSCoordinates tmp = null;
//        Object tmpObj = contextEntry.get(key);
//        if (tmpObj == null) {
//        } else {
//            try {
//                tmp = new GPSCoordinates(String.valueOf(tmpObj));
//            } catch (Exception ex) {
//                ex.printStackTrace();
//                return tmp;
//            }
//        }
//
//        return tmp;
//    }
    public HashEntryDoubleVector GetObjectAsDoubleVector(String key) {
        HashEntryDoubleVector tmp = null;
        Object tmpObj = contextEntry.get(key);
        if (tmpObj == null) {
        } else {
            try {
                tmp = new HashEntryDoubleVector(String.valueOf(tmpObj));
            } catch (Exception ex) {
                //ex.printStackTrace();
                return tmp;
            }
        }
        return tmp;
    }

    public void setObjectAsDoubleVector(String key, String vectorValue) {
        try {
            HashEntryDoubleVector tmp = new HashEntryDoubleVector(vectorValue);
            contextEntry.put(key, tmp);
        } catch (Exception ex) {
        }
    //return tmp;
    }

     public void setObjectAsDoubleVectorwithException(String key, String vectorValue) throws Exception {
        try {
            HashEntryDoubleVector tmp = new HashEntryDoubleVector(vectorValue);
            contextEntry.put(key, tmp);
        } catch (Exception ex) {
            throw ex;
        }
    //return tmp;
    }

    public void setObjectAsDouble(String key, double someDouble) {
        //Object tmpObj = someDouble;
        contextEntry.put(key, someDouble);
    }

    public void setObjectAsString(String key, String someString) {
        ///Object tmpObj = someString;
        contextEntry.put(key, someString);
    }

    public void AppendToVector(String key, String value) {
        String vector = (String) contextEntry.get(key);
        if (vector != null) {
            vector = vector + Constants.DELIMITER + value;
        //vector.AddToVector(value);

        }
    }

    public double GetAverageOfDoubleVector(String key) {
        double average = -1;
        try {
            HashEntryDoubleVector vectorDouble = new HashEntryDoubleVector((String) contextEntry.get(key));
            if (vectorDouble != null) {
                average = vectorDouble.average;
            }
        } catch (Exception ex) {
        }
        return average;
    }

    public double GetStdDeviationOfDoubleVector(String key) {
        double deviation = -1;
        try {
            HashEntryDoubleVector vectorDouble = new HashEntryDoubleVector((String) contextEntry.get(key));
            if (vectorDouble != null) {
                deviation = vectorDouble.stdDeviation;
            }
        } catch (Exception ex) {
        }
        return deviation;
    }

    public double GetCustomAverageOfDoubleVector(String key, int minutes) {
        double customAverage = -1;
        try {
            HashEntryDoubleVector vectorDouble = new HashEntryDoubleVector((String) contextEntry.get(key));
            if (vectorDouble != null) {
                customAverage = vectorDouble.GetCustomAverage(minutes);
            }
        } catch (Exception ex) {
        }
        return customAverage;
    }

    public void replaceObject(String key, Object obj) {
        contextEntry.remove(key);
        contextEntry.put(key, obj);
    }

    public void replaceObjectAsString(String key, String obj) {
        try{
        contextEntry.remove(key);
        }catch(Exception ex){
            
        }
        contextEntry.put(key, obj);
    }

    public void removeObject(String key) {
        contextEntry.remove(key);
    }

    public Enumeration<String> keys() {
        return contextEntry.keys();
    }

    @Override
    public String toString() {
        String tmp = "";
        Enumeration<String> enkeys = contextEntry.keys();
        int icount = 0;
        while (enkeys.hasMoreElements()) {
            String tmpKey = enkeys.nextElement();
            Object tmpvalue = contextEntry.get(tmpKey);
            String className = tmpvalue.getClass().getName();
            String scalarVector = "";
            String type = "";
            if ((!className.equals(HashEntryDoubleVector.className))) {
                scalarVector = Constants.SCALAR;
                try {
                    double tmpInt = Double.parseDouble(tmpvalue.toString());
                    type = Constants.TYPE_DOUBLE;
                } catch (Exception ex) {
                    type = Constants.TYPE_STRING;
                }
            } else {
                scalarVector = Constants.VECTOR;
                type = Constants.TYPE_DOUBLE;
            }
            if (icount == 0) {
                tmp = tmpKey + Constants.DELIMITER_SINGLE_ENTRY + scalarVector + Constants.DELIMITER_SINGLE_ENTRY + type + Constants.DELIMITER_SINGLE_ENTRY + tmpvalue.toString() + Constants.DELIMITER_ENTRY;
            } else if (icount == contextEntry.size() - 1) {
                tmp = tmp + tmpKey + Constants.DELIMITER_SINGLE_ENTRY + scalarVector + Constants.DELIMITER_SINGLE_ENTRY + type + Constants.DELIMITER_SINGLE_ENTRY + tmpvalue.toString();
            } else {
                tmp = tmp + tmpKey + Constants.DELIMITER_SINGLE_ENTRY + scalarVector + Constants.DELIMITER_SINGLE_ENTRY + type + Constants.DELIMITER_SINGLE_ENTRY + tmpvalue.toString() + Constants.DELIMITER_ENTRY;
            }
            icount++;
        }
        return tmp;
    }
}
