package nxm.xbc.prim;

import java.util.Arrays;

import nxm.sys.lib.MidasException;
import nxm.sys.lib.Primitive;
import nxm.sys.lib.Table;
import nxm.sys.lib.TextFile;

/** Generates the HTML pages from supported_calls.tbl.
    @author  J. Czechowski IV
 */
public class makedocs extends Primitive {
  private Table    support = null; // Table of supported calls.
  private TextFile outAbc  = null; // Output file that lists the support in alphabetical order.
  private TextFile outCat  = null; // Output file that lists the support by category.

  /** default constructor */
  public makedocs () {}

  public int open() {
    support = MA.getTable("TAB");
    outCat  = MA.getTextFile("OUT");
    outAbc  = MA.getTextFile("OUT2");

    if (support == null) { throw new MidasException("Invalid table TAB="+support); }
    outAbc.open(TextFile.OUTPUT|TextFile.FORCEABORT);
    outCat.open(TextFile.OUTPUT|TextFile.FORCEABORT);

    return NORMAL;
  }

  public int process() {
    // WRITE THE HEADDER --------------------------------
    writeBlock(outAbc, outCat, COMMON_HEADDER_1);
    writeBlock(null,   outCat, SEC_HEADDER_2);
    writeBlock(outAbc, null,   ABC_HEADDER_2);
    writeBlock(outAbc, outCat, COMMON_HEADDER_3);
    Table prefixTbl = support.getTable("_UNSUPPORTED_PREFIXES_");
    if (prefixTbl != null) {
      String[] prefixKeys = prefixTbl.getKeys();
      for (String prefixKey : prefixKeys) {
        Table prefix = prefixTbl.getTable(prefixKey);
        writePrefix(outAbc, prefixKey, prefix);
        writePrefix(outCat, prefixKey, prefix);
      }
    }
    writeBlock(outAbc, outCat, COMMON_MIDDLE_1);
    writeBlock(null,   outCat, SEC_MIDDLE_2);
    writeBlock(outAbc, null,   ABC_MIDDLE_2);
    writeBlock(outAbc, outCat, COMMON_MIDDLE_3);

    // WRITE THE ENTRIES --------------------------------
    Table mergedEntries = new Table();
    String[] keys = support.getKeys();

    for (String key : keys) {
      if (!key.equals("_UNSUPPORTED_PREFIXES_")) {
        Table    subTable = support.getTable(key);
        String[] subKeys  = subTable.getKeys();

        writeSectionHead(outCat, key);
        for (String subKey : subKeys) {
          Table entry = subTable.getTable(subKey);

          mergedEntries.put(subKey, entry);   // Add alphabetical entry to list to sort later.
          writeEntry(outCat, subKey, entry);  // Write category in order.
        }
      }
    }
    String[] mergedKeys = mergedEntries.getKeys();
    Arrays.sort(mergedKeys);

    for (String mergedKey : mergedKeys) {
      writeEntry(outAbc, mergedKey, mergedEntries.getTable(mergedKey));
    }

    // WRITE THE FOOTER ---------------------------------
    writeBlock(outAbc, outCat, COMMON_FOOTER);

    return FINISH;
  }

  /** Writes an entry to the file. */
  private void writeEntry(TextFile out, String name, Table entry) {
    String bgcolor = "green";
    String support = entry.getS("SUPPORT",  "");
    String comment = entry.getS("COMMENT",  "");
    String lang    = entry.getS("LANGUAGE", "");

         if (support.equalsIgnoreCase("FULL"      )) { support = "Full";    bgcolor = "green";  }
    else if (support.equalsIgnoreCase("NONE"      )) { support = "None";    bgcolor = "red";    }
    else if (support.equalsIgnoreCase("PARTIAL"   )) { support = "Partial"; bgcolor = "yellow"; }
    else if (support.equalsIgnoreCase("DEPRECATED")) { support = "Dep.";    bgcolor = "yellow"; }
    else if (support.equalsIgnoreCase("NOSUPPORT" )) { support = "n/a";     bgcolor = "grey";   }
    else { M.warning("Unknown support '"+support+"'"); }

    if ((comment == null) || (comment.trim().length() == 0)) {
      comment = "";
    } else {
      comment = " - <i>"+comment+"</i>";
    }

    if ((lang == null) || (lang.trim().length() == 0)) {
      lang = "&nbsp;";
    }

    out.writeln("<tr><td class=\"chk\" bgcolor=\"" + bgcolor + "\">" + support + "</td>"
              + "<td>" + name + comment + "</td>"
              + "<td class=\"chk\">" + lang + "</td></tr>");
  }

  /** Writes the section header to a file. */
  private void writeSectionHead(TextFile out, String section) {
    out.writeln("<tr><td colspan=\"3\">"+section+"</td></tr>");
  }

  /** Writes the prefix entry. */
  private void writePrefix(TextFile out, String prefix, Table entry) {
    String comment = entry.getS("COMMENT", "&nbsp;");
    comment = comment.replaceAll("[(]", "<i>(").replaceAll("[)]", ")</i>");
    out.writeln("        <tr><td>" + prefix + "</td><td>" + comment + "</td></tr>");
  }

  /** Writes a block of text out to the file(s). If either file is null it is ignored. */
  private void writeBlock(TextFile out1, TextFile out2, String[] text) {
    for (String str : text) {
      if (out1 != null) { out1.writeln(str); }
      if (out2 != null) { out2.writeln(str); }
    }
  }

  public int close() {
    outAbc.close();
    outCat.close();

    return NORMAL;
  }

  //////////////////////////////////////////////////////////////////////////////////////////////////
  // The HTML Text Blocks
  //////////////////////////////////////////////////////////////////////////////////////////////////

  /** The common headder part 1. */
  private static final String[] COMMON_HEADDER_1
          = { "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">",
              "<!-- ************************************************ -->",
              "<!-- ** THIS FILE IS AUTO_GENERATED, DO NOT MODIFY ** -->",
              "<!-- ************************************************ -->",
              "<html>",
              "  <head>",
              "    <link href=\"style.css\" type=\"text/css\" rel=\"stylesheet\">"};

  private static final String[] ABC_HEADDER_2
          = { "    <title>XBC Support (Alphabetical)</title>",
              "  </head>",
              "  <body>",
              "  <div>",
              "    <h3>XBC Support (Alphabetical)</h3>"};

  private static final String[] SEC_HEADDER_2
          = { "    <title>XBC Support (Categorical)</title>",
              "  </head>",
              "  <body>",
              "    <h3>XBC Support (Categorical)</h3>"};

  /** The common headder part 3. */
  private static final String[] COMMON_HEADDER_3
          = { "    <p>",
              "      The following lists the set of command prefixes that are not supported.",
              "      Commands with these prefixes will not be listed below.",
              "    </p>",
              "    <table>",
              "      <tbody>",
              "        <tr><th colspan=\"2\">Unsupported Prefixes</th></tr>",};


  /** The first part of the common middle. */
  private static final String[] COMMON_MIDDLE_1
          = { "      </tbody>",
              "    </table>",
              "    <br>",
              "    <br>",
              "    <p>"};

  /** The second part of the middle for section-ordered. */
  private static final String[] SEC_MIDDLE_2
          = { "      The following is a list of the X-Midas functions supported by XBC arranged",
              "      by the categories used in X-Midas's XVIEW.",
              "      See <a href=\"XBCSupport2.html\">XBCSupport (Alphabetical)</a>",
              "      for an alphabetical listing."};

  /** The second part of the middle for alphabetical-ordered. */
  private static final String[] ABC_MIDDLE_2
          = { "      The following is a list of the X-Midas functions supported by XBC arranged",
              "      in the alphabetical order.",
              "      See <a href=\"XBCSupport.html\">XBCSupport (Categorical)</a>",
              "      for a categorical listing."};

  /** The third part of the common middle. */
  private static final String[] COMMON_MIDDLE_3
          = { "      Also note that <code>QSORTER</code> (part of <code>host/qsort.c</code> in",
              "      X-Midas 3.8.3) is fully supported as if it was an <code>M$</code> call.",
              "    </p>",
              "    <table>",
              "      <tbody>",
              "        <tr>",
              "          <th colspan=\"2\">Legend</th>",
              "        </tr>",
              "        <tr><td class=\"chk\" bgcolor=\"green\"> Full   </td><td>Command is fully supported.</td></tr>",
              "        <tr><td class=\"chk\" bgcolor=\"yellow\">Partial</td><td>Partial support for the command, some features may not work, see notes.</tr>",
              "        <tr><td class=\"chk\" bgcolor=\"yellow\">Dep.   </td><td>Command is deprecated, do not use.</td></tr>",
              "        <tr><td class=\"chk\" bgcolor=\"red\">   None   </td><td>Support for the command has not been added yet.</td></tr>",
              "        <tr><td class=\"chk\" bgcolor=\"gray\">  n/a    </td><td>Command will not be supported.</td></tr>",
              "      </tbody>",
              "    </table>",
              "    <br>",
              "    <br>",
              "    <table>",
              "      <tbody>",
              "<tr><th>Support</th><th>Command</th><th>Coded In</th></tr>" };

  /** The common footer. */
  private static final String[] COMMON_FOOTER
          = { "      </tbody>",
              "    </table>",
              "  </body>",
              "</html>"};
}
