package yi.or.db;

import java.util.*;
import java.io.*;
import yi.or.ObjRcg;
import java.net.*;

public class Engine
{
    static String DEFAULTDIR="http://bird.cs.washington.edu/gts/test";
    
    String[] imgFiles= null;
    String[] objFiles= null;
    String[] objs= ObjRcg.getAllObjNames();
    //the two hash tables below are just help to construct the fileTable and Objtable;
    Hashtable imgNameTable= null;
    Hashtable objNameTable= null;
    Hashtable imgTable= null;
    Hashtable objTable= null;
    
    public Engine(String db_dir)
    {
        imgFiles= getAllImgFileName();//FindAllImages.everySingleName(db_dir);
        objFiles= getAllObjFileName();//FindAllImages.getObjFileNames(db_dir);
        
        
        //initialize the file name hash table
        imgNameTable= new Hashtable(imgFiles.length);
        for (int i=0;i<imgFiles.length;i++)
            imgNameTable.put(imgFiles[i],imgFiles[i]);
        //initialize the obj name hash table
        objNameTable= new Hashtable(objs.length);
        for (int i=0;i<objs.length;i++)
            objNameTable.put(objs[i],objs[i]);
        
            
        //initialize the image hash table
        imgTable= new Hashtable(imgFiles.length);
        for (int i=0;i<imgFiles.length;i++)
            imgTable.put(imgFiles[i], new Vector());
        //initialize the object hash table
        objTable= new Hashtable(objs.length);
        for (int i=0;i<objs.length;i++)
            objTable.put(objs[i], new Vector());
        
        //add the file names to the object table
        for (int i=0;i<objFiles.length;i++) {
            try {
                //BufferedReader br= new BufferedReader(new FileReader(objFiles[i]));
                URL url= new URL(objFiles[i]);
                BufferedReader br= new BufferedReader(new InputStreamReader(url.openStream()));
                String[] sepName= Util.fileNameSep(objFiles[i]);
                String dirName= sepName[0];
                
                String str;
                while ((str=br.readLine())!=null) {
                    //get the file name
                    StringTokenizer st= new StringTokenizer(str, ":, ", false);
                    String fileName= st.nextToken();
                    Object fileObject= imgNameTable.get(dirName+fileName);
                    Vector image_v= (Vector)imgTable.get(dirName+fileName);
                    while (st.hasMoreTokens()) {
                        String objName=st.nextToken();
                        Vector v= (Vector)objTable.get(objName);
                        v.addElement(fileObject);
                        Object objobj= objNameTable.get(objName);
                        image_v.addElement(objobj);
                    }
                }
            }
            catch (IOException e) {
                System.err.println("error when reading the object files: "+e);
            }
        }
        
    }
    
    public Engine()
    {
        this(DEFAULTDIR);
    }
    
    public Vector queryOnObject(String objName) {
        return (Vector)objTable.get(objName);
    }
    
    public Vector queryOnImage(String imgName) {
        return (Vector)imgTable.get(imgName);
    }
    
    public String[] getRandomImages(int num)
    {
        String[] rst= new String[num];
        int numofimg=imgFiles.length;
        Random r= new Random();
        for (int i=0;i<num;i++) {
            int ran=r.nextInt()%numofimg;
            if (ran<0) ran+=numofimg;
            rst[i]=imgFiles[ran];
        }
        return rst;
    }
    
    public static void main(String args[])
    {
        Engine e= new Engine(DEFAULTDIR);
        System.err.println("find "+e.imgFiles.length+" image files");
        System.err.println("find "+e.objFiles.length+" object files");
        Vector ov=e.queryOnObject("boat");
        Vector iv=e.queryOnImage((String)ov.elementAt(0));
    }
    
    String[] getAllImgFileName()
    {
        String[] str= {
            "http://bird.cs.washington.edu/gts/test/a/Image03.jpg",
            "http://bird.cs.washington.edu/gts/test/a/Image16.jpg",
            "http://bird.cs.washington.edu/gts/test/a/Image24.jpg",
            "http://bird.cs.washington.edu/gts/test/boat/Q7180237.jpg",
            "http://bird.cs.washington.edu/gts/test/boat/Q7180244.jpg",
            "http://bird.cs.washington.edu/gts/test/boat/Q7180280.jpg",
            "http://bird.cs.washington.edu/gts/test/boat/Q7180298.jpg",

        };
        return str;
    }
    
    String[] getAllObjFileName()
    {
        String[] str= {
            "http://bird.cs.washington.edu/gts/test/a/objects",
            "http://bird.cs.washington.edu/gts/test/boat/objects",
        };
        return str;
    }
}