// 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): 

importClass(java.awt.Dimension);
importPackage(java.util);
importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.except);
importPackage(Packages.com.silverwrist.venice.ui);
importPackage(Packages.com.silverwrist.venice.ui.conf);
importPackage(Packages.com.silverwrist.venice.ui.conf.view);
importPackage(Packages.com.silverwrist.venice.ui.helpers);

SCALING_NUM = 1;
SCALING_DENOM = 2;

// get the request object and the community
rinput = bsf.lookupBean("request");
comm = rinput.getCommunity(true,"top.js.vs");

// get the current conference
currc = new CurrentConference(rinput);
conf = currc.getConference(true,"conf/conferences.js.vs?cc=" + comm.communityID);

// get the current topic
on_error = "conf/topics.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID;
topic = currc.getTopic(true,on_error);

// if we need to restore posts in another topic, do so
read_new = true;
if (rinput.hasParameter("rtop") && rinput.hasParameter("rct"))
{ // retrieve the "other topic"
  other_topic = currc.getOptionalTopic("rtop");
  if (other_topic!=null)
  { // get the number of unread messages
    nunread = rinput.getParameterInt("rct",0);
    if ((nunread>0) && (nunread<=other_topic.totalMessages))
    { // set the unread message count
      try
      { // reset the unread messages
        other_topic.unreadMessages = nunread;

      } // end try
      catch (e)
      { // it didn't work - ignore the error
        logger.warn("exception setting unread messages: " + e);

      } // end catch

      if (topic.topicID==other_topic.topicID)
        read_new = false;  // don't do a read new if we just restored this topic

    } // end if
    else  // ignore this
      logger.warn("number of unread messages out of range");

  } // end if

} // end if

// get the post interval
piv = currc.getPostInterval(on_error);

// get the "read new" parameter
if (read_new)
  read_new = rinput.hasParameter("rnm");

rc = null;
try
{ // create the view
  rc = new PostsView(comm,currc);
  logger.debug(rc.unread + " unread messages");

  // fill in parameters
  rc.first = piv.first;
  rc.last = piv.last;
  rc.showAdvanced = rinput.hasParameter("shac");
  rc.noBozos = rinput.hasParameter("nbz");
  rc.messages = topic.getMessages(piv.first,piv.last);
  rc.topCustom = conf.getCustomBlock(ConferenceContext.CUST_BLOCK_TOP);
  rc.bottomCustom = conf.getCustomBlock(ConferenceContext.CUST_BLOCK_BOTTOM);
  rc.title = topic.name;
  html = vlib.queryHTMLRendering(rinput);
  rc.subtitle = topic.totalMessages + " Total; " + rc.unread + " New; Last: "
              + html.formatDate(topic.lastUpdateDate);

  // do a "read new" on this topic
  if (read_new)
    topic.unreadMessages = 0;

  // visit the topic
  visit_order = currc.getTopicVisitOrder();
  if (visit_order!=null)
    visit_order.visit(topic.topicNumber);
  rc.topicVisitOrder = visit_order;

  // get the aliases list and synthesize the topic stem and QID
  aliases = conf.aliases;
  tmp = aliases.get(0).toString() + "." + topic.topicNumber;
  rc.topicStem = tmp + ".";
  rc.pageQID = "go/" + comm.alias + "!" + tmp;

  if (conf.displayPostPictures() && rinput.user.displayPostPictures())
  { // set up the user p[hoto dimensions
    old_psz = rinput.engine.getUserPhotoSize();
    rc.photoDims = new Dimension((old_psz.width * SCALING_NUM) / SCALING_DENOM,
                                 (old_psz.height * SCALING_NUM) / SCALING_DENOM);

  } // end if

  rc.buildInternals(rinput.user);  // build the bozo filter and user photo lists in compiled code

} // end try
catch (e)
{ // handle an exception here
  etype = vlib.exceptionType(e) + "";
  if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error listing messages: " + e.message,on_error);
  else if (etype.match("AccessError"))
  { // unable to get message list - maybe we weren't logged in
    if (rinput.user.isLoggedIn())
      rc = new ErrorBox("Access Error",e.message,on_error);
    else
      rc = new LogInOrCreate();

  } // end else if
  else
    rc = e;

} // end catch

vlib.output(rc);  // all done!