package kr.co.jinwoosi.clfd.clfd.prod.web;

import kr.co.jinwoosi.clfd.clfd.dgns.model.Dgns;
import kr.co.jinwoosi.clfd.clfd.dgns.model.DgnsSearch;
import kr.co.jinwoosi.clfd.clfd.dgns.service.DgnsService;
import kr.co.jinwoosi.clfd.clfd.prod.service.ClfdProdService;
import kr.co.jinwoosi.clfd.clfd.site.service.ClfdSiteService;
import kr.co.jinwoosi.clfd.conts.prod.model.Prod;
import kr.co.jinwoosi.clfd.conts.prod.model.ProdSearch;
import kr.co.jinwoosi.clfd.framework.cd.model.CdDtl;
import kr.co.jinwoosi.clfd.framework.cd.service.CdDtlService;
import kr.co.jinwoosi.clfd.framework.security.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

@Controller
public class ClfdProdController {

    @Autowired
    private ClfdSiteService clfdSiteService;
    @Autowired
    private ClfdProdService clfdProdService;
    @Autowired
    private DgnsService dgnsService;
    @Resource(name = "cdDtlService")
    private CdDtlService cdDtlService;

    @RequestMapping(value = "/clfd/prod/list.do")
    public String clfdProdListPage(HttpServletRequest request, HttpServletResponse response, Model model,
                                   @RequestParam(value = "searchText", required = false) String searchText) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        model.addAttribute("user", user);

        if(user != null) {
            DgnsSearch dgnsSearch = new DgnsSearch();
            dgnsSearch.setUserId(user.getUserId());
            dgnsSearch.setDgnsCntRct("Y");
            // 사용자의 최근 테스트 결과 정보
            Dgns dgns = dgnsService.select(dgnsSearch);
            model.addAttribute("dgnsRct", dgns);
        }

        if (clfdSiteService.visitClfdSite(request, response, model) == null) {
            return "";
        }

        if (searchText == null) {
            searchText = "";
        }
        model.addAttribute("searchText", searchText);

        String[] prodTyArray = request.getParameterValues("prodTyArray");

        if (prodTyArray != null) {
            model.addAttribute("mProdTyList", new ArrayList(Arrays.asList(prodTyArray)));
        }

        String mSkTyCus = request.getParameter("skTyCus");
        model.addAttribute("mSkTyCus", mSkTyCus);

        String mSkTy = request.getParameter("skTy");
        model.addAttribute("mSkTy", mSkTy);

        String sortToken = request.getParameter("sortToken");
        if (sortToken == null) {
            sortToken = "";
        }
        model.addAttribute("sortToken", sortToken);

        String mSideBar = request.getParameter("mSideBar");
        if (mSideBar == null) {
            mSideBar = "off";
        }
        model.addAttribute("mSideBar", mSideBar);

        try {
            model.addAttribute("prodTyCdDtlList", cdDtlService.selectList(new CdDtl("prodTy")));
            model.addAttribute("skTyList", cdDtlService.selectList(new CdDtl("baumanTy")));
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return "clfd/prod/list";
    }

    @RequestMapping(value = "/clfd/prod/list.json")
    @ResponseBody
    public HashMap<String, Object> clfdProdListJson(HttpServletRequest request, ProdSearch prodSearch) {
        User user = (User) request.getSession().getAttribute("user");
        HashMap<String, Object> result = new HashMap<>();
        String[] prodTyArray = request.getParameterValues("prodTyArray[]");

        if (prodTyArray != null) {
            prodSearch.setProdTyList(new ArrayList(Arrays.asList(prodTyArray)));
        }

        prodSearch.setShowSt("Y");
        if (user == null) {
            result.put("prodList", clfdProdService.selectList(prodSearch));
        } else {
            result.put("prodList", clfdProdService.selectListWithLike(prodSearch, user.getUserId()));
        }
        result.put("result", "success");
        return result;
    }

    @RequestMapping(value = "/clfd/prod/dtl.do")
    public String clfdProdDtlPage(HttpServletRequest request, HttpServletResponse response, Model model,
                                  @RequestParam(value = "prod", required = false) Long prodId) {

        User user = (User) request.getSession().getAttribute("user");

        if (prodId == null) {
            return "";
        }

        ProdSearch prodSearch = new ProdSearch();
        prodSearch.setProdId(prodId);
        Prod prod;
        if (user == null) {
            prod = clfdProdService.select(prodSearch);
        } else {
            prod = clfdProdService.selectWithLike(prodSearch, user.getUserId());
        }

        if (prod == null) {
            return "";
        }

        if (clfdSiteService.visitClfdSite(request, response, model) == null) {
            return "";
        }
        try {
            model.addAttribute("prodTyCdDtlList", cdDtlService.selectList(new CdDtl("prodTy")));
            model.addAttribute("skTyList", cdDtlService.selectList(new CdDtl("baumanTy")));
            model.addAttribute("prod", prod);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return "clfd/prod/dtl";
    }

    @RequestMapping(value = "/clfd/recommend/prod/list.json", method = RequestMethod.GET)
    @ResponseBody
    public HashMap<String, Object> recommendProductList(ProdSearch prodSearch) {
        HashMap<String, Object> result = new HashMap<>();
        result.put("productGbn", cdDtlService.selectList(new CdDtl("prodTy")));
        result.put("recommendList", clfdProdService.selectList(prodSearch));
        result.put("result", "success");
        return result;
    }

}
