package code.table;

import code.modules.CurryModule;
import code.symbols.DataSymbol;
import code.symbols.Symbol;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Enumeration;

/**
 * This class provides printed views of various tables storing
 * the symbols of a program.  It uses enumerators to retrive
 * the symbols.
 *
 * @author Sergio Antoy
 * @since June 25, 2004
 */

public class TablesPrinter {

//    public static void printModuleTable() {
//        CurryModule[] moduleArray
//                = (CurryModule[]) ModuleTable.moduleVector().toArray();
//        Arrays.sort(moduleArray,
//                new Comparator() {
//                    public int compare(Object o1, Object o2) {
//                        Date d1 = ((CurryModule) o1).lastModDate;
//                        Date d2 = ((CurryModule) o2).lastModDate;
//                        if ((d1 == null) && (d2 == null)) {
//                            return 0;
//                        } else if (d1 == null) {
//                            return 1;
//                        }
//                        //d1 less than d2, older
//                        else if (d2 == null) {
//                            return -1;
//                        }
//                        //d1 greater than d2, newer
//                        else {
//                            return d2.compareTo(d1);
//                        }
//                    }
//                });
//        System.out.println("Module table sorted by time, " + moduleArray.length + " modules:");
//        for (int j = 0; j < moduleArray.length; ++j) {
//            moduleArray[j].PrintAsTxtLoadable();
//        }
//
//    }

    public static void printModuleTable() {
	System.out.println("---- ModuleTable begins ----");
        Enumeration xenum = ModuleTable.getAllModules();
        while (xenum.hasMoreElements()) {
            CurryModule module = (CurryModule) xenum.nextElement();
	    if (module == null) continue;          // undefined
            String moduleName = module.moduleName;
	    System.out.println(moduleName);
        }
	System.out.println("---- ModuleTable ends ----");
    }

    public static void printMapTable() {
	System.out.println("---- MapTable begins ----");
        Enumeration xenum = MapTable.getAllSymbols();
        int id = 0;
        while (xenum.hasMoreElements()) {
            DataSymbol symbol = (DataSymbol) xenum.nextElement();
            System.out.print(id + "  ");
            if (symbol == null)
                System.out.println("null !?");
            else
                System.out.println(symbol.moduleName + "." +
                        symbol.symbolName);
            id++;
        }
	System.out.println("---- MapTable ends ----");
    }

    public static void printAllSymbolTables() {
	System.out.println("---- SymbolTables begin ----");
        Enumeration xenum = ModuleTable.getAllModules();
        while (xenum.hasMoreElements()) {
            CurryModule module = (CurryModule) xenum.nextElement();
	    if (module == null) continue;          // undefined
            String moduleName = module.moduleName;
            printSymbolTable(moduleName);
        }
	System.out.println("---- SymbolTables end ----");
    }

    public static void printSymbolTable(String moduleName) {
	System.out.println(moduleName);
        SymbolTable symbolTable = ModuleTable.getTable(moduleName);
        Enumeration xenum = symbolTable.getAllSymbols();
        while (xenum.hasMoreElements()) {
            Symbol symbol = (Symbol) xenum.nextElement();
            System.out.println("  " + symbol.symbolName);
        }
    }

    public static void printUndefinedSymbols() {
        Enumeration xenum = ModuleTable.getUndefinedSymbols();
	if (xenum.hasMoreElements()) {
	    System.out.println("---- Undefined symbols begin ----");
	    while (xenum.hasMoreElements()) {
		String symbolName = (String) xenum.nextElement();
		System.out.println(symbolName);
	    }
	    System.out.println("---- Undefined symbols end ----");
	} else {
	    System.out.println("---- NO Undefined symbols ----");
	}
    }

}

