package natlab;
import java.util.*;
import java.io.*;

import natlab.toolkits.filehandling.FunctionOrScriptQuery;
import ast.*;

public class LookupFile{ 
    private static HashMap<String, String> builtinClasses = initialize();
    private static HashMap<String, String> builtinFunctions;
    private static HashMap<String, String> builtinPackages;
    private static HashMap<String, String> outputInfo;
    private static HashMap<String, Function> currentFile;
    private static HashMap<String, ASTNode> lib; 
    private static HashMap<String, String> initialize(){
	try{
	    InputStream fin=LookupFile.class.getResourceAsStream("builtin.ser");
	    ObjectInputStream oin = new ObjectInputStream(fin);
	    HashMap<String, HashMap<String, String>> map = (HashMap<String, HashMap<String, String>>) oin.readObject();
	    builtinPackages = map.get("packages");
	    builtinClasses = map.get("classes");
	    builtinFunctions = map.get("functions");
	    outputInfo = map.get("output_info");
	    currentFile = new HashMap<String, Function>();
	    lib = new HashMap<String, ASTNode>();
	    return builtinClasses;
	}catch(Exception e){
              e.printStackTrace();
              System.err.println("Library definitions file not found.");
	}
	return null;
    }; 

    public static String getOutputInfo(String function){
	if (outputInfo.containsKey(function))
	    return outputInfo.get(function);
	return "DWH,H";
    }

    public static boolean scriptOrFunctionExists(String s){
	if (currentFile.containsKey(s)) {
	    return true;
	}
	if (lib.containsKey(s)) return true; 
	if (builtinClasses.containsKey(s)) return true; 
	if (builtinPackages.containsKey(s)) return true; 
	if (builtinFunctions.containsKey(s)) return true; 
	return false; 
    } 

    public static void setPrograms(HashMap<String, Program> progs){
	lib.clear();
	lib.putAll(progs);
    }

    //TODO remove this - this does not capture names correctly in the case
    //of nested functions exist - there should be separate methods
    //setCurrentScript and setCurrentFunction, or something like that
    @Deprecated
    public static void setCurrentProgram(Program f){
	currentFile.clear();
	if (!FunctionList.class.isInstance(f))
	    return;
	for (Function function: ((FunctionList)f).getFunctions())
	    currentFile.put(function.getName(), function);
    }
    
    
    /**
     * returns a FunctionOrScriptQuery object that allows querying
     * which matlab functions exist in the file environment simulated
     * by LookupFile.
     */
    public static FunctionOrScriptQuery getFunctionOrScriptQueryObject(){
        return new FunctionOrScriptQuery() {
            public boolean isFunctionOrScript(String name) {
                return scriptOrFunctionExists(name);
            }
        };
    }
}


@@ -1420,24 +1420,148 @@
 ,H%22;%0A    %7D%0A%0A
+    public static boolean packageExists(String s)%7B%0A%09if (builtinPackages.containsKey(s)) return true; %0A%09return false;%0A    %7D%0A%0A
     public s
@@ -1757,59 +1757,8 @@
 e; %0A
-%09if (builtinPackages.containsKey(s)) return true; %0A
 %09if 

return PREFIX kind for packages.