package com.csnf.controller;

import com.csnf.entity.Product;
import com.csnf.service.IProductService;
import com.csnf.service.ProductService;
import com.csnf.util.PageUtil;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

@WebServlet(name = "productctl", value = "/product")
public class ProductController extends HttpServlet {
    private Logger logger = Logger.getLogger(ProductController.class);
    private IProductService productService = new ProductService();
    private PageUtil pageUtil = new PageUtil();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        String pagesize = req.getParameter("size");
        String currentPage = req.getParameter("page");
        try {
            pageUtil.setPageSize(Integer.parseInt(pagesize));
        }catch (NumberFormatException e){
            pageUtil.setPageSize(10);
        }
        try {
            pageUtil.setCurrentPage(Integer.parseInt(currentPage));
        }catch (NumberFormatException e){
            pageUtil.setCurrentPage(1);
        }
        pageUtil.setTotalCount(productService.getAllProduct(null).size());
        pageUtil.setTotalPage(pageUtil.getTotalCount() % pageUtil.getPageSize() == 0
                ? pageUtil.getTotalCount() / pageUtil.getPageSize() : pageUtil.getTotalCount() / pageUtil.getPageSize() + 1);
        pageUtil.setStartIndex(pageUtil.getPageSize()*(pageUtil.getCurrentPage()-1));
        //将分页信息存入request域中
        req.setAttribute("pageUtil", pageUtil);
        switch (action) {
            case "add":
                addProduct(new Product());
                req.setAttribute("productList", listProduct(pageUtil));
                req.getRequestDispatcher("/WEB-INF/jsp/product/index.jsp").forward(req, resp);
                break;
            case "list":
                req.setAttribute("productList", listProduct(pageUtil));
                req.getRequestDispatcher("/WEB-INF/jsp/product/index.jsp").forward(req, resp);
                break;
            case "edit":
                editProduct(new Product());
                req.setAttribute("productList", listProduct(pageUtil));
                req.getRequestDispatcher("/WEB-INF/jsp/product/index.jsp").forward(req, resp);
                break;
            case "delete":
                deleteProduct(Integer.valueOf(req.getParameter("productId")));
                req.setAttribute("productList", listProduct(pageUtil));
                req.getRequestDispatcher("/WEB-INF/jsp/product/index.jsp").forward(req, resp);
                break;
        }
    }

    /**
     * 添加商品
     *
     * @param product
     */
    private void addProduct(Product product) {
        logger.info("addProduct");
    }

    /**
     * 查询商品列表
     *
     * @return
     */
    private List<Product> listProduct(PageUtil pageUtil) {
        List<Product> productList = productService.getAllProduct(pageUtil);
        return productList;
    }

    private void editProduct(Product product) {
        logger.info("editProduct");
    }

    private void deleteProduct(Integer productId) {
        logger.info("deleteProduct");
    }

    private Product productDetails(Integer productId) {
        logger.info("productDetails");
        return null;
    }
}
