package liquibase.dbdoc;

import liquibase.change.Change;
import liquibase.database.Database;
import liquibase.structure.core.Column;
import liquibase.structure.core.Table;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TableWriter extends HTMLWriter {

    public TableWriter(File rootOutputDir, Database database) {
        super(new File(rootOutputDir, "tables"), database);
    }

    @Override
    protected String createTitle(Object object) {
        return "Changes affecting table \""+object.toString() + "\"";
    }

    @Override
    protected void writeCustomHTML(FileWriter fileWriter, Object object, List<Change> changes, Database database) throws IOException {
        final Table table = (Table) object;
        writeTableRemarks(fileWriter, table, database);
        writeColumns(fileWriter, table, database);
    }

    private void writeColumns(FileWriter fileWriter, Table table, Database database) throws IOException {
        List<List<String>> cells = new ArrayList<List<String>>();

        for (Column column : table.getColumns()) {
            String remarks = column.getRemarks();
            cells.add(Arrays.asList(column.getType().toString(),
                    column.isNullable() ? "NULL" : "NOT NULL",
                    "<A HREF=\"../columns/" + table.getName().toLowerCase() + "." + column.getName().toLowerCase() + ".html" + "\">" + column.getName() + "</A>",
                    remarks != null ? remarks : ""));
            //todo: add foreign key info to columns?
        }


        writeTable("Current Columns", cells, fileWriter);
    }
    
    private void writeTableRemarks(FileWriter fileWriter, Table table, Database database) throws IOException {
        final String tableRemarks = table.getRemarks();
        if (tableRemarks != null && tableRemarks.length() > 0) {
        	final List<List<String>> cells = new ArrayList<List<String>>();
        	cells.add(Arrays.asList(tableRemarks));
        	writeTable("Table Description", cells, fileWriter);
        }
    }
}

@@ -127,16 +127,99 @@
 Column;%0A
+import liquibase.structure.core.Index;%0Aimport liquibase.structure.core.PrimaryKey;%0A
 import l
@@ -249,16 +249,16 @@
 .Table;%0A
-
 %0Aimport 
@@ -1014,32 +1014,88 @@
 ble, database);%0A
+        writeTableIndexes(fileWriter, table, database);%0A
     %7D%0A%0A    priva
@@ -2187,24 +2187,24 @@
 eRemarks));%0A
-
         %09wri
@@ -2268,10 +2268,873 @@
 %7D%0A    %7D%0A
+    %0A    private void writeTableIndexes(FileWriter fileWriter, Table table, Database database) throws IOException %7B%0A        final List%3CList%3CString%3E%3E cells = new ArrayList%3CList%3CString%3E%3E();%0A        final PrimaryKey primaryKey = table.getPrimaryKey();%0A        if (!table.getIndexes().isEmpty()) %7B%0A            for (Index index : table.getIndexes()) %7B%0A                cells.add(Arrays.asList((primaryKey != null && primaryKey.getBackingIndex() == index ? %22Primary Key %22 : index.isUnique() ? %22Unique %22 : %22Non-Unique %22) +%0A                        (index.getClustered() == null ? %22%22 : (index.getClustered() ? %22Clustered%22 : %22Non-Clustered%22)),%0A                        index.getName(),%0A                        index.getColumnNames().replace(index.getTable().getName() + %22.%22,%22%22)));%0A            %7D%0A        writeTable(%22Current Table Indexes%22, cells, fileWriter);%0A        %7D%0A    %7D%0A
 %7D%0A

Write current table indexes into dbDoc