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

import database.*;
import cart.ShoppingCart;

public class BookCatalogServlet 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> Books Catalog </title></head>" +
		    "<body bgcolor=#fffed0>" +
		    "<font color=\"#ff0000\"><center><h3>Books We Have ...</h3></center></font>");

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

        // Additions to the shopping cart
        String bookToAdd = request.getParameter("Buy");
        if (bookToAdd != null) {
            BookDetails book = database.getBookDetails(bookToAdd);

            cart.add(bookToAdd, book, "book");
            out.println("" +
                        "<font color=\"#ff0000\">" +
                        "Added <i>" + book.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 align=\"left\"><a href=\"" +
                        response.encodeUrl("/servlet/showcart") +
                        "\"> Check Shopping Cart</a></th>" +
                        "<th>&nbsp;</th>" +
                        "<th align=\"right\"><a href=\"" +
                        response.encodeUrl("/servlet/cashier") +
                        "\"> Buy your Books</a></th>" +
                        "</tr></table");
        }

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

        // Additions to the shopping cart
        String Page = request.getParameter("Page");
        if (Page == null) {
	  out.println("" +
		    "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<B>1</B>" +
		    "&nbsp&nbsp<A href=\"bookcatalog?Page=2\">2</A>");
	  Start = 0;
	  CurrPage = 1;
	}

	String two = "2";
	String one = "1";

	if(Page != null){
	  if(Page.equals(one)) {
	    out.println("" +
			"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<B>1</B>" +
			"&nbsp&nbsp<A href=\"bookcatalog?Page=2\">2</A>");
	    Start = 0;
	    CurrPage = 1;
	  }
	  if(Page.equals(two)) {
	    out.println("" +
			"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<A href=\"bookcatalog?Page=1\">1</A>" +
			"&nbsp&nbsp<B>2</B>");
	    Start = 5;
	    CurrPage = 2;
	  }
	}

	out.println("" +
		    "<BR><SPAN class=SizeTwoDown> page count: 2</SPAN><hr>");

	out.println("" +
		    "<TABLE cellspacing=2 cellpadding=2 border=0 width=\"95%\">");

        BookDetails[] books = database.getBooksSortedByTitle();
        int numBooks = database.getNumberOfBooks();
        for(int i=Start; i < (Start + 6); i++) {
            String bookId = books[i].getBookId();

            //Print out info on each book in its own two rows
            out.println("" +
			"<TR>" +
			"<TD colspan=4>" + // width=\"60%\">" +
			"<B><A href=\"" +
			response.encodeUrl("/servlet/bookdetails?bookId=" 
					   + bookId)+ 
			"\">" + books[i].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/bookcatalog?Buy="+ bookId + "&Page=" + CurrPage)+
			"\"><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[i].getPrice() + "<BR><SPAN class=SizeOneDown><B>Authors</B>: " + books[i].getFirstName() + " " + books[i].getSurname() + "<BR>" +
			"<B>Publish Date</B>:" + books[i].getYear() + "<BR>" +
			"<B>Review</B>:" + books[i].getDescription() +"<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.";

    }
}
