/*
 * 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.HTMLRendering;
import com.silverwrist.venice.ui.helpers.ImageHandler;

public class ConferenceBox implements SideBoxFactory
{
  /*--------------------------------------------------------------------------------
   * Internal class that implements the actual sidebox
   *--------------------------------------------------------------------------------
   */

  private class ConferenceBoxImpl implements ContentDirect
  {
    /*====================================================================
     * Attributes
     *====================================================================
     */

    private UserContext uc;     // current user context
    private List hotlist;       // current conference hotlist

    /*====================================================================
     * Constructor
     *====================================================================
     */

    ConferenceBoxImpl(UserContext uc) throws DataException
    {
      this.uc = uc;
      this.hotlist = uc.getConferenceHotlist();

    } // end constructor

    /*====================================================================
     * Implementations from interface Content
     *====================================================================
     */

    public boolean needFrame()
    {
      return false;

    } // end needFrame

    public int getMenuSelector()
    {
      return MENU_SELECTOR_NOCHANGE;

    } // end getMenuSelector

    public String getPageTitle(RequestOutput ro)
    {
      if (title_image!=null)
      { // return an image rendering
	ImageHandler ih = (uc.isLoggedIn() ? title_image : anon_title_image);
	return ih.getRendering(ro);

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

      } // end else

    } // end getPageTitle

    public String getPageQID()
    {
      return null;

    } // end getPageQID

    /*====================================================================
     * Implementations from interface ContentDirect
     *====================================================================
     */

    public void render(RequestOutput out) throws IOException
    {
      HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));

      String norm_font = html.getFontTag(html.SIDEBOX_CONTENT_FOREGROUND,"sidebox");

      if (hotlist.size()>0)
      { // write out the hotlist....
	String hilite = html.getFontTag(html.SIDEBOX_CONTENT_LINK,"sidebox");
	out.write("<TABLE ALIGN=CENTER BORDER=0 CELLPADDING=0 CELLSPACING=2>\n");
	Iterator it = hotlist.iterator();
	while (it.hasNext())
	{ // get the next hotlist entry and add it to the display
	  ConferenceHotlistEntry hle = (ConferenceHotlistEntry)(it.next());
	  ConferenceContext conf = hle.getConference();
	  String href = "conf/topics.js.vs?sig=" + conf.getEnclosingCommunity().getCommunityID() + "&conf="
	              + conf.getConfID();
	  out.write("<TR VALIGN=MIDDLE>\n<TD ALIGN=CENTER WIDTH=" + bullet.getWidth() + ">");
	  bullet.render(out);
	  out.write("</TD>\n<TD ALIGN=LEFT CLASS=\"sidebox\">\n" + norm_font
		    + "<B><A CLASS=\"sidebox\" HREF=\"" + html.formatURL(href,html.SERVLET) + "\">" + hilite
		    + StringUtil.encodeHTML(conf.getName()) + "</FONT></A></B> ("
		    + StringUtil.encodeHTML(conf.getEnclosingCommunity().getName()) + ")</FONT>\n");
	  if (conf.anyUnread())
	  { // write out the new-messages tag and its enclosing link
            href = "conf/read_new.js.vs?sig=" + conf.getEnclosingCommunity().getCommunityID() + "&conf="
                 + conf.getConfID();
	    out.write("&nbsp;<A HREF=\"" + html.formatURL(href,html.SERVLET) + "\">");
	    newmsg.render(out);
	    out.write("</A>\n");

	  } // end if

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

	} // end while

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

      } // end if
      else // write the "null" message
	out.write(norm_font + "<EM>" + StringUtil.encodeHTML(null_message) + "</EM></FONT>\n");

      if (uc.isLoggedIn())
      { // write the link at the end
	out.write("<P>" + html.getFontTag(html.SIDEBOX_CONTENT_FOREGROUND,"sidebox-footer")
		  + "<B>[ <A CLASS=\"sidebox\" HREF=\"" + html.formatURL("conf/hotlist.js.vs",html.SERVLET)
                  + "\">" + html.getFontTag(html.SIDEBOX_CONTENT_LINK,"sidebox-footer")
                  + StringUtil.encodeHTML(manage_link) + "</FONT></A> ]</B></FONT>");

      } // end if

    } // end render

  } // end class ConferenceBoxImpl

  /*--------------------------------------------------------------------------------
   * 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 null_message;            // message if they have no hotlist conferences
  private String manage_link;             // "Manage" link text
  private ImageHandler bullet;            // bullet image
  private ImageHandler newmsg;            // new message image

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

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

  /*--------------------------------------------------------------------------------
   * 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 conference list sidebox",cfg);
      title += ":";
      anon_title = cfg_h.getSubElementText("anon-title");
      if (anon_title==null)
	anon_title = title;
      else
	anon_title += ":";

    } // end else

    null_message = cfg_h.getSubElementText("null-msg");
    if (null_message==null)
      null_message = "You have no conferences in your hotlist.";

    manage_link = cfg_h.getSubElementText("manage-link");
    if (manage_link==null)
      manage_link = "Manage";

    bullet = new ImageHandler(cfg_h.getSubElement("bullet"),"purple-ball.gif","*",14,14);
    newmsg = new ImageHandler(cfg_h.getSubElement("new-image"),"tag_new.gif","New!",40,20);

  } // end setConfiguration

  public Content create(RequestInput ri) throws DataException
  {
    return new ConferenceBoxImpl(ri.getUser());

  } // end create

} // end class ConferenceBox
