package yi.net.html;

import yi.util.io.*;

public class HtmlAlbum {

  static String USAGE = "Usage: -i image_list_file_name -t title -r row_num -c col_num";

  public static String DEFAULT_TITLE = "Html Ablum";
  public static String BG_COLOR = "#ffffff";
  public static String TEXT_COLOR = "#000099";
  public static String LINK_COLOR = "#660066";
  public static String VLINK_COLOR = "#cc99cc";
  public static String ALINK_COLOR = "#33ff99";
  public static String FONT_FACE = "\"Verdana, Arial, Helvetica, Sans-Serif\"";

  public static String generateHtmlAlbum(String title, String[] thumb_img_links, String[] img_links, int col_num, int row_num, boolean show_img_name) {
    StringBuffer html = new StringBuffer();

    // header
    if (title == null) title = DEFAULT_TITLE;
    html.append(
      "<HTML>\n" +
      "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n"
    );

    // boby header
    html.append(
      "<BODY bgcolor=" + BG_COLOR +
           " text=" + TEXT_COLOR +
           " link=" + LINK_COLOR +
           " vlink=" + VLINK_COLOR +
           " alink=" + ALINK_COLOR +
      ">\n"
    );

    // 1st level table
    html.append(
      "<TABLE>\n" +
      "<TR><TD ALIGN=CENTER><FONT face=" + FONT_FACE + " size=\"+1\"><B>" + title + "</B></FONT></TD></TR>\n" +
      "<P>\n" +
      "<TR><TD ALIGN=CENTER><TABLE>\n"
    );

    // image list
    int count = 0;
    for (int y = 0; y < row_num; y ++) {

      if (count >= img_links.length) break;

      html.append("<TR>\n");

      for (int x = 0; x < col_num; x ++, count++) {
        if (count >= img_links.length) break;
        String short_img_name = yi.util.io.File.getShortName(img_links[count]);
        if (show_img_name) {
          html.append(
            "<TD ALIGN=CENTER VALIGN=BOTTOM>" +
            "<FONT face=" + FONT_FACE + " size=\"-2\">" +
            "<A HREF=" + img_links[count] + "><IMG SRC=" + thumb_img_links[count] + " ALT=" + short_img_name + ">" +
            "<BR>" + short_img_name + "</A></FONT></TD>\n"
          );
        } else {
          html.append(
            "<TD ALIGN=CENTER VALIGN=BOTTOM>" +
            "<A HREF=" + img_links[count] + "><IMG SRC=" + thumb_img_links[count] + " ALT=" + short_img_name + ">" +
            "</A></TD>\n"
          );
        }
      }

      html.append("</TR>\n");
    }

    // ending
    html.append(
      "</TABLE></TD></TR>\n" +
      "<P><TR><TD ALIGN=CENTER><TR><TD ALIGN=CENTER></TD></TR>\n" +
      "</TABLE>\n" +
      "</BODY>\n" +
      "</HTML>\n"
    );

    return html.toString();
  }

  /**
   *
   */
  public static void main(String[] args) {

    // image list file name
    String img_name = ReadArgu.getOption('i', args);
    if (img_name == null) {
      System.err.println(USAGE);
      System.exit(1);
    }

    // title
    String title = ReadArgu.getOption('t', args);
    if (title == null) {
      System.err.println(USAGE);
      System.exit(1);
    }

    // column number
    String col_num_str = ReadArgu.getOption('c', args);
    if (col_num_str == null) {
      System.err.println(USAGE);
      System.exit(1);
    }

    // row number
    String row_num_str = ReadArgu.getOption('r', args);
    if (row_num_str == null) {
      System.err.println(USAGE);
      System.exit(1);
    }

    try {
      int col_num = Integer.parseInt(col_num_str);
      int row_num = Integer.parseInt(row_num_str);
      String[] img_names = ReadArgu.readStrings(img_name);
      String html = HtmlAlbum.generateHtmlAlbum(title, img_names, img_names, col_num, row_num, true);
      System.out.println(html);
    } catch (java.io.IOException e) {
      e.printStackTrace();
    }

    System.exit(0);
  }
}