package net.alanmaxwell.html;

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;

public class HTMLStrings {
  public static DecimalFormat numbersTo1DP =
    new DecimalFormat("#,###,##0.#");
  public static DecimalFormat numbersTo2DP =
    new DecimalFormat("#,###,##0.##");
  public static DecimalFormat numbersTo3DP =
    new DecimalFormat("#,###,##0.###");
  public static SimpleDateFormat dateMaker =
    new SimpleDateFormat("d MMMMM yyyy");
  
  public static String getContentType() {
    return getContentType("text/html");
  };

  public static String getContentType(String contentType) {
    return "Content-type: " + contentType + "\n\n";
  };

  public static String getHTMLHeadWithCSS(String pageTitle, 
                                          String styleSheetURL) {

    return getHTMLHead(pageTitle, styleSheetURL, "");
  };

  public static String getHTMLHeadWithScript(String pageTitle, 
                                             String headScriptURL) {
    return getHTMLHead(pageTitle, "", headScriptURL);
  };
  public static String getHTMLHeadWithScript(String pageTitle, 
          String headScriptURL,String eggTimer) {
return getHTMLHead(pageTitle, "", headScriptURL,eggTimer);
};

  public static String getHTMLHead(String pageTitle) {
    return getHTMLHead(pageTitle, "", "");
  };
  public static String getHTMLHead(String pageTitle, 
          String styleSheetURL,
          String headScriptURL){
      return getHTMLHead(pageTitle,styleSheetURL,headScriptURL,"");
  }
  
  public static String getHTMLHead(String pageTitle, 
                                   String styleSheetURL,
                                   String headScriptURL,String eggTimer) {

    String styleSheetHTML;
    String headScriptHTML;

    if (styleSheetURL.length() <= 0) {
      styleSheetHTML = "";
    } else {
      styleSheetHTML = 
        "\n    <link rel=\"stylesheet\" type=\"text/css\" \n" + 
        "          href=\"" +
             styleSheetURL + "\" /> \n";
    };

    if (headScriptURL.length() <= 0) {
      headScriptHTML = "";
    } else {
      headScriptHTML =
        "\n    <script type=\"text/javascript\" src=\"" + headScriptURL + "\"> \n" +
        "    </script> \n";
    };

    return
      "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> \n\n" +
      "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \n" +
      "          \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> \n\n" +
      "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\"> \n" +
      "  <head> \n" +
      "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" /> \n" +
      "    <title>" + pageTitle + "</title> \n" +
      styleSheetHTML +
      headScriptHTML +
      "  </head> \n\n" +
      "  <body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#0000FF\" vlink=\"#000080\" alink=\"#FF0000\" "+eggTimer+" > \n";
  }; // END of getHTMLHeader()

  public static String getHTMLTail() {
    return getHTMLTail("");
  };

  public static String getHTMLTail(String tailScriptURL) {
    String tailScriptHTML = null;

    if (tailScriptURL.length() <= 0) {
      tailScriptHTML = "";
    } else {
      tailScriptHTML =
        "\n    <script type=\"text/javascript\" src=\"" + tailScriptURL + "\"> \n" +
        "    </script> \n" +
        "      <noscript> \n" +
        "        [!!]&nbsp;&nbsp;Style-sheets disabled (cannot detect browser support) \n" +
        "      </noscript> \n";
    };

    return
      "\n" +
      "    <hr size=\"1\" noshade=\"noshade\" /> \n\n" +
      "    <small class=\"FooterText\"> \n" +
      "      &nbsp;Page created: " +
               dateMaker.format(Calendar.getInstance().getTime()) + 
               "&nbsp;&nbsp;&#8226;&nbsp;&nbsp; \n" +
      tailScriptHTML +
      "    </small> \n" +
      "  </body> \n" +
      "</html> \n";
  }; // END of getHTMLTail()

  public static String getHTMLErrorFragment(String errorMessage) {
    return
      "    <h1 class=\"error\">Error Message</h1> \n\n" +
      "    <p class=\"error\"> \n" +
      "      " + errorMessage + " \n" +
      "    </p> \n\n";
  }; // END of getHTMLErrorfragment()

  public static String getHTMLErrorWithCSS(String errorMessage, 
                                           String styleSheetURL) {

    return getHTMLError(errorMessage, styleSheetURL, "");
  };

  public static String getHTMLErrorWithScript(String errorMessage,
                                              String headScriptURL) {

    return getHTMLError(errorMessage, "", headScriptURL);
  };

  public static String getHTMLError(String errorMessage) {
    return getHTMLError(errorMessage, "", "");
  }; // END of getHTMLError()

  public static String getHTMLError(String errorMessage,
                                    String styleSheetURL,
                                    String headScriptURL) {
    
    return 
      getHTMLHead("Error Message", styleSheetURL, headScriptURL) +
      getHTMLErrorFragment(errorMessage) +
      getHTMLTail();
  }; // END of getHTMLError()
}