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

import java.util.*;
import java.io.IOException;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.ui.*;

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

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

  private static Random rng;

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

  private int conf_num;
  private String title;
  private String message;
  private String confirm_url;
  private int confirm_url_type;
  private String deny_url;
  private int deny_url_type;
  private Date created;

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

  public ConfirmBox(RequestInput ri, String attr_name, String param_name, String title,
		    String message, String confirm_url, int confirm_url_type, String deny_url,
		    int deny_url_type)
  {
    // Fill in all the parameters except created.
    this.conf_num = rng.nextInt(0x1000000);
    this.title = title;
    this.message = message;
    this.confirm_url = confirm_url + "&" + param_name + "=" + this.conf_num;
    this.confirm_url_type = confirm_url_type;
    this.deny_url = deny_url;
    this.deny_url_type = deny_url_type;

    // Stash this object in a session variable.
    ri.setSessionAttribute(attr_name,this);

    // Now start the timer ticking...
    this.created = new Date();

  } // end constructor

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

  protected boolean doConfirm(int test_num)
  {
    Date now = new Date();
    if ((now.getTime()-created.getTime())>TIMEOUT)
      return false;
    return (test_num==conf_num);

  } // end doConfirm

  /*--------------------------------------------------------------------------------
   * Implementations from interface Content
   *--------------------------------------------------------------------------------
   */

  public boolean needFrame()
  {
    return true;

  } // end needFrame

  public int getMenuSelector()
  {
    return MENU_SELECTOR_NOCHANGE;

  } // end getMenuSelector

  public String getPageTitle(RequestOutput ro)
  {
    return title;

  } // end getPageTitle

  public String getPageQID()
  {
    return null;

  } // end getPageQID

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentDirect
   *--------------------------------------------------------------------------------
   */

  public void render(RequestOutput out) throws IOException
  {
    HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));
    out.write("<P><TABLE ALIGN=CENTER WIDTH=\"70%\" BORDER=1 CELLPADDING=2 CELLSPACING=1><TR VALIGN=MIDDLE>"
              + "<TD ALIGN=CENTER BGCOLOR=\"" + html.getColor(html.CONFIRM_TITLE_BACKGROUND) + "\">\n"
	      + html.getFontTag(html.CONFIRM_TITLE_FOREGROUND,"confirmbox") + StringUtil.encodeHTML(title)
	      + "</FONT>\n</TD></TR><TR VALIGN=MIDDLE><TD ALIGN=CENTER>\n"
	      + html.getFontTag(html.CONTENT_FOREGROUND,"confirmbox") + "<P>" + StringUtil.encodeHTML(message)
	      + "<P>\n<A HREF=\"" + html.formatURL(confirm_url,confirm_url_type) + "\">"
	      + html.getButtonVisual("yes") + "</A>&nbsp;&nbsp;\n<A HREF=\""
	      + html.formatURL(deny_url,deny_url_type) + "\">" + html.getButtonVisual("no")
	      + "</A>\n</FONT></TD></TR></TABLE><P>\n");

  } // end render

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

  public static final boolean isConfirmed(RequestInput ri, String attr_name, String param_name)
  {
    // First, get the ConfirmBox out of the session data
    Object obj = ri.getSessionAttribute(attr_name);
    ri.setSessionAttribute(attr_name,null);  // we can only get it out once!
    if ((obj==null) || !(obj instanceof ConfirmBox)) // this is not a valid ConfirmBox!
      return false;
    ConfirmBox cb = (ConfirmBox)obj;

    // Now get the confirmation number out of the URL parameters.
    String test_num_str = ri.getParameter(param_name);
    if (test_num_str==null)
      return false;  // no confirmation!

    int test_num;
    try
    { // convert to an integer
      test_num = Integer.parseInt(test_num_str);

    } // end try
    catch (NumberFormatException nfe)
    { // if the confirmation number is bogus, we're h0sed
      return false;

    } // end catch

    return cb.doConfirm(test_num);

  } // end isConfirmed

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

  static
  { // initialize the random number generator
    Date now = new Date();
    rng = new Random(now.getTime());

  } // end static initializer

} // end class ConfirmBox
