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

import database.*;
import cart.ShoppingCart;

public class MusicCatalogServlet extends HttpServlet { 

    public void doGet (HttpServletRequest request,
                       HttpServletResponse response)
	throws ServletException, IOException
    {
	int Start = 0;

        // 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> Musics Catalog </title></head>" +
		    "<body bgcolor=#fffed0>" +
		    "<font color=\"#ff0000\"><center><h3>The Music U Want ... </h3></center></font>");

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

        // Additions to the shopping cart
        String musicToAdd = request.getParameter("Buy");
        if (musicToAdd != null) {
            MusicDetails music = database.getMusicDetails(musicToAdd);

            cart.add(musicToAdd, music, "music");
            out.println("<p>" +
                        "<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 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 Musics</a></th>" +
                        "</tr></table");
        }

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

	out.println("" +
		    "<TABLE cellspacing=2 cellpadding=2 border=0 width=\"95%\">" +
		    "<TR>" +
		    "<TD colspan=3>" +
		    "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=0 BORDER=0>" +
		    "<TR>");

        // Additions to the shopping cart
        String Page = request.getParameter("Page");
        if (Page == null) {
	  out.println("" +
		    "<TD colspan=3> <B>1</B> " +
		    " <A href=\"catalog?Page=2\" onClick=\"return SubmitPage('2')\">2</A> ");
	  Start = 0;
	}

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

	if(Page != null){
	  if(Page.equals(one)) {
	    out.println("" +
			"<TD colspan=3> <B>1</B> " +
			" <A href=\"catalog?Page=2\" onClick=\"return SubmitPage('2')\">2</A> ");
	    Start = 0;
	  }
	  if(Page.equals(two)) {
	    out.println("" +
			"<TD colspan=3> <A href=\"catalog?Page=1\" onClick=\"return SubmitPage('2')\">1</A> " +
			" <B>2</B> ");
	    Start = 5;
	  }
	}

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


        MusicDetails[] musics = database.getMusicsSortedByTitle();
        int numMusics = database.getNumberOfMusics();
        for(int i=Start; i < (Start + 5); i++) {
            String musicId = musics[i].getMusicId();

            //Print out info on each music in its own two rows
            out.println("" +
			"<TR>" +
			"<TD width=\"60%\">" +
			"<B><A href=\"" +
			response.encodeUrl("/servlet/musicdetails?musicId=" 
					   + musicId)+ 
			"\">" + musics[i].getTitle() + "</A></B>" +
			"</TD>" +
			"<TD align=right valign=top nowrap>" +

			"</TD>" +
			"<TD align=right valign=baseline nowrap>" +
			"<A href=\"" +
			response.encodeUrl("/servlet/catalog?Buy="+musicId)+
			"\"><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[i].getPrice() + "<BR><SPAN class=SizeOneDown><B>Authors</B>: " + musics[i].getFirstName() + " " + musics[i].getSurname() + "<BR>" +
			"<B>Company</B>"+musics[i].getCompany()+"<BR>" +
			"<B>Review:</B>"+musics[i].getDescription()+"<BR>" +
			"</SPAN>" +
			"</TD>" +
			"</TR>" +
			"</TABLE><P>" +
			"</TD>" +
			"</TR>");

        }

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

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

    }
}
