/*
 * 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@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.sidebox;

import java.io.IOException;
import java.util.*;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.ImageHandler;

public class JSPSideBoxFactory implements SideBoxFactory
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private ImageHandler title_image;       // title image for sidebox for logged-in users
  private ImageHandler anon_title_image;  // title image for sidebox for not-logged-in users
  private String title;                   // title of sidebox for logged-in users
  private String anon_title;              // title of sidebox for not-logged-in users
  private String format_jsp;              // name of formatting JSP file
  private Map strings;                    // the strings we can access from the JSP page

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

  public JSPSideBoxFactory()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal functions
   *--------------------------------------------------------------------------------
   */

  final String getTitle(boolean logged_in, RequestOutput ro)
  {
    if (title_image!=null)
    { // return an image rendering
      ImageHandler ih = (logged_in ? title_image : anon_title_image);
      return ih.getRendering(ro);

    } // end if
    else
    { // return a title string
      String rc = (logged_in ? title : anon_title);
      return StringUtil.encodeHTML(rc);
      
    } // end else

  } // end getTitle

  final String getTargetJSPName()
  {
    return format_jsp;

  } // end getTargetJSPName

  final String getString(String key)
  {
    return (String)(strings.get(key));

  } // end getString

  final String replaceStrings(String base)
  {
    return StringUtil.replaceAllVariables(base,strings);

  } // end replaceStrings

  /*--------------------------------------------------------------------------------
   * Implementations from interface SideBoxFactory
   *--------------------------------------------------------------------------------
   */

  public void setConfiguration(Element cfg) throws ConfigException
  {
    DOMElementHelper cfg_h = new DOMElementHelper(cfg);
    title_image = new ImageHandler(cfg_h.getSubElement("title-image"));
    if (title_image.isDefined())
    { // we're using image titles...
      title = anon_title = null;
      anon_title_image = new ImageHandler(cfg_h.getSubElement("anon-title-image"));
      if (!(anon_title_image.isDefined()))
	anon_title_image = title_image;

    } // end if
    else
    { // standard textual title
      title_image = anon_title_image = null;
      title = cfg_h.getSubElementText("title");
      if (title==null)
	throw new ConfigException("no <title/> specified for sidebox",cfg);
      title += ":";
      anon_title = cfg_h.getSubElementText("anon-title");
      if (anon_title==null)
	anon_title = title;
      else
	anon_title += ":";

    } // end else

    format_jsp = cfg_h.getSubElementText("format-jsp");
    if (format_jsp==null)
      throw new ConfigException("no <format-jsp/> specified for sidebox",cfg);

    Element s = cfg_h.getSubElement("strings");
    if (s!=null)
    { // we've got some replacement strings to add
      HashMap tmp = new HashMap();
      NodeList sl = s.getChildNodes();
      for (int i=0; i<sl.getLength(); i++)
      { // get all the replacement strings and add them to the map
	Node n = sl.item(i);
	if (n.getNodeType()==Node.ELEMENT_NODE)
	{ // get the text inside this subelement and save it off
	  DOMElementHelper s_h = new DOMElementHelper((Element)n);
	  tmp.put(n.getNodeName(),s_h.getElementText());

	} // end if

      } // end for

      if (tmp.isEmpty())
	strings = Collections.EMPTY_MAP;
      else
	strings = Collections.unmodifiableMap(tmp);

    } // end if
    else // no replacement strings
      strings = Collections.EMPTY_MAP;

  } // end setConfiguration

  public Content create(RequestInput ri)
  {
    return new JSPSideBox(this,ri.getEngine(),ri.getUser());

  } // end create

} // end class JSPSideBoxFactory
