package control;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.CartMod;
import model.CartProduct;
import model.ProductForCart;
import model.SanPham;

public class Cart extends HttpServlet {
	private static final long serialVersionUID = 2L;
public Cart(){
}
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html; charset=UTF-8");
    	try {
            HttpSession session = request.getSession();
            String user = (String)session.getAttribute("user");
            CartMod giohang = null;
            Object obj = session.getAttribute("giohang");    
            if (obj != null) {
                giohang = (CartMod) obj;
            }
            String cartRep = request.getParameter("rep");

            if (cartRep.equals("buy")) {
            	int id = Integer.parseInt(request.getParameter("id"));
            	String qtt = request.getParameter("qtt");
            	int soluong = 0;
            	Pattern qttpt = Pattern.compile("^[0-9]+(([0-9])?[0-9]*)*$");
            	Matcher qttmt = qttpt.matcher(qtt);
            	if (qttmt.matches()){            	
                soluong = Integer.parseInt(qtt);
            	}
            	if (soluong < 1) { soluong = 1;}
                SanPham spmod = new SanPham();
                ProductForCart sp = spmod.ProductForCart(id); //Lay San Pham Tu DB cho Cart
               String TrangThai =  sp.getStatus();
               if(TrangThai == null){TrangThai = "HetHang";}
                if (TrangThai.equals("HetHang")){
                	response.sendRedirect("ProductId?id="+id);
                } else {
                CartProduct product = new CartProduct(sp, soluong);
                if (giohang == null) { //Mua lan dau tien
                    giohang = new CartMod();
                }
                giohang.ThemMonHang(product);
                }
            } else if (cartRep.equals("discard")) {
                String id = request.getParameter("id"); // Id San Pham Can Loai Bo
                CartProduct mh=new CartProduct(new ProductForCart(id, "","", 0f), 0);
                giohang.XoaMonhang(mh);
                
            } else if (cartRep.equals("updateqtt")) {
            	int id = Integer.parseInt(request.getParameter("id"));
            	String qtt = request.getParameter("qtt");
            	int soluong = 0;
            	Pattern qttpt = Pattern.compile("^[0-9]+(([0-9])?[0-9]*)*$");
            	Matcher qttmt = qttpt.matcher(qtt);
            	if (qttmt.matches()){            	
                soluong = Integer.parseInt(qtt);
            	}
            	if (soluong < 1) { soluong = 1;}
                SanPham spmod = new SanPham();
                ProductForCart sp = spmod.ProductForCart(id); //Lay San Pham Tu DB cho Cart
                String TrangThai =  sp.getStatus();
                if(TrangThai == null){TrangThai = "HetHang";}
                 if (TrangThai.equals("HetHang")){
                 	response.sendRedirect("ProductId?id="+id);
                 } else {
                CartProduct product = new CartProduct(sp, soluong);
                if (giohang == null) { //Mua lan dau tien
                    giohang = new CartMod();
                }
                giohang.CapNhatSoLuong(product);
                
            }}
            
            else if (cartRep.equals("checkout")) {
            	if(user==null){ 
            		//response.sendRedirect("index.jsp?customer=checkout");
            		response.sendRedirect("index.jsp?customer=checkout");
            	}
                response.sendRedirect("index.jsp?customer=checkout");
            }
       
            session.setAttribute("giohang", giohang);
            
            if (cartRep.equals("clear")) {
            	session.removeAttribute("obj");
                session.removeAttribute("giohang");           
                response.sendRedirect("index.jsp?customer=mycart");
             } 
            response.sendRedirect("index.jsp?customer=mycart");
        } catch (Exception e) {
            //e.printStackTrace();
            response.getWriter().print(e.getMessage());
        }
    }
}
