/*
 * 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 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.servlets.format;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.htmlcheck.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;

public class TextMessageDialog implements ContentRender, ColorSelectors
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  // Dialog types
  public static final int TYPE_ACCEPT_DECLINE = 0;

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private int type;
  private String title;
  private String text;
  private String[] choices;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public TextMessageDialog(int type, String title, String text, String[] choices)
  {
    this.type = type;
    this.title = title;
    this.text = text;
    this.choices = choices;

    int nbutton = getNumButtons(type);
    if (choices.length<nbutton)
      throw new InternalStateError("insufficient elements in choices array");

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal overrideable operations
   *--------------------------------------------------------------------------------
   */

  protected int getNumButtons(int xtype)
  {
    if (xtype==TYPE_ACCEPT_DECLINE)
      return 2;
    throw new InternalStateError("invalid TextMessageDialog type :" + type);

  } // end getNumButtons

  protected String getImageNameForButton(int xtype, int ndx)
  {
    if (xtype==TYPE_ACCEPT_DECLINE)
    { // accept or decline
      if (ndx==0)
	return "bn_i_accept.gif";
      else if (ndx==1)
	return "bn_i_decline.gif";
    
      throw new InternalStateError("invalid button index: " + ndx);

    } // end if

    throw new InternalStateError("invalid TextMessageDialog type :" + type);

  } // end getImageNameForButton

  protected String getAltTextForButton(int xtype, int ndx)
  {
    if (xtype==TYPE_ACCEPT_DECLINE)
    { // accept or decline
      if (ndx==0)
	return "I Accept";
      else if (ndx==1)
	return "I Decline";
    
      throw new InternalStateError("invalid button index: " + ndx);

    } // end if

    throw new InternalStateError("invalid TextMessageDialog type :" + type);

  } // end getImageNameForButton

  /*--------------------------------------------------------------------------------
   * Implementations from interface VeniceContent
   *--------------------------------------------------------------------------------
   */

  public String getPageTitle(RenderData rdat)
  {
    return title;

  } // end getPageTitle

  public String getPageQID()
  {
    return null;

  } // end getPageQID

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentRender
   *--------------------------------------------------------------------------------
   */

  public void renderHere(Writer out, RenderData rdat) throws IOException
  {
    rdat.writeContentHeader(out,title,null);
    out.write(rdat.getStdFontTag(CONTENT_FOREGROUND,2));
    out.write("\n");
    out.write(text);
    out.write("</FONT><P>\n<DIV ALIGN=\"center\">\n");

    int nbutton = getNumButtons(type);
    for (int i=0; i<nbutton; i++)
    { // write out the individual buttons
      if (i>0)
	out.write("&nbsp;\n");
      out.write("<A HREF=\"");
      out.write(rdat.getEncodedServletPath(choices[i]));
      out.write("\"><IMG SRC=\"");
      out.write(rdat.getFullImagePath(getImageNameForButton(type,i)));
      out.write("\" ALT=\"");
      out.write(getAltTextForButton(type,i));
      out.write("\" WIDTH=80 HEIGHT=24 BORDER=0></A>\n");

    } // end for

    out.write("</DIV>\n");

  } // end renderHere

} // end class TextMessageDialog
