import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import database.*;
import cart.*;

public class ShowCartServlet extends HttpServlet { 

    public void doGet (HttpServletRequest request,
                       HttpServletResponse response)
	throws ServletException, IOException
    {
        // 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();

        //Print out the response
        out.println("<html>" +
                    "<head><title>Your Shopping Cart</title></head>" +

                    "<body  bgcolor=\"#fffed0\">" +
                    "<center>" +
                    "<h3>" +
                    "<font color=#ff0000>Your Shopping Till Now ... </font>" +
                    "</h3>" +
                    "</center>" +
                    "<hr> <br> &nbsp;");

        String Id =request.getParameter("Remove");
        if (Id != null) {
	    ShoppingCartItem itm = (ShoppingCartItem) cart.getItemByKey(Id);

	    String type = itm.getType();
	    if(type.equals("book")){
	      BookDBServlet database = (BookDBServlet)
                getServletConfig().getServletContext().getServlet("bookdb");
	      BookDetails book = database.getBookDetails(Id);
	      
	      out.println("<font color=\"#ff00000\" size=\"+2\">" +
			  "You just removed: <strong>" + book.getTitle() +
			  "</strong> <br> &nbsp; <br>" +
			  "</font>");
	    }
	    if(type.equals("music")){
	      MusicDBServlet database = (MusicDBServlet)
                getServletConfig().getServletContext().getServlet("musicdb");
	      MusicDetails music = database.getMusicDetails(Id);
	      
	      out.println("<font color=\"#ff00000\" size=\"+2\">" +
			  "You just removed: <strong>" + music.getTitle() +
			  "</strong> <br> &nbsp; <br>" +
			  "</font>");

	    }
            cart.remove(Id);
            

        } else if (request.getParameter("Clear") != null) {
            cart.clear();
            
            out.println("<font color=\"#ff0000\" size=\"+2\"><strong>" +
                        "You just cleared your shopping cart!" +
                        "</strong> <br>&nbsp; <br> </font>");
        }

        // Print a summary of the shopping cart
        int num = cart.getNumberOfItems();
        if (num > 0) {

            // Return the Shopping Cart Nice and Pretty
            out.println("<table cellpadding=1>" +
                        "<tr><font=\"+2\">" +
                        "<th align=left>Title</TH>" +
                        "<th align=left>Price</TH>" +
                        "<th align=left>Quantity</TH>" +
                        "<th align=left>Amount</TH>" +
                        "</font></tr>");
            
            Enumeration e = cart.getItems();
            while (e.hasMoreElements()) {
                ShoppingCartItem item = (ShoppingCartItem) e.nextElement();
		String type =  item.getType();

		if(type.equals("book")){
		  BookDetails bookDetails = (BookDetails) item.getItem();

		  out.println("<tr>" +
			      
			      "<td bgcolor=\"#fffed0\">" +
			      "<strong><a href=\"" +
			      response.encodeUrl("/servlet/bookdetails?bookId=" +
						 bookDetails.getBookId()) +
			      "\">" + bookDetails.getTitle() + "</a></strong>" +
			      "</td>" +
			      
			      "<td bgcolor=\"#fffed0\" align=\"right\">" +
			      Cashier.format(bookDetails.getPrice()) +
			      "</td>" +

			      "<td align=\"right\">" +
			      item.getQuantity() +
			      "</td>" +

			      "<td bgcolor=\"#fffed0\" align=\"right\">" +
			      Cashier.format(bookDetails.getPrice() * item.getQuantity()) +
			      "</td>" +
			      
			      "<td bgcolor=\"#fffed0\">" +
			      "<strong>" + "<a href=\"" +
			      response.encodeUrl("/servlet/showcart?Remove=" +
						 bookDetails.getBookId())
			      + "\">Remove Item</a></strong>" +
			      "</td></tr>");
		  
		}
		if(type.equals("music")){
		  MusicDetails musicDetails = (MusicDetails) item.getItem();
		  
		  out.println("<tr>" +
			      
			      "<td bgcolor=\"#fffed0\">" +
			      "<strong><a href=\"" +
			      response.encodeUrl("/servlet/musicdetails?musicId=" +
						 musicDetails.getMusicId()) +
			      "\">" + musicDetails.getTitle() + "</a></strong>" +
			      "</td>" +
			      
			      "<td bgcolor=\"#fffed0\" align=\"right\">" +
			      Cashier.format(musicDetails.getPrice()) +
			      "</td>" +

			      "<td align=\"right\" bgcolor=\"#fffed0\">" +
			      item.getQuantity() +
			      "</td>" +

			      "<td bgcolor=\"#fffed0\" align=\"right\">" +
			      Cashier.format(musicDetails.getPrice() * item.getQuantity()) +
			      "</td>" +
			      
			      "<td bgcolor=\"#fffed0\">" +
			      "<strong>" + "<a href=\"" +
			      response.encodeUrl("/servlet/showcart?Remove=" +
						 musicDetails.getMusicId())
			      + "\">Remove Item</a></strong>" +
			      "</td></tr>");
		  
		}


            }

            // Print the total at the bottom of the table
            Cashier cashier = new Cashier(cart);
            out.println("<tr><td colspan=\"5\" bgcolor=\"#fffed0\">" +
                        "<br></td></tr>" +

                        "<tr>" +
                        "<td colspan=\"3\" align=\"right\"" +
                        "bgcolor=\"#fffed0\">" +
                        "Subtotal:</td>" +
                        "<td bgcolor=\"#fffed0\" align=\"right\">" +
                        cashier.format(cashier.getAmount()) + "</td>" +
                        "</td><td><br></td></tr>" +

                        "<tr>" +
                        "<td colspan=\"3\" align=\"right\"" +
                        "bgcolor=\"#fffed0\">" +
                        "CA Sales Tax:</td>" +
                        "<td bgcolor=\"#fffed0\" align=\"right\">" +
                        cashier.format(cashier.getTax()) + "</td>" +
                        "</td><td><br></td></tr>" +

                        "<tr>" +
                        "<td colspan=\"3\" align=\"right\"" +
                        "bgcolor=\"fffed0\">" +
                        "<font color=\"ff0000\">" +
                        "<strong>Grand Total:</strong></font></td>" +
                        "<td bgcolor=\"fffed0\" align=\"right\">" +
                        cashier.format(cashier.getTotal()) + "</td>" +
                        "</td><td><br></td></tr>" +
                        "</table>");
            
            // Where to go and what to do next
            out.println("<p> &nbsp; <p><a href=\"" +
                        response.encodeUrl("/servlet/cashier") +
                        "\">Check Out</a> &nbsp; &nbsp; &nbsp;" +
                        
                        "<a href=\"" + 
                        response.encodeUrl("/servlet/showcart?Clear=clear") +
                        "\">Clear Cart</a>");
        } else {

            // Shopping cart is empty!
            out.println("<font size=\"+2\">" +
                        "There is nothing in your shopping cart.</font>" +
                        "<br> &nbsp; <br>" +
                        "<center><a href=\""+
                        response.encodeUrl("/servlet/catalog") +
                        "\">Back to the Catalog</a> </center>");
        }

        out.println("</body> </html>");
        out.close();
    }
        
    
    public String getServletInfo() {
        return "The ShowCart servlet returns information about" +
               "the books that the user is in the process of ordering.";
    }
}




