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

import database.*;
import cart.MyCart;


public class MyCatalogServlet 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);
	MyCart cart = (MyCart)session.getValue(session.getId());

        // If the user has no cart, create a new one
        if (cart == null) {
            cart = new MyCart();
            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> Product Catalog </title></head>" +

                    "<body  bgcolor=\"#ffffff\">" +
                    "<center>" +
                    "<hr> <br> &nbsp;" +
                    "<h1>" +
                    "<font size=\"+3\" color=\"red\">My </font>" +
                    "<font size=\"+3\" color=\"purple\">Store</font>" +
                    "</h1>" +
                    "</center>" +
                    "<br> &nbsp; <hr> <br> &nbsp;");

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

        // Additions to the shopping cart
        String productToAdd = request.getParameter("Buy");
        if (productToAdd != null) {
            ProductDetails product = database.getProductDetails(productToAdd);

            cart.add(productToAdd, product);
            out.println("<p><h3>" +
                        "<font color=\"#ff0000\">" +
                        "You just added <i>" + product.getName() + "</i> "+
                        "to your shopping cart</font></h3>");
        }

        //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 Selections</a></th>" +
                        "</tr></table");
        }

        // Always prompt the user to buy more -- get and show the catalog
        out.println("<br> &nbsp;" +
                    "<h3>Please choose from our selections</h3>" +
                    "<center> <table>");

        ProductDetails[] products = database.getProductsSortedByName();
        int numProducts = database.getNumberOfProducts();
        for(int i=0; i < numProducts; i++) {
            String productId = products[i].getProductID();

            //Print out info on each book in its own two rows
            out.println("<tr>" +

                        "<td bgcolor=\"#ffffaa\">" +
                        "<a href=\"" +
                        response.encodeUrl("/servlet/productdetails?productId=" +
                                           productId) +
                        "\"> <strong>" + products[i].getName() +
                        " </strong></a></td>" +

                        "<td bgcolor=\"#ffffaa\" rowspan=2>" +
                        "$" + products[i].getPrice() +
                        " </td>" +

                        "<td bgcolor=\"#ffffaa\" rowspan=2>" +
                        "<a href=\"" +
                        response.encodeUrl("/servlet/mycatalog?Buy=" + productId)
                        + "\">; Add to Cart &nbsp;</a></td></tr>");
        }

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

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

    }
}















