
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import database.*;
import cart.ShoppingCart;

public class HotProductsServlet extends HttpServlet { 

    public void doGet (HttpServletRequest request,
                       HttpServletResponse response)
	throws ServletException, IOException
    {
	int Start = 0;
	int CurrPage = 1;
        // Get the user's session and shopping cart
	HttpSession session = request.getSession(true);
	ShoppingCart cart = (ShoppingCart)session.getValue(session.getId());

        // If the user has no cart, create a new one
        if (cart == null) {
            cart = new ShoppingCart();
            session.putValue(session.getId(), cart);
        }

	// set content-type header before accessing the Writer
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

	// then write the data of the response
        out.println("<html>" +
		    "<head><title> Hot Products </title></head>" +
		    "<body bgcolor=#fffed0>" +
		    "<font color=\"#ff0000\"><center><h2>Hot Products ...</h2></center></font><br>");

        //Data on the books are from the database servlet
        BookDBServlet bookdatabase = (BookDBServlet)
        getServletConfig().getServletContext().getServlet("bookdb");


        //Data on the books are from the database servlet
        MusicDBServlet musicdatabase = (MusicDBServlet)
        getServletConfig().getServletContext().getServlet("musicdb");

        // Additions to the shopping cart
        String itemToAdd = request.getParameter("Buy");
        String type = request.getParameter("Type");

        if (itemToAdd != null) {
	  if(type.equals("book")){
            BookDetails book = bookdatabase.getBookDetails(itemToAdd);
            cart.add(itemToAdd, book, "book");
            out.println("" +
                        "<font color=\"#ff0000\">" +
                        "Added <i>" + book.getTitle() + "</i> "+
                        "to Shopping Cart</font>");
	  }
	  if(type.equals("music")){
            MusicDetails music = musicdatabase.getMusicDetails(itemToAdd);
            cart.add(itemToAdd, music, "music");
            out.println("" +
                        "<font color=\"#ff0000\">" +
                        "Added <i>" + music.getTitle() + "</i> "+
                        "to Shopping Cart</font>");
	  }

        }


	//Give the option of checking cart or checking out if cart not empty
        if (cart.getNumberOfItems() > 0) {
            out.println("<table><tr>" +
                        "<th>&nbsp;</th>" +
                        "<th align=\"right\"><a href=\"" +
                        response.encodeUrl("/servlet/cashier") +
                        "\"> Pay for the items ...</a></th>" +
                        "</tr></table");
        }                



        // Always prompt the user to buy more -- get and show the catalog

	BookDetails[] books = bookdatabase.getBooksSortedByTitle();
	String bookId = books[2].getBookId();

	MusicDetails[] musics = musicdatabase.getMusicsSortedByTitle();
	String musicId = musics[2].getMusicId();

	out.println("" +
                    "<hr><br><TABLE cellspacing=2 cellpadding=2 border=0 width=\"95%\">" +
		    "<TR>" +
		    "<TD colspan=4>" + // width=\"60%\">" +
		    "<B><A href=\"" +
		    response.encodeUrl("/servlet/bookdetails?bookId=" 
				       + bookId)+ 
		    "\">" + books[2].getTitle() + "</A></B>" +
		    "</TD>" +
		    "<TD valign=top nowrap>" +
		    "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
		    "</TD>" +
		    "<TD align=right valign=baseline nowrap>" +
		    "<A href=\"" +
		    response.encodeUrl("/servlet/hot?Buy="+ bookId + "&Type=book")+
		    "\"><SPAN class=SizeOneDown><B>Add To Shopping Cart</B></SPAN>" +
		    "</A>" +
		    "</TD>" +
		    "</TR>" +
		    "<TR>" +
		    "<TD colspan=4>" +
		    "<TABLE CELLSPACING=\"0\" CELLPADDING=\"0\">" +
		    "<TR>" +
		    "<TD>  </TD>" +
		    "<TD>" +
		    "<B>Price Range:</B>" + books[2].getPrice() + "<BR><SPAN class=SizeOneDown><B>Authors</B>: " + books[2].getFirstName() + " " + books[2].getSurname() + "<BR>" +
		    "<B>Publish Date</B>:" + books[2].getYear() + "<BR>" +
		    "<B>Review</B>:" + books[2].getDescription() +"<BR>" +
		    "</SPAN>" +
		    "</TD>" +
		    "</TR>" +
		    "</TABLE><P>" +
		    "</TD>" +
		    "</TR>" +




		    "<TR>" +
		    "<TD colspan=4>" + // width=\"60%\">" +
		    "<B><A href=\"" +
		    response.encodeUrl("/servlet/musicdetails?musicId=" 
				       + musicId)+ 
		    "\">" + musics[2].getTitle() + "</A></B>" +
		    "</TD>" +
		    "<TD valign=top nowrap>" +
		    "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +
		    "</TD>" +
		    "<TD align=right valign=baseline nowrap>" +
		    "<A href=\"" +
		    response.encodeUrl("/servlet/hot?Buy="+ musicId + "&Type=music")+
		    "\"><SPAN class=SizeOneDown><B>Add To Shopping Cart</B></SPAN>" +
		    "</A>" +
		    "</TD>" +
		    "</TR>" +
		    "<TR>" +
		    "<TD colspan=4>" +
		    "<TABLE CELLSPACING=\"0\" CELLPADDING=\"0\">" +
		    "<TR>" +
		    "<TD>  </TD>" +
		    "<TD>" +
		    "<B>Price Range:</B>" + musics[2].getPrice() + "<BR><SPAN class=SizeOneDown><B>Singers</B>: " + musics[2].getFirstName() + " " + musics[2].getSurname() + "<BR>" +
		    "<B>Company</B>:" + musics[2].getCompany() +"<BR>" +
		    "</SPAN>" +
		    "</TD>" +
		    "</TR>" +
		    "</TABLE><P>" +
		    "</TD>" +
		    "</TR>");


        out.println("</table></body></html>");
        out.close();
    }

    public String getServletInfo() {
        return "The Catalog servlet adds books to the user's " +
               "shopping cart and prints the catalog.";

    }
}
