package edu.rice.rubis.beans.servlets;

import edu.rice.rubis.beans.*;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.rmi.PortableRemoteObject;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.rmi.RemoteException;
import java.util.GregorianCalendar;
import java.util.Enumeration;
import java.net.URLEncoder;

/** In fact, this class is not a servlet itself but it provides
 * output services to servlets to send back HTML files.
 * @author <a href="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a href="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
 * @version 1.0
 */

public class ServletPrinter
{
  private PrintWriter       out;
  private String            servletName;
  private GregorianCalendar startDate;


  /**
   * Creates a new <code>ServletPrinter</code> instance.
   *
   * @param toWebServer a <code>HttpServletResponse</code> value
   * @param callingServletName a <code>String</code> value
   */
  public ServletPrinter(HttpServletResponse toWebServer, String callingServletName)
  {
    startDate = new GregorianCalendar();
    toWebServer.setContentType("text/html");
    try
    {
      out = toWebServer.getWriter();
    }
    catch (IOException ioe)
    {
      ioe.printStackTrace();
    }
    servletName = callingServletName;
  }

  void printFile (String filename)
  {
    FileReader fis = null;
    try
    {
      fis = new FileReader(filename);
      char[] data = new char[4*1024]; // 4K buffer
      int    bytesRead;
      bytesRead = fis.read(data);
      while (/*(bytesRead = fis.read(data))*/ bytesRead != -1)
      {
        out.write(data, 0, bytesRead);
	bytesRead = fis.read(data);
      }
    }
    catch (Exception e)
    {
      out.println("Unable to read file (exception: "+e+")<br>\n");
    }
    finally
    {
      if (fis != null)
        try
        {
          fis.close();
        }
      catch (Exception ex)
      {
        out.println("Unable to close the file reader (exception: "+ex+")<br>\n"); 
      }
    }
  }

  void printHTMLheader(String title)
  {
    printFile(Config.HTMLFilesPath+"/header.html");
    out.println("<title>"+title+"</title>\n");
  }

  void printHTMLfooter()
  {
    GregorianCalendar endDate = new GregorianCalendar();

    out.println("<br><hr>RUBiS (C) Rice University/INRIA<br><i>Page generated by "+servletName+" in "+TimeManagement.diffTime(startDate, endDate)+"</i><br>\n");
    out.println("</body>\n");
    out.println("</html>\n");	
  }

  void printHTML(String msg)
  {
    out.println(msg);
  }

  void printHTMLHighlighted(String msg)
  {
    out.println("<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n");
    out.println("<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>"+msg+"</B></FONT></TD></TR>\n");
    out.println("</TABLE><p>\n");
  }

  

  void printItemHeader()
  {
    out.println("<TABLE border=\"1\" summary=\"List of items\">\n"+
                "<THEAD>\n"+
                "<TR><TH>Designation<TH>Price<TH>Bids<TH>End Date<TH>Bid Now\n"+
                "<TBODY>\n");
  }


  void printItemFooter(String URLprevious, String URLafter)
  {
    out.println("</TABLE>\n");
    out.println("<p><CENTER>\n"+URLprevious+"\n&nbsp&nbsp&nbsp"+URLafter+"\n</CENTER>\n");
  }

  /** 
   * Item footer printed function
   *
   * @since 1.1
   */
  void printItemFooter()
  {
    out.println("</TABLE>\n");
  }



}
