package com.servlets;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.artifacts.FactoryProvider;
import com.dao.CategoriesDao;
import com.dao.CategoryDao;
import com.dao.ProductDao;
import com.entities.Categories;
import com.entities.Category;
import com.entities.Product;

@MultipartConfig
public class CategoriesTitle extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public CategoriesTitle() {
        super();
       
    }

	
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	response.setContentType("text/html;charset=UTF-8");
    	try(PrintWriter out = response.getWriter()){
    		try {
    			String op = request.getParameter("operation");
    			if (op.trim().equals("addcategories")) {
    				String title = request.getParameter("Ttitle");
    				
    				Categories categories = new Categories();
    				categories.setCategoriesTitle(title);
    				CategoriesDao categoriesDao = new CategoriesDao(FactoryProvider.getFactory());
    				int catId = categoriesDao.saveCategories(categories);
    				System.out.println(catId);
    				out.println("success");
    				
    				
    			}else if(op.trim().equals("addcategory")) {
    				String catTitle = request.getParameter("Ctitle");
    				int catId = Integer.parseInt(request.getParameter("catId"));
    				Category category = new Category();
    				category.setCategoryTitle(catTitle);
    				
    				CategoriesDao cdoa = new CategoriesDao(FactoryProvider.getFactory());
    				Categories categories = cdoa.getCategoriesById(catId);
    				category.setCategories(categories);
    				CategoryDao categoryDao = new CategoryDao(FactoryProvider.getFactory());
    				int catyId = categoryDao.saveCategory(category);
    				System.out.println(catyId);
    				out.println("success");
    				
    			}else if(op.trim().equals("addproducts")) {
    				String pName = request.getParameter("pname");
    				String pDesc = request.getParameter("pdesc");
    				int pPrice = Integer.parseInt(request.getParameter("pprice"));
    				int pDiscount = Integer.parseInt(request.getParameter("pdiscount"));
    				int catyId = Integer.parseInt(request.getParameter("catyId"));
    				Part part=request.getPart("pphoto");
    				
    				Product product =new Product();
    				product.setpName(pName);
    				product.setpDesc(pDesc);
    				product.setpPrice(pPrice);
    				product.setpDiscount(pDiscount);
    				product.setpPhoto(part.getSubmittedFileName());
    				
    				CategoryDao caDao = new CategoryDao(FactoryProvider.getFactory());
    				Category category = caDao.getCategoryById(catyId);
    				product.setCategory(category);
    				
    				ProductDao productDao = new ProductDao(FactoryProvider.getFactory());
    				int pId = productDao.saveProduct(product);
    				
    				@SuppressWarnings("deprecation")
					String path = request.getRealPath("images")+File.separator +"products"+File.separator + part.getSubmittedFileName();
    				System.out.println(path);
    				
    				try {
    				
    				FileOutputStream fos = new FileOutputStream(path);
    				InputStream is = part.getInputStream();
    				
    				byte []data = new byte[is.available()];
    				is.read(data);
    				
    				fos.write(data);
    				fos.close();
    				
    				}catch(Exception e) {
    					e.printStackTrace();
    				}
    				
    				
    				
    				System.out.println(pId);
    				
    				
    				
    				HttpSession httpSession=request.getSession();
    				httpSession.setAttribute("message", "Product added successfully !");
    				response.sendRedirect("bussiness.jsp");
    				
    				
    				
    				
    				}
    		}catch(Exception e) {
    			e.printStackTrace();
    		}
    		
    		}
	}

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		processRequest(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		processRequest(request, response);
	}

}
