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

//-----------------have to do correct session as a little chnge is there now
//Assuming userObject is passed by session and productId is passed thru encodeURL
//One more thing --------multithreaded model or singlethreaded model

public class BidServlet extends HttpServlet {
    HttpSession session;
    int productId,cid;
    String pd,bf,bh,rsp,login,passwd;    //productDetails,bidForm,responsePage
    //UserObject usob;             //get from session bind by name "user"
    //ProductObject prob;          //get from session bind by name "product"
    BidPageGen bpg;
    ////BidQueryHandler bqh;


    public void init (ServletConfig config) throws ServletException {
	super.init(config);
	//bpg=new BidPageGen();
	//bqh=new BidQueryHandler();
	
    }

    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {
	
	bpg=new BidPageGen();
	////bqh=new BidQueryHandler();
	session = request.getSession(false);
	//session.putValue("user",new UserObject("sandeep","pandey"));
	
	if(session==null){
	    login="faltoo";
	    passwd="faltoo";
	    System.out.println("Has not get any Session u understanddddddddddddddddddddddddddddddddddddddddddddddddddddd");
	}
	else{

	    //    usob=(UserObject)session.getValue("user");
	    login=(String)session.getValue("userLogin");
	    passwd=(String)session.getValue("password");
	    System.out.println("Has got this login and passwd "+login+passwd);
	    System.out.println("u understandddddddddddddddddddddddddddd");
	}
	
	productId=Integer.parseInt(request.getParameter("productId"));
	//prob=session.getAttribute("product");
	//productId=prob.getId();
	cid=SharedObjects.qe.getCurrentBid(productId);

	response.setContentType("text/html");
	PrintWriter out=response.getWriter();
	pd=SharedObjects.qe.getProductDetailsPage(productId);
	bf=SharedObjects.qe.getBidFormPage(productId);
	bh=SharedObjects.qe.getBidDetailsPage(productId);
	
	rsp=pd+bh+bf;
	//rsp=bh;
	//System.out.println(rsp+productId);
	ProdHtmlGen.linksToPages(out);

	out.println(rsp);
	
	out.close();
	//bqh.closeCon();
	//have to concatenate Image of product if available

    }



    public void doPost(HttpServletRequest Request,HttpServletResponse Response)
	throws ServletException, IOException {
	
       String invalidpasswd=null,invalidbid=null,sucbid=null,invalidlogin=null;
       int loginstat;                   //1 if no such user else 0
        //have to do session funda
	//Assuming mybid is the button label in the form
       System.out.println("****************************************");
       Response.setContentType("text/html");
       PrintWriter out=Response.getWriter();
       session = Request.getSession(false);
       //cid=bqh.getCurrentBid(productId);
       if(Request.getParameter("username")==null){
	   invalidlogin=bpg.InvalidLoginPage();
	   out.println(invalidlogin);
       }
       else
	   {
	       if(login.equals(Request.getParameter("username"))){
		   System.out.println("Login in post "+login);
		   if(!passwd.equals(Request.getParameter("password"))){
		       System.out.println("Passwd in post if part  "+passwd);
		       System.out.println("password id this u give .........................."+Request.getParameter("password"));
		       invalidpasswd=bpg.InvalidPasswordPage();
		       out.println(invalidpasswd);
		   }
		   else{
		       if(Integer.parseInt(Request.getParameter("bidprice"))<=cid){
			   invalidbid=bpg.InvalidBidPage();
			   out.println(invalidbid);
		       }
		       else{
			   sucbid=bpg.SuccessfulBidPage();
			   out.println(sucbid);
			   System.out.println("++++++"+Request.getParameter("autobidbutton")+"||||||||||||||||||||||||||");
			   if (Request.getParameter("autobidbutton").indexOf("1")!=-1)
			       SharedObjects.qe.updateCurrentBid(productId,Integer.parseInt(Request.getParameter("bidprice")),login,Request.getParameter("autobidbutton"),Integer.parseInt(Request.getParameter("maxbid")));
			   else
			       SharedObjects.qe.updateCurrentBid(productId,Integer.parseInt(Request.getParameter("bidprice")),login,Request.getParameter("autobidbutton"),0);
			   //bqh.updateCurrentBid(productId,Integer.parseInt(Request.getParameter("bidprice")));
		       }
		   }
	       }
	       else
		   {
		       String nlog=Request.getParameter("username");
		       String npass=Request.getParameter("password");
		       loginstat=SharedObjects.qe.IsValidUser(nlog,npass);
		       System.out.println("|||||||||||||||||"+loginstat);
		       if(loginstat==1){
			   invalidlogin=bpg.InvalidLoginPage();
			   out.println(invalidlogin);
		       }
		       else
			   if(loginstat==0){
			       if(session==null){
				   //usob=new UserObject(nlog,npass);
				   session=Request.getSession(true);
				   session.putValue("userLogin",nlog);
				   session.putValue("password",npass);
				  System.out.println("Creating a new session");
				  System.out.println("Fucking problemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"); 
			       }
			       System.out.println("===============");
			       if(Integer.parseInt(Request.getParameter("bidprice"))<=cid){
				   System.out.println(cid);
				   invalidbid=bpg.InvalidBidPage();
				   out.println(invalidbid);
			       }
			       else{
				   sucbid=bpg.SuccessfulBidPage();
				   out.println(sucbid);
				   System.out.println("++++++"+Request.getParameter("autobidbutton")+"||||||||||||||||||||||||||");
				   if (Request.getParameter("autobidbutton").indexOf("1")!=-1)
				     SharedObjects.qe.updateCurrentBid(productId,Integer.parseInt(Request.getParameter("bidprice")),nlog,Request.getParameter("autobidbutton"),Integer.parseInt(Request.getParameter("maxbid")));
				 else
				     SharedObjects.qe.updateCurrentBid(productId,Integer.parseInt(Request.getParameter("bidprice")),nlog,Request.getParameter("autobidbutton"),0);
			       }
			   }
		   }
	   }
    }    
}    
//functions done by the servlet are:
    //Then check passwd verification
    //check if bid is ok or not
    //then call a fnc to insert it into the dbase
    

    //Works remaining are:
    //Autobid part
    //If max amount is OK or not-------------
    //Have to do shifting of bid from CurrentBid to BidHistory















