/*
 * 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.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 CommunityBox implements SideBoxFactory
{
  /*--------------------------------------------------------------------------------
   * Internal class that implements the actual sidebox
   *--------------------------------------------------------------------------------
   */

  class CommunityBoxImpl implements ContentDirect
  {
    private UserContext uc;
    private List comm_list;

    CommunityBoxImpl(UserContext uc) throws DataException
    {
      this.uc = uc;
      this.comm_list = uc.getMemberCommunities();

    } // end constructor

    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

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

      if (comm_list.size()>0)
      { // load up the rendering data and render the contents of the list
	String link_font = html.getFontTag(html.SIDEBOX_CONTENT_LINK,"sidebox");
	out.write("<TABLE ALIGN=CENTER BORDER=0 CELLPADDING=0 CELLSPACING=2>\n");
	Iterator it = comm_list.iterator();
	while (it.hasNext())
	{ // write each community out in turn
	  CommunityContext comm = (CommunityContext)(it.next());
	  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<A CLASS=\"sidebox\" HREF=\""
		    + html.formatURL("community/" + comm.getAlias(),html.SERVLET) + "\">" + link_font + "<B>"
		    + StringUtil.encodeHTML(comm.getName()) + "</B></FONT></A>\n");

	  if (comm.isAdmin())
	  { // write the host tag at the end
	    out.write("&nbsp;");
	    host.render(out);

	  } // end if

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

	} // end while

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

      } // end if
      else // write the "no communities" message
	out.write(html.getFontTag(html.SIDEBOX_CONTENT_FOREGROUND,"sidebox") + "<EM>"
		  + StringUtil.encodeHTML(null_message) + "</EM></FONT>\n");

      if (uc.isLoggedIn())
      { // write the links at the end
	String hilite = html.getFontTag(html.SIDEBOX_CONTENT_LINK,"sidebox-footer");
	out.write("<P>" + html.getFontTag(html.SIDEBOX_CONTENT_FOREGROUND,"sidebox-footer")
		  + "<B>[ <A CLASS=\"sidebox\" HREF=\"" + html.formatURL("comm/sb_list.js.vs",html.SERVLET)
		  + "\">" + hilite + StringUtil.encodeHTML(manage_link) + "</FONT></A> ");
	if (uc.canCreateCommunity())
	  out.write("| <A CLASS=\"sidebox\" HREF=\"" + html.formatURL("comm/create.js.vs",html.SERVLET) + "\">"
		    + hilite + StringUtil.encodeHTML(create_new_link) + "</FONT></A> ");
	out.write("]</B></FONT>");

      } // end if

    } // end render

  } // end class CommunityBoxImpl

  /*--------------------------------------------------------------------------------
   * 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're not a member of any communities
  private String manage_link;             // "Manage" link text
  private String create_new_link;         // "Create New" link text
  private ImageHandler bullet;            // bullet image
  private ImageHandler host;              // host image

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

  public CommunityBox()
  { // 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 community 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 are not a member of any communities.";

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

    create_new_link = cfg_h.getSubElementText("create-new-link");
    if (create_new_link==null)
      create_new_link = "Create New";

    bullet = new ImageHandler(cfg_h.getSubElement("bullet"),"purple-ball.gif","*",14,14);
    host = new ImageHandler(cfg_h.getSubElement("host-image"),"tag_host.gif","Host!",40,20);

  } // end setConfiguration

  public Content create(VeniceEngine engine, UserContext uc) throws DataException
  {
    return new CommunityBoxImpl(uc);

  } // end create
  
} // end class CommunityBox
