/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.conf.jsp;

import java.io.IOException;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;

public class PostBoxTag extends VeniceConfBodyTagSupport
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String action = null;
  private String type = "servlet";
  private int real_type = -1;
  private HashMap params = new HashMap();
  private String newtopic_name = null;
  private String newtopic_value = null;
  private String pseud_name = null;
  private String pseud_value = null;
  private String attach_name = null;
  private boolean attach_value = false;
  private String text_name = null;
  private String text_value = null;
  private ArrayList buttons = new ArrayList();

  /*--------------------------------------------------------------------------------
   * Overrides from class BodyTagSupport
   *--------------------------------------------------------------------------------
   */

  public int doStartTag() throws JspException
  {
    if (action==null)
      throw new JspTagException("<post:box/> action= attribute not specified!");
    HTMLRendering html = (HTMLRendering)(getRequestInput().queryService(HTMLRendering.class));
    real_type = html.convertLinkType(type);
    if (real_type<0)
      throw new JspTagException("<post:box/> type= attribute not a valid link type");
    return EVAL_BODY_TAG;

  } // end doStartTag

  public int doAfterBody()
  {
    return SKIP_BODY;

  } // end doAfterBody

  public int doEndTag() throws JspException
  {
    if (pseud_name==null)
      throw new JspTagException("<post:box/> tag has no <post:pseud/> tag inside it!");
    if (attach_name==null)
      throw new JspTagException("<post:box/> tag has no <post:attach/> tag inside it!");
    if (text_name==null)
      throw new JspTagException("<post:box/> tag has no <post:text/> tag inside it!");
    if (buttons.isEmpty())
      throw new JspTagException("<post:box/> tag has no <post:button/> tags inside it!");

    HTMLRendering html = (HTMLRendering)(getRequestOutput().queryService(HTMLRendering.class));

    try
    { // write out what we came here to accomplish
      JspWriter out = pageContext.getOut();
      out.write("<FORM METHOD=\"POST\" ACTION=\"" + html.formatURL(action,real_type)
		+ "\"><DIV CLASS=\"content\">\n");
      Iterator it = params.entrySet().iterator();
      while (it.hasNext())
      { // write the hidden parameters
	Map.Entry ntry = (Map.Entry)(it.next());
	out.write("<INPUT TYPE=\"HIDDEN\" NAME=\"" + ntry.getKey() + "\" VALUE=\"" + ntry.getValue()
		  + "\">\n");

      } // end while

      String my_font = html.getFontTag(html.CONTENT_FOREGROUND,"content");
      out.write("<TABLE BORDER=0 CELLPADDING=0>\n");
      if (newtopic_name!=null)
      { // write the "new topic name" field
	out.write("<TR><TD ALIGN=LEFT COLSPAN=2 CLASS=\"content\">\n"
		  + my_font + "New topic name:</FONT><BR>\n<SPAN CLASS=\"cinput\">"
		  + "<INPUT TYPE=\"TEXT\" CLASS=\"cinput\" NAME=\"" + newtopic_name
		  + "\" SIZE=37 MAXLENGTH=128 VALUE=\"");
        if (newtopic_value!=null)
	  out.write(newtopic_value);
	out.write("\"></SPAN>\n</TD></TR>\n");

      } // end if

      out.write("<TR><TD ALIGN=LEFT COLSPAN=2 CLASS=\"content\">\n"
                + my_font + "Your name/header:</FONT><BR>\n<SPAN CLASS=\"cinput\">"
		+ "<INPUT TYPE=\"TEXT\" CLASS=\"cinput\" NAME=\"" + pseud_name
		+ "\" SIZE=37 MAXLENGTH=255 VALUE=\"");
      if (pseud_value!=null)
	out.write(pseud_value);
      out.write("\"></SPAN>\n" + my_font +  "<INPUT TYPE=\"CHECKBOX\" NAME=\"" + attach_name
		+ "\" VALUE=\"Y\"");
      if (attach_value)
	out.write(" CHECKED");
      out.write("> Attach a file</FONT>\n</TD></TR>\n<TR>\n<TD ALIGN=LEFT CLASS=\"content\">\n"
		+ my_font + "Message:</FONT>\n</TD>\n<TD ALIGN=RIGHT CLASS=\"content\">" + my_font
		+ "\n<A HREF=\"" + html.getExternalStaticPath("html-reference.html")
		+ "\" TARGET=\"_blank\">HTML Guide</A>\n</FONT></TD>\n</TR>\n<TR>"
		+ "<TD ALIGN=LEFT COLSPAN=2 CLASS=\"cinput\">\n<TEXTAREA NAME=\"" + text_name
		+ "\" WRAP=SOFT ROWS=7 COLS=51>");
      if (text_value!=null)
	out.write(text_value);
      out.write("</TEXTAREA>\n</TD></TR>\n<TR><TD ALIGN=CENTER COLSPAN=2 CLASS=\"content\">\n");
      boolean first = true;
      it = buttons.iterator();
      while (it.hasNext())
      { // write out all the buttons
	String id = (String)(it.next());
	if (first)
	  first = false;
	else
	  out.write("&nbsp;");
	out.write(html.getButtonInput(id) + "\n");

      } // end while

      out.write("</TD></TR>\n</TABLE>\n</DIV></FORM>\n");

    } // end try
    catch (IOException e)
    { // convert the I/O error into something the servlet engine can deal with
      throw new JspTagException("error writing post box - " + e.getMessage());

    } // end catch

    return EVAL_PAGE;

  } // end doEndTag

  public void release()
  {
    super.release();
    action = null;
    type = "servlet";
    real_type = -1;
    params.clear();
    pseud_name = null;
    pseud_value = null;
    attach_name = null;
    attach_value = false;
    text_name = null;
    text_value = null;
    buttons.clear();

  } // end release

  /*--------------------------------------------------------------------------------
   * Attribute set functions
   *--------------------------------------------------------------------------------
   */

  public void setAction(String v)
  {
    action = v;

  } // end setAction

  public void setType(String v)
  {
    type = v;

  } // end setType

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  final void addParam(String name, String value)
  {
    params.put(name,value);

  } // end addParam

  final void setNewTopic(String param_name, String value) throws JspException
  {
    if (newtopic_name!=null)
      throw new JspTagException("<post:newtopic/> may only appear once inside a <post:box/>!");
    if (param_name==null)
      throw new JspTagException("<post:newtopic/> parameter name not specified!");
    newtopic_name = param_name;
    newtopic_value = value;

  } // end setNewTopic

  final void setPseud(String param_name, String value) throws JspException
  {
    if (pseud_name!=null)
      throw new JspTagException("<post:pseud/> may only appear once inside a <post:box/>!");
    if (param_name==null)
      throw new JspTagException("<post:pseud/> parameter name not specified!");
    pseud_name = param_name;
    pseud_value = value;

  } // end setPseud

  final void setAttach(String param_name, boolean value) throws JspException
  {
    if (attach_name!=null)
      throw new JspTagException("<post:attach/> may only appear once inside a <post:box/>!");
    if (param_name==null)
      throw new JspTagException("<post:attach/> parameter name not specified!");
    attach_name = param_name;
    attach_value = value;

  } // end setAttach

  final void setText(String param_name, String value) throws JspException
  {
    if (text_name!=null)
      throw new JspTagException("<post:text/> may only appear once inside a <post:box/>!");
    if (param_name==null)
      throw new JspTagException("<post:text/> parameter name not specified!");
    text_name = param_name;
    text_value = value;

  } // end setText

  final void addButton(String id)
  {
    buttons.add(id);

  } // end addButton

} // end class PostBoxTag
