/*
 * 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) 2002-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.content;

import java.util.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;
import com.silverwrist.venice.frame.FramedContent;

public class ConfirmBox implements FramedContent
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final long TIMEOUT = 60000;  // 1 minute

  private static Random s_rng;

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

  private String m_title;             // confirm box title
  private String m_message;           // confirm box message
  private int m_confirmation_number;  // confirmation number
  private String m_yes_type;          // "yes" link type
  private String m_yes_url;           // "yes" link URL
  private String m_no_type;           // "no" link type
  private String m_no_url;            // "no" link URL
  private java.util.Date m_created;   // this box was created when?

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

  public ConfirmBox(Request r, String attr_namespace, String attr_name, String param_name, String title,
		    String message, String yes_url_type, String yes_url, String no_url_type, String no_url)
  {
    // Fill in all the parameters except m_created.
    m_title = title;
    m_message = message;
    m_confirmation_number = s_rng.nextInt(0x1000000);
    m_yes_type = yes_url_type;
    m_yes_url = yes_url;
    if (m_yes_url.indexOf('?')>=0)
      m_yes_url += ("&" + param_name + "=" + m_confirmation_number);
    else
      m_yes_url += ("?" + param_name + "=" + m_confirmation_number);
    m_no_type = no_url_type;
    m_no_url = no_url;

    // Save this object in a session attribute.
    SessionInfoProvider prov = (SessionInfoProvider)(r.queryService(SessionInfoProvider.class));
    prov.getSessionInfo().setObject(attr_namespace,attr_name,this);

    // Now start the timer ticking...
    m_created = new java.util.Date();

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  protected boolean doConfirm(int num)
  {
    java.util.Date now = new java.util.Date();
    if ((now.getTime() - m_created.getTime())>TIMEOUT)
      return false;  // confirmation has timed out
    return (num==m_confirmation_number);

  } // end doConfirm

  /*--------------------------------------------------------------------------------
   * Implementations from interface FramedContent
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns the desired menu selector for this page.  The <EM>menu selector</EM> is a string that specifies
   * which menu is to be displayed on the left menu bar, in addition to the "global menu."  This value may
   * be <CODE>null</CODE>, in which case the current menu selector is not changed.
   *
   * @return The desired menu selector.
   */
  public String getMenuSelector()
  {
    return null;

  } // end getMenuSelector

  /**
   * Returns the desired title for the page.  This title is concatenated with the <EM>site title</EM> (set in
   * global properties) to form the actual page title that gets sent to the browser.
   *
   * @return The desired page title.
   */
  public String getPageTitle()
  {
    return m_title;

  } // end getPageTitle

  /**
   * Returns the desired quick ID for the page.  The <EM>page quick ID</EM> is a small text string which can be used
   * to "tag" the page with a unique identifier, for use with external tools such as offsite hit counters.
   * If this value is <CODE>null</CODE>, the quick ID is not used for this page.
   *
   * @return The desired page quick ID.
   */
  public String getPageQID()
  {
    return null;

  } // end getPageQID

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

  public static boolean isConfirmed(Request r, String attr_namespace, String attr_name, String param_name)
  {
    // First, retrieve the confirm box out of the session data.
    ConfirmBox cb = null;
    RequestHelper rhelp = new RequestHelper(r);
    try
    { // look up the session data and retrieve the confirmation box
      SessionInfo session = rhelp.getSession();
      cb = (ConfirmBox)(session.getObject(attr_namespace,attr_name));
      session.removeObject(attr_namespace,attr_name);

    } // end try
    catch (NoSuchObjectException e)
    { // there's no confirm box there
      return false;

    } // end catch
    catch (ClassCastException cce)
    { // there's something there but it's not a confirm box
      return false;

    } // end catch

    // Now get the confirmation number out of the parameters.
    int num = rhelp.getParameterInt(param_name,-1);
    if (num<0)
      return false;  // there's no hope this will match

    return cb.doConfirm(num);

  } // end isConfirmed

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

  /**
   * Returns the title of the confirmation message box.
   *
   * @return The title of the confirmation message box.
   */
  public String getTitle()
  {
    return m_title;

  } // end getTitle

  /**
   * Returns the confirmation message text.
   *
   * @return The confirmation message text.
   */
  public String getMessage()
  {
    return m_message;

  } // end getMessage

  public String getYesLinkType()
  {
    return m_yes_type;

  } // end getYesLinkType

  public String getYesLink()
  {
    return m_yes_url;

  } // end getYesLink

  public String getNoLinkType()
  {
    return m_no_type;

  } // end getNoLinkType

  public String getNoLink()
  {
    return m_no_url;

  } // end getNoLink

  /*--------------------------------------------------------------------------------
   * Static initializer
   *--------------------------------------------------------------------------------
   */

  static
  { // initialize the random number generator
    s_rng = new Random(System.currentTimeMillis());

  } // end static initializer

} // end class ConfirmBox
