
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;
	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> Musics Catalog </title></head>" +
		    "<body bgcolor=#fffed0>" +
		    "<font color=\"#ff0000\"><center><h3>Musics We Have ...</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("" +
                        "<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 Music CD</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=\"musiccatalog?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=\"musiccatalog?Page=2\">2</A>");
	    Start = 0;
	    CurrPage = 1;
	  }
	  if(Page.equals(two)) {
	    out.println("" +
			"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<A href=\"musiccatalog?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%\">");

        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 colspan=4>" + // width=\"60%\">" +
			"<B><A href=\"" +
			response.encodeUrl("/servlet/musicdetails?musicId=" 
					   + musicId)+ 
			"\">" + musics[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/musiccatalog?Buy="+ musicId + "&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>" + musics[i].getPrice() + "<BR><SPAN class=SizeOneDown><B>Singers</B>: " + musics[i].getFirstName() + " " + musics[i].getSurname() + "<BR>" +
			"<B>Company</B>:" + musics[i].getCompany() +"<BR>" +
			"</SPAN>" +
			"</TD>" +
			"</TR>" +
			"</TABLE><P>" +
			"</TD>" +
			"</TR>");

        }

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

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

    }
}
