/*
 * 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): 
 */
package com.silverwrist.venice.ui.conf;

import java.util.*;
import com.silverwrist.venice.core.*;

public class TopicVisitOrder
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private short[] topics;        // list of topic numbers
  private boolean[] unread;      // which ones have any unread messages that we know about?
  private int ndx_next;          // index of the "next" topic entry

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

  TopicVisitOrder(List topiclist)
  {
    this.topics = new short[topiclist.size()];
    this.unread = new boolean[topiclist.size()];

    ndx_next = -1;
    for (int i=0; i<topiclist.size(); i++)
    { // fill in topic numbers and unread states
      TopicContext t = (TopicContext)(topiclist.get(i));
      topics[i] = t.getTopicNumber();
      unread[i] = (t.getUnreadMessages()>0);
      if (unread[i] && (ndx_next<0))
	ndx_next = i;

    } // end for

  } // end constructor

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

  public final short getNext()
  {
    if (ndx_next<0)
      return -1;
    else if (unread[ndx_next])
      return topics[ndx_next];
    else
      return -1;

  } // end getNext

  public final boolean isNext()
  {
    if (ndx_next>=0)
      return unread[ndx_next];
    else
      return false;

  } // end isNext

  public final void visit(short topnum)
  {
    if (ndx_next<0)
      return;

    int i;
    if (topics[ndx_next]!=topnum)
    { // go searching for the matching topic number
      for (i=0; i<topics.length; i++)
      { // simple linear search - can't be tricky because the topic numbers might be
	// in any order
	if (topnum==topics[i])
	{ // plant ourselves right there
	  unread[i] = false;
	  ndx_next = i;
	  break;

	} // end if

      } // end for

    } // end if
    else
      unread[ndx_next] = false;

    // move the "next" pointer
    i = ndx_next;
    do
    { // move forward to next "unread" topic
      if (unread[i])
      { // found an unread topic - point it here and return
	ndx_next = i;
	return;

      } // end if

      if (++i==unread.length)
	i = 0;  // wrap around

    } while (i!=ndx_next);  // end do

    ndx_next = -1;  // no more unread topics

  } // end visit

} // end class TopicVisitOrder
