/*
 * TestUtils.java
 *
 * Created on October 15, 2003, 11:48 PM
 */

package hep.aida.ref.remote.testRemote;

import java.text.DateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import java.util.StringTokenizer;

import hep.aida.ref.remote.RemoteConnectionException;
import hep.aida.ref.remote.interfaces.AidaTreeClient;
import hep.aida.ref.remote.interfaces.AidaTreeServant;
import hep.aida.ref.remote.interfaces.AidaTreeServer;
import hep.aida.ref.remote.interfaces.AidaUpdateEvent;

/**
 * This class provides static utilities like strin-to-array and 
 * array-to-string conversion for manipulating with Paths.
 *
 * Also it provides test directories and objects for Client and Server
 * tests.
 *
 * This class should be used for tests only!
 * @author  serbo
 */
public class TestUtils {
    
    private static String treeName = "TestTreeName";
    
    private static String[] dirNames1 = new String[] { "dir1-1", "dir1-2", "Object1" };
    private static String[] dirNames2 = new String[] { "dir2-1", "dir2-2", "Object2" };
    private static String[] dirNames3 = new String[] { "dir3-1", "dir3-2", "Object3" };
    private static Object[] dirs = new Object[] { dirNames1, dirNames2, dirNames3 };
    
    private static String[] dirTypes = new String[] { "dir" , "dir" , "RemoteManagedObjectTest"  };
    
    // Creates array of directory names from the path String
    public static String[] stringToArray(String path) {
        String[] result = null;
        if ( path == null || path.equals("")) return null;

        StringTokenizer tokenizer = new StringTokenizer(path, "/");
        int nTokens = tokenizer.countTokens();
        result = new String[nTokens];
        for (int i = 0; i < nTokens; i++) {
            result[i] = tokenizer.nextToken().trim();
            //System.out.println("TestUtils.stringToArray: Token "+i+", dir: "+result[i]);
        }
        return result;
    }

    // Creates path String from the array. Starts with "/"
    public static String arrayToString(String[] path) {
        String result = "";
        if ( path == null || path.length == 0) return null;

        for (int i = 0; i<path.length; i++) {
            result += "/"+path[i].trim();
        }
        //System.out.println("TestUtils.arrayToString  result="+result);
        return result;
    }
    
    public static String getCurrentTime() {
        Date date = new Date(System.currentTimeMillis());
       
        String tmpString = DateFormat.getDateTimeInstance().format(date);
        StringTokenizer st = new StringTokenizer(tmpString);
        String dateString = "";
        while (st.hasMoreTokens()) {
            dateString += st.nextToken();
            if (st.hasMoreTokens()) { dateString += "_"; }
        }
        int index = dateString.indexOf(",");
        if (index > 0 && index < dateString.length()-1) {
            dateString = dateString.substring(0,index-1) + dateString.substring(index+1);
        }
        return dateString;
    }
    
    
    // Get info methods

    public static java.lang.Object find(String path) throws IllegalArgumentException {
        System.out.println("TestUtils.find: path="+path);
        String[] p = stringToArray(path);
        String time = null;
        
        if (p != null && p[p.length-1].startsWith("Object")) {
            
            // Here we just return Object name + Time string
            time = p[p.length-1] + "_" + getCurrentTime();
            
        } else throw new IllegalArgumentException("Illegal path: "+path);
        return time;
    }
    
    // Return full path, not just list og names in that directory
    public static String[] listObjectNames(String path) throws IllegalArgumentException {
        System.out.println("TestUtils.listObjectNames: path="+path);
        if (path.equals("/")) return new String[] { "Tests" };
        String[] p = stringToArray(path);
        if (p.length < 4) {            
            String[] tmp = (String[]) dirs[p.length-1];
            String[] result = new String[tmp.length];
            for (int i=0; i<tmp.length; i++) result[i] = path + "/" + tmp[i];
            return result;
        }
        else if (p.length == 4) return null;
        else throw new IllegalArgumentException("Illegal path: "+path);
    }
    
    // Return just the type name
    public static String[] listObjectTypes(String path) throws IllegalArgumentException {
        System.out.println("TestUtils.listObjectTypes: path="+path);
        if (path.equals("/")) return new String[] { "dir" };
         String[] p = stringToArray(path);
        if (p.length < 4) {            
            return dirTypes;
        }
        else if (p.length == 4) return null;
        else throw new IllegalArgumentException("Illegal path: "+path);
    }    
    
    public static String treeName() {
        return treeName;
    }
    
}
