/*
 * Copyright 2003 Code Futures Software, Ltd. All rights reserved.
 *
 * This is proprietary code. Use is subject to license terms.
 *
 * Author: Andy Grove
 * Date: 28-Nov-2003
 * Time: 07:37:14
 *  
 * Visit http://www.codefutures.com for more information.
 */
package com.codefutures.firestorm.codegen.html;

import com.codefutures.firestorm.api.codegen.Provider;
import com.codefutures.firestorm.api.codegen.ProviderOptions;
import com.codefutures.firestorm.api.codegen.CodeGenException;
import com.codefutures.firestorm.api.studio.Studio;
import com.codefutures.firestorm.api.studio.Wizard;
import com.codefutures.firestorm.config.dom.FireStormConfig;
import com.codefutures.firestorm.project.dom.Project;
import com.codefutures.firestorm.project.dom.TableDao;
import com.codefutures.firestorm.project.dom.TableDaoColumn;
import com.codefutures.progress.ProgressListener;

import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.util.Map;

public class HtmlProvider implements Provider
{
    public void setConfig(FireStormConfig config)
    {
    }

    public String getMenuItemName()
    {
        return "Generate HTML Documentation";
    }

    public char getMenuItemMnemonic()
    {
        return 'H';
    }

    public String getProviderId()
    {
        return "html";
    }

    public String getDescription()
    {
        return "Generates HTML Documentation";
    }

    public ProviderOptions getDefaultOptions(
            Project project
            )
    {
        HtmlProviderOptions ret = new HtmlProviderOptions();
        ret.setOutputDir( new File( "gen-html" ) );
        return ret;
    }

    public void readOptions(Map map, ProviderOptions options) {
    }

    public void writeOptions(Map map, ProviderOptions options) {
    }

    public boolean validateSetup(
            Studio studio,
            Project project,
            ProviderOptions options
            )
    {
        return true;
    }

    public Wizard getWizard(
            Studio studio,
            Project project,
            ProviderOptions options
            )
    {
        return null;
    }

    public void generateCode(
            Project project,
            ProviderOptions options,
            ProgressListener listener)
            throws CodeGenException, IOException
    {
        listener.message( "Generating HTML Documentation" );

        File outputDir = options.getOutputDir();
        if (!outputDir.exists())
        {
            outputDir.mkdirs();
        }

        TableDao table[] = project.getDatabase().getTableList();
        for (int i = 0; i < table.length; i++)
        {
            // update the progress bar
            listener.progress( i, table.length );

            File html = new File( outputDir, table[i].getSqlName() + ".html" );

            PrintWriter w = new PrintWriter( new FileWriter(html) );

            w.println( "<html>" );
            w.println( "<head>" );
            w.println( "<title>" + table[i].getSqlName() + "</title>" );
            w.println( "</head>" );
            w.println( "<body>" );

            w.println( "<h1>" + table[i].getSqlName() + "</h1>" );

            w.println( "<table>" );
            TableDaoColumn col[] = table[i].getColumnList();
            for (int j = 0; j < col.length; j++)
            {
                w.println( "<tr>" );
                w.println( "<td>" + col[j].getSqlName() + "</td>" );
                w.println( "<td>" + col[j].getSqlType() + "</td>" );
                w.println( "<td>" + col[j].getJavaName() + "</td>" );
                w.println( "<td>" + col[j].getJavaType() + "</td>" );
                w.println( "</tr>" );
            }
            w.println( "</table>" );

            w.println( "</body>" );
            w.println( "</html>" );

            w.close();

            listener.complete();
        }
    }

    public void completedAction(
            Studio studio,
            Project project,
            ProviderOptions options,
            long timeMilliseconds
            )
    {
    }

}
