/*
 * 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.menus;

import java.io.Writer;
import java.io.IOException;
import java.util.*;
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;

public class CommunityMenu implements MenuComponent
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final TextItem s_unjoin = new TextItem("Unjoin");

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

  private String m_image_url;                     // path to actual image
  private String m_title;                         // title for menu
  private List m_items_list;                      // list of menu items
  private int m_cid;                              // community ID
  private boolean m_show_unjoin;                  // show the "Unjoin" menu choice?
  private MenuComponent m_unjoin = null;          // "unjoin" menu choice, or null

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

  CommunityMenu(CommunityContext ctxt, List items)
  {
    try
    { // retrieve the contact info for this puppy
      ContactInfo ci = ctxt.getContactInfo();
      m_image_url = ci.getPhotoURL();

    } // end try
    catch (DataException e)
    { // if we couldn't get the contact info, screw it
      m_image_url = null;

    } // end catch

    m_title = ctxt.getName();
    m_items_list = items;
    m_cid = ctxt.getCommunityID();
    m_show_unjoin = ctxt.canUnjoin();
    if (ctxt.canUnjoin())
      m_unjoin = new LinkItem("comm/unjoin.js.vs?cc=" + ctxt.getCommunityID(),LinkTypes.SERVLET,s_unjoin);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface MenuComponent
   *--------------------------------------------------------------------------------
   */

  public void render(RequestOutput out, Writer wr, MenuTemplate template, Set defines) throws IOException
  {
    HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));
    if (wr==null)
      wr = out.getWriter();
    template.writeBlock(wr,"pre-comm");
    wr.write(html.getCommunityLogoTag(m_image_url));
    template.writeBlock(wr,"title-pre-comm");
    template.renderTitle(wr,m_title,null);
    template.writeBlock(wr,"title-post-comm");

    if ((m_items_list.size()>0) || m_show_unjoin)
    { // write the items list
      template.writeBlock(wr,"items-pre-comm");
      for (Iterator it=m_items_list.iterator(); it.hasNext(); )
      { // display each menu item in turn
	MenuComponent mc = (MenuComponent)(it.next());
	mc.render(out,wr,template,defines);

      } // end for

      if (m_show_unjoin)
      { // write the "Unjoin" link
	if (m_items_list.size()>0)
	  template.writeBlock(wr,"unjoin-break");
	m_unjoin.render(out,wr,template,defines);

      } // end if

      template.writeBlock(wr,"items-post-comm");

    } // end if

    template.writeBlock(wr,"post-comm");

  } // end render

  public void render(RequestOutput out, Writer wr, MenuTemplate template) throws IOException
  {
    this.render(out,wr,template,Collections.EMPTY_SET);

  } // end render

  public String getTitle(RequestOutput out)
  {
    return m_title;

  } // end getTitle

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

  public final int getID()
  {
    return m_cid;

  } // end getID

} // end class CommunityMenu
