// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

public class CssTest {
  public static void main(String[] args) {
    new CssTest(System.getProperty("QUERY_STRING"),
                System.getProperty("HTTP_COOKIE"));
  }

  protected final boolean COOKIES = true;
  protected final boolean QUERY_STRING = false;
  protected  String[] names = CssChoices.names;
  protected String[] values = new String[names.length];
  protected LookupTable lookupTable;

  public CssTest(String data, String cookies) {
    processData(CssChoices.defaults, COOKIES);
    processData(cookies, COOKIES);
    processData(data, QUERY_STRING);
    lookupTable = new LookupTable(names, values, null);
    printBody();
  }

  protected void printBody() {
    printHttpHeaders();
    printDocument();
  }

  protected void processData(String data,
                             boolean cookieFlag) {
    LookupTable table;
    if (cookieFlag) {
      CookieParser parser = new CookieParser(data);
      table = parser.parse();
    } else {
      QueryStringParser parser =
        new QueryStringParser(data);
      table = parser.parse();
    }
    String value;
    for(int i=0; i<names.length; i++) {
      value = table.getValue(names[i]);
      if (value != null && !value.equals(""))
        values[i] = value;
    }
  }

  protected void printHttpHeaders() {
    String expires = CssChoices.expires;
    System.out.println
      ("Content-Type: text/html\n" +
       "Set-Cookie: repeatVisitor=true" + expires);
    for (int i=1; i<names.length; i++)
      System.out.println
        ("Set-Cookie: " +
         names[i] + "=" + values[i] + expires);
    System.out.println();
  }
  
  protected void printDocument() {
    System.out.println
      ("<!DOCTYPE HTML PUBLIC " +
         "\"-//W3C//DTD HTML 3.2//EN\">\n" +
       "<HTML>\n<HEAD>\n" +
       "<TITLE>CSS1 Test</TITLE>\n" +
       "<STYLE>\n<!--");
    printStyleRules();
    System.out.println
      ("-->\n</STYLE>\n</HEAD>\n\n" +
       "<BODY>\n" +
       "<H1>CSS1 Test</H1>\n" +
       welcomeMessage() +
       "This page demonstrates a few of the\n" +
       "capabilities of cascading style sheets\n" +
       "by letting you select options interactively.\n" +
       "If your browser supports cookies, values\n" +
       "you select in this session will be used\n" +
       "as initial settings in future sessions.\n" +
       "<FORM ACTION=\"" + CssChoices.url + "\">\n" +
       "  <H2>Background</H2>");
    printSelect("  Color Name:", "bgColor",
                CssChoices.bgColor);
    System.out.println
      ("  <H2>Main Headings:</H2>");
    printSelect("  Size:", "headingSize",
                CssChoices.headingSize);
    printSelect("  Font:", "headingFont",
                CssChoices.headingFont);
    printSelect("Alignment", "headingAlignment",
                CssChoices.headingAlignment);
    System.out.println
      ("  <H2>Indentation</H2>\n" +
       "  CSS1 allows you to define margins\n" +
       "  for built-in and custom paragraph classes.\n" +
       "  <P CLASS=\"indent1\">\n" +
       "  These lines are indented<BR>\n" +
       "  according to the first-level<BR>\n" +
       "  margin setting below (in inches).<BR>\n" +
       "  <P CLASS=\"indent2\">\n" +
       "  These lines are indented<BR>\n" +
       "  according to the second-level<BR>\n" +
       "  margin setting below (in inches).\n" +
       "  </P>");
    printSelect("  First-Level:", "indent1",
                CssChoices.indent);
    printSelect("  Second-Level:", "indent2",
                CssChoices.indent);
    System.out.println
      ("  <P>\n" +
       "  <CENTER>\n" +
       "    <INPUT TYPE=\"SUBMIT\" VALUE=\"Try It\">\n" +
       "  </CENTER>\n" +
       "</FORM>\n</BODY>\n</HTML>");
  } 

  protected void printStyleRules() {
    System.out.println
      ("BODY { background: " + val("bgColor") + " }\n" +
       "H1 { text-align: " + val("headingAlignment") +
       ";\n" +
       "     font-size: " + val("headingSize") +
       "pt;\n" +
       "     font-family: " + val("headingFont") +
       " }\n" +
       "P.indent1 { margin-left: " + val("indent1") +
       "in }\n" +
       "P.indent2 { margin-left: " + val("indent2") +
       "in }");
  } 

  protected String val(String name) {
    return(lookupTable.getValue(name));
  }

  protected void printOption(String value, String name) {
    System.out.print
      ("    <OPTION VALUE=\"" + value + "\"");
    if (value.equals(val(name)))
      System.out.print(" SELECTED>");
    else
      System.out.print(">");
    System.out.println(value);
  }

  protected void printSelect(String intro,
                             String name,
                             String[] options) {
    System.out.println
      (intro + "\n" +
       "  <SELECT NAME=\"" + name + "\">");
    for (int i=0; i<options.length; i++)
      printOption(options[i], name);
    System.out.println
      ("  </SELECT>");
  }

  protected String welcomeMessage() {
    if(val("repeatVisitor") != null)
      return("Welcome back!\n");
    else
      return("Welcome, first time visitor!\n");
  }
}
