//package prodpackage;

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

public class ProdHtmlGen
{ 

  
  ProdHtmlGen() {}
    public static void addHtmlHeader(PrintWriter pw,String title){  
	
	    pw.println("<HTML><HEAD><TITLE>");
	    pw.println(title);
	    pw.println("</TITLE></HEAD><BODY BGCOLOR=\"#fffff\">");
	    linksToPages(pw);
    }

    
    public static void linksToPages(PrintWriter pw){
	pw.println("<HR><FONT SIZE=+0><CENTER>");
	pw.println( "<A HREF="+Config.httpInit+"UserMainServlet> <font size=3 color=\"plum\">Home</font> </a>&nbsp");
	pw.println("<A HREF="+Config.httpInit+"BrowseServlet?whereFrom=sell&parentName=Product> <font size=3 color=\"plum\">Sell </font></a>&nbsp");
	pw.println("<A HREF="+Config.httpInit+"BrowseServlet?whereFrom=browse&parentName=Product> <font size=3 color=\"plum\">Browse </font></a>&nbsp");
	pw.println("<A HREF="+Config.httpInit+"LoginServlet2><font size=3 color=\"plum\">Login </font></a>&nbsp");
	pw.println("<A HREF="+Config.httpInit+"SearchServlet><font size=3 color=\"plum\">Search </font></a>&nbsp");
	pw.println("<A HREF="+Configure.HTTP_URL+"tut.htm><font size=3 color=\"plum\">Help</font></a>&nbsp");
	// pw.println("<A HREF=> <font size=3 color=\"plum\">Help</font></a>&nbsp");
	pw.println("</FONT></CENTER><HR><FONT SIZE=+0>");
    }
    
	
    public static void addHtmlFooter(PrintWriter pw){ 
	pw.println("</BODY></HTML>");
	}

    public static void showChilds(PrintWriter pw,Vector v,String browseOrSell){
	int size=v.size();
	String s=null;
	String news=null;
	String url=null;
	//Outputing a table of childs
	pw.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"  bgcolor=\"#CCCCCC\">");
        for(int i=0;i<size;i++){
	    s=(String) v.elementAt(i);
	    System.out.println(s);
	    System.out.println(" browseOrSell is" +browseOrSell);
	    // Replacing all occurences on blanks in the url by '+'. That's how we send a parameter whose value contains a blank
	    news=s.replace(' ','+');
	    //System.out.println(news);
	    //System.out.println(Config.httpInit);
	    url="<A HREF="+Config.httpInit+"BrowseServlet?whereFrom="+browseOrSell+"&parentName="+ news+">"+ s+"</A>";
	    System.out.println("url is "+url);
	    
	    //Outputing a table row
	    pw.print("<tr bgcolor=\"#EFEFEF\"><td>");
	    pw.print(url);
	    pw.print("</td></tr>");
	}
	pw.println("</table>");
    }
    
    /*
      Whenever sysads wishes to browse thru categories, he clicks on a category name. On clicking, all the childs  in that category are shown alongwith the link to add category in a particular child.
      
     */


    public static void showChildsWithAddingOption(PrintWriter pw,Vector v,String whereFrom,String sysOption){
	int size=v.size();
	String s=null;
	String news=null;
	String url=null;
	String addAddress=null;
	
	//Outputing a table of childs
	pw.println("<table>");
        for(int i=0;i<size;i++){
	    s=(String) v.elementAt(i);
	    news=s.replace(' ','+');
	    // url="<A HREF=http://144.16.111.4:5018/servlet/BrowseServlet?whereFrom="+whereFrom+"&parentName="+ news+">"+ s+"</A>";
	    // addAddress="<A HREF=http://144.16.111.4:5018/servlet/AddCategoryServlet?parentName="+ news+">"+"<span style=\"color:plum\">Add Category</span>"+"</A>";
	    //System.out.println(Config.httpInit);
	    url="<A HREF="+Config.httpInit+"BrowseServlet?whereFrom="+whereFrom+"&parentName="+ news+"&sysadOption="+sysOption+">"+ s+"</A>";
	   addAddress="<A HREF="+Config.httpInit+"AddCategoryServlet?parentName="+ news+">"+"<span style=\"color:plum\">Add Category</span>"+"</A>";   
	    

	 //Outputing a table row
	    pw.print("<tr>");
	    pw.print("<td>"+ url+"</td>");
	    pw.print("<td>"+ addAddress+"</td>");
	    pw.print("</tr>");
	}
	pw.println("</table>");
    }

    /*
      Whenever sysads wishes to delete categories, he clicks on a category name. On clicking, all the childs  in that category are shown alongwith the link to add category in a particular child.
      
     */

    public static void showChildsWithDeletingOption(PrintWriter pw,Vector v,String whereFrom,String sysOption){
	int size=v.size();
	String s=null;
	String news=null;
	String url=null;
	String addAddress=null;
	
	//Outputing a table of childs
	showMessage(pw,"Warning! You are going to delete categories");
	pw.println("<table>");
        for(int i=0;i<size;i++){
	    s=(String) v.elementAt(i);
	    news=s.replace(' ','+');
	    url="<A HREF="+Config.httpInit+"BrowseServlet?whereFrom="+whereFrom+"&parentName="+ news+"&sysadOption="+sysOption+">"+ s+"</A>";
	   addAddress="<A HREF="+Config.httpInit+"DeleteCategoryServlet?parentName="+ news+">"+"<span style=\"color:plum\">Delete Category</span>"+"</A>";   
	    
	 //Outputing a table row
	    pw.print("<tr>");
	    pw.print("<td>"+ url+"</td>");
	    pw.print("<td>"+ addAddress+"</td>");
	    pw.print("</tr>");
	}
	pw.println("</table>");
    }



    public static void showCategoryAdditionForm (PrintWriter pw,String parentName){
	pw.println("Parent Category Name: <b><span style=\"color:plum\">"+parentName+"</span></b><br>");
	pw.println("<form action="+Config.httpInit+"AddCategoryServlet"+" method=\"POST\">");
	pw.println("<input type=\"hidden\" name=parentName value="+"\""+parentName+"\""+">");
	pw.println("<TABLE>");
	
	pw.println("<TR>");
       	pw.println("<TD> <P>Enter Name of new Sub-Category</P> </TD>");
	pw.println("<TD> <P><INPUT TYPE=text NAME=subcatname SIZE=40></P> </TD>");
	pw.println("<TD></TD>");
	pw.println("</TR>");
	
	pw.println("<TR>");
	pw.println("<TD><p>Feature Number</p></TD>");
	pw.println("<TD> <P><span style=\"color:red\">Enter the name of Feature</span></P> </TD>");
	pw.println("<TD> <P><span style=\"color:red\">Type of Feature (String/Integer)</span></P> </TD>");
	pw.println("</TR>");
	
	int succ=0;
	for(int i=0;i<20;i++){
	    succ=i+1;
	    pw.println("<TR>");
	    pw.println("<TD><p>"+succ +"</p></TD>");
	    pw.println("<TD> <P><INPUT TYPE=text VALUE=\"\" NAME=feature"+i+" SIZE=40></P> </TD>");
	    pw.println("<TD>");
	    pw.println("<select name=\"type"+i+"\""+ ">");
	    pw.println("<option value=\"String\"><b>String</b> </option>");
	    pw.println("<option value=\"Integer\"><b>Integer</b> </option>");
	    pw.println("</select>");
	    pw.println("</TD>");
	    pw.println("</TR>");
	}
	
	pw.println("<TD><INPUT TYPE=submit VALUE=send > </TD><TD> <INPUT TYPE=reset value=reset></td><td></td>");
	pw.println("</TABLE>");    
	pw.println("</FORM>");
    }
    
    /*
      1. This function generates the Html page containing information about products placed for auction in category name.
      2. Details of each product appear in a row.
      3. Details include 
     */

    public static void showProductDetailsTable (PrintWriter pw,String catName, Vector prodDetailsVec){
	String prodId=null;
	String prodName=null;
	String location=null;
	String maxBid=null;
	String noOfBids=null;
	String closingDate=null; 
	int size=prodDetailsVec.size();
	
	if(size == 0){
	    showMessage(pw,"No products currently for auction in this category");
	}
	else{
	    showMessage(pw,"Products placed for auction in "+catName+" are as follows: ");
	
	    // System.out.println("Generating HTML file in showProductDetailsTable, no. of products are "+size);
	    pw.println("<TABLE bgcolor=\"#ffffff\">");
	    pw.println("<TR>");
	    pw.println("<TD bgcolor=\"#CCCCFF\"> Number</TD>");
	    pw.println("<TD bgcolor=\"#CCCCFF\"> Product Name </TD>");
	    pw.println("<TD bgcolor=\"#CCCCFF\" > Location </TD>");
	    pw.println("<TD bgcolor=\"#CCCCFF\"> Maximum bid </TD>");
	    pw.println("<TD bgcolor=\"#CCCCFF\"> No.of Bids </TD>");
	    pw.println("<TD bgcolor=\"#CCCCFF\"> Closing Date </TD>");
	    pw.println("</TR>");
	    for(int i=0;i<size;i++){
		Vector elementVec= (Vector)prodDetailsVec.elementAt(i); // This vector has product details of a particular product
		prodId= (String)elementVec.elementAt(0);
		prodName= (String)elementVec.elementAt(1);
		location= (String)elementVec.elementAt(2);
		maxBid= (String)elementVec.elementAt(3);
		noOfBids= (String)elementVec.elementAt(4);
		closingDate= (String)elementVec.elementAt(5);
		System.out.println("Product details "+ i +" "   + prodId+ " " + prodName+ " " + location+ " "+ maxBid+ " " + noOfBids+ " "+closingDate);
		pw.println("<TR bgcolor=\"#EFEFEF\">");
	    
		pw.println("<TD>");
		pw.print(i+1);
		pw.print("</TD>");

		pw.println("<TD>");
		pw.print("<A HREF="+Config.httpInit1+"BidServlet?productId="+prodId+">"+ prodName+"</A>");
		pw.print("</TD>");
	    
		pw.println("<TD>");
		pw.print(location);
		pw.print("</TD>");
	    
		pw.println("<TD>");
		pw.print(maxBid);
		pw.print("</TD>");

		pw.println("<TD>");
		pw.print(noOfBids);
		pw.print("</TD>");

		pw.println("<TD>");
		pw.print(closingDate);
		pw.print("</TD>");
		pw.println("</TR>");
	    
	    }	
	    pw.println("</TABLE>");
	}    
    }
    
    public static void showSysadOptions (PrintWriter pw){
	pw.println("<FONT SIZE=+9 color=\"blue\"><B><CENTER>Sysad Interface</CENTER></B></FONT>");
	pw.println("<HR><FONT SIZE=+0><CENTER>");
	pw.println("<A HREF="+Config.httpInit+"BrowseServlet?whereFrom=sysad&parentName=Product&sysadOption=1> <font size=3 color=\"teal\">Add Category To Database</font> </a>&nbsp");
	pw.println("<br><br>");
	pw.println("<A HREF="+Config.httpInit+"BrowseServlet?whereFrom=sysad&parentName=Product&sysadOption=2> <font size=3 color=\"teal\">Delete Category To Database</font> </a>&nbsp");
	pw.println("</FONT></CENTER><HR><FONT SIZE=+0>");
    }
    public static void showMessage (PrintWriter pw,String mesg){
	pw.println(mesg);
    }
    


}




