package com.style24.admin.biz.web;

import java.util.Collection;

import com.gagaframework.web.parameter.GagaMap;
import com.gagaframework.web.util.GagaDateUtil;
import com.style24.admin.biz.service.TsaBusinessService;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.persistence.TscPageRequest;
import com.style24.persistence.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.style24.admin.biz.service.TsaDisplayService;
import com.style24.admin.biz.service.TsaRendererService;
import com.style24.admin.support.controller.TsaBaseController;
import com.style24.core.support.message.TscMessageByLocale;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.rest.server.GagaResponse;

import javax.servlet.http.HttpServletRequest;

/**
 * 전시 Controller
 *
 * @author
 * @since 2020. 12. 16
 */
@Controller
@RequestMapping("/display")
@Slf4j
public class TsaDisplayController extends TsaBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TsaRendererService rendererService;

	@Autowired
	private TsaDisplayService displayService;

	@Autowired
	private TsaBusinessService businessService;

	/**
	 * 카테고리관리 화면
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 4
	 */
	@GetMapping("/category/form")
	public ModelAndView categoryForm() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("siteList", rendererService.getAvailCommonCodeList("G000"));
		mav.addObject("cateGbList", rendererService.getAvailCommonCodeList("G032"));
		mav.addObject("cateTypeList", rendererService.getAvailCommonCodeList("G031"));
		mav.addObject("formalGbList", rendererService.getAvailCommonCodeList("G009"));
		mav.addObject("contentsLocList", rendererService.getAvailCommonCodeList("G028"));

		mav.setViewName("display/CategoryForm");

		return mav;
	}

	/**
	 * 카테고리 목록
	 * @param category - 카테고리 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 4
	 */
	@PostMapping("/category/list")
	@ResponseBody
	public Collection<Category> getCategoryList(@RequestBody Category category) {
		return displayService.getCategoryList(category);
	}

	/**
	 * 카테고리 저장
	 * @param category - 카테고리 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 4
	 */
	@PostMapping("/category/save")
	@ResponseBody
	public GagaResponse saveCategory(@RequestBody Category category) {
		displayService.saveCategory(category);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 카테고리 갱신
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 4
	 */
	@PostMapping("/category/refresh")
	@ResponseBody
	public GagaResponse refreshCategory() {
		displayService.refreshCategory();
		return super.ok(message.getMessage("SUCC_0010"));
	}

	/**
	 * 품목카테고리매핑관리 화면
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 5
	 */
	@GetMapping("/itemkind/category/mapping/form")
	public ModelAndView itemkindCategoryMappingForm() {
		ModelAndView mav = new ModelAndView();

		// 카테고리구분
		mav.addObject("cateGbList", rendererService.getAvailCommonCodeList("G032"));

		mav.setViewName("display/ItemkindCategoryForm");

		return mav;
	}

	/**
	 * 카테고리구분 목록
	 * @param cateGb - 카테고리구분
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 6
	 */
	@GetMapping("/category/gb/list/{cateGb}")
	@ResponseBody
	public Collection<Category> getCategoryGbList(@PathVariable String cateGb) {
		return displayService.getCategoryGbList(cateGb);
	}

	/**
	 * 품목카테고리매핑 저장
	 * @param itemkindCateList - 품목카테고리 목록
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 6
	 */
	@PostMapping("/itemkind/category/mapping/save")
	@ResponseBody
	public GagaResponse saveItemkindCategoryMappingList(@RequestBody Collection<ItemkindCategory> itemkindCateList) {
		if (itemkindCateList == null || itemkindCateList.isEmpty()) {
			throw new IllegalStateException(message.getMessage("FAIL_1001"));
		}

		displayService.saveItemkindCategoryMappingList(itemkindCateList);

		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 품목카테고리매핑 삭제
	 * @param itemkindCateList - 품목카테고리 목록
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 6
	 */
	@PostMapping("/itemkind/category/mapping/delete")
	@ResponseBody
	public GagaResponse deleteItemkindCategoryMappingList(@RequestBody Collection<ItemkindCategory> itemkindCateList) {
		if (itemkindCateList == null || itemkindCateList.isEmpty()) {
			throw new IllegalStateException(message.getMessage("FAIL_1004"));
		}

		displayService.deleteItemkindCategoryMappingList(itemkindCateList);

		return super.ok(message.getMessage("SUCC_0003"));
	}

	/**
	 * 품목카테고리매핑 목록
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 5
	 */
	@GetMapping("/itemkind/category/mapping/list/{itemkindCd}")
	@ResponseBody
	public Collection<ItemkindCategory> getItemkindCategoryMappingList(@PathVariable String itemkindCd) {
		return displayService.getItemkindCategoryMappingList(itemkindCd);
	}

	/**
	 * 카테고리 목록 화면(팝업)
	 * @param category
	 * @return
	 * @author xyzp1539
	 * @since 2021. 01. 11
	 */
	@GetMapping("/category/popup/form")
	public ModelAndView categorySearchForm(Category category) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("siteList", rendererService.getAvailCommonCodeList("G000"));
		mav.addObject("cateGbList", rendererService.getAvailCommonCodeList("G032"));
		mav.addObject("cateTypeList", rendererService.getAvailCommonCodeList("G031"));
		mav.addObject("formalGbList", rendererService.getAvailCommonCodeList("G009"));
		mav.addObject("conentsLocList", rendererService.getAvailCommonCodeList("G028"));
		mav.addObject("params", category);

		mav.setViewName("display/CategorySearchForm");
		return mav;
	}

	/**
	 * 상품별카테고리전시관리 화면
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 13
	 */
	@GetMapping("/goods/category/form")
	public ModelAndView goodsCategoryForm() {
		ModelAndView mav = new ModelAndView();

		// 상품상태
		String[] exceptCds = {"G008_00"};
		mav.addObject("goodsStatList", rendererService.getCommonCodeList("G008", "Y", exceptCds));
		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList());
		// MD
		mav.addObject("mdIdList", rendererService.getBrandMdList());
		// 정상이월
		mav.addObject("formalGbList", rendererService.getAvailCommonCodeList("G009"));
		// 품목
		mav.addObject("itemkindList", rendererService.getAllItemkindList());
		// 년도
		int toYear = Integer.parseInt(GagaDateUtil.getToday("yyyy")) - 4;
		mav.addObject("styleYearList", rendererService.getYearList(toYear, 0, 5));
		// 시즌
		mav.addObject("seasonList", rendererService.getAvailCommonCodeList("G006"));

		// 카테고리 등록/수정
		mav.addObject("cateGbList", rendererService.getAvailCommonCodeList("G032"));

		mav.setViewName("display/GoodsCategoryForm");

		return mav;
	}

	/**
	 * 상품별카테고리전시관리 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 15
	 */
	@PostMapping("/goods/category/list")
	@ResponseBody
	public GagaMap getGoodsCategoryList(@RequestBody GoodsCategory goodsCategory) {
		GagaMap result = new GagaMap();

		goodsCategory.setRegNo(TsaSession.getInfo().getUserNo()); // 엑셀조회시 로그인 사용자의 엑셀 상품조회시 사용
		goodsCategory.setPageable(new TscPageRequest(goodsCategory.getPageNo() - 1, goodsCategory.getPageSize()));
		goodsCategory.getPageable().setTotalCount(displayService.getGoodsCategoryListCount(goodsCategory));

		result.set("pageing", goodsCategory);
		result.set("goodsCategoryList", displayService.getGoodsCategoryList(goodsCategory));

		return result;
	}

	/**
	 * 상품별카테고리전시 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 17
	 */
	@PostMapping("/goods/category/save")
	@ResponseBody
	public GagaResponse saveGoodsCategory(@RequestBody GoodsCategory goodsCategory) {
		log.info("saveGoodsCategory goodsCategory::::::::{}",goodsCategory);
		displayService.saveGoodsCategory(goodsCategory);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 상품별카테고리 리스트 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 18
	 */
	@GetMapping("/goods/category/view/list")
	@ResponseBody
	public Collection<GoodsCategory> getGoodsCategoryViewList(@RequestParam(value = "goodsCd")String goodsCd) {
		return displayService.getGoodsCategoryViewList(goodsCd);
	}

	/**
	 * 상품별카테고리 삭제
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 18
	 */
	@PostMapping("/goods/category/delete")
	@ResponseBody
	public GagaResponse deleteGoodsCategory(@RequestBody GoodsCategory goodsCategory) {
		displayService.deleteGoodsCategoryList(goodsCategory);
		return super.ok(message.getMessage("SUCC_0003"));
	}

	/**
	 * 카테고리별상품전시관리 화면
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 19
	 */
	@GetMapping("/category/goods/form")
	public ModelAndView CategoryGoodsForm() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("siteList", rendererService.getAvailCommonCodeList("G000"));
		// 카테고리구분
		mav.addObject("cateGbList", rendererService.getAvailCommonCodeList("G032"));

		mav.addObject("cateTypeList", rendererService.getAvailCommonCodeList("G031"));
		mav.addObject("formalGbList", rendererService.getAvailCommonCodeList("G009"));
		mav.addObject("conentsLocList", rendererService.getAvailCommonCodeList("G028"));
		mav.addObject("goodsStatList", rendererService.getAvailCommonCodeList("G008"));

		mav.setViewName("display/CategoryGoodsForm");

		return mav;
	}

	/**
	 * 카테고리별상품 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 27
	 */
	@PostMapping("/category/goods/list")
	@ResponseBody
	public GagaMap getCategoryGoodsList(@RequestBody CategoryGoods categoryGoods) {
		GagaMap result = new GagaMap();

		categoryGoods.setRegNo(TsaSession.getInfo().getUserNo());	//엑셀조회시 로그인 사용자의 엑셀 상품조회시 사용
		categoryGoods.setPageable(new TscPageRequest(categoryGoods.getPageNo() - 1, categoryGoods.getPageSize()));
		categoryGoods.getPageable().setTotalCount(displayService.getCategoryGoodsListCount(categoryGoods));

		result.set("pageing", categoryGoods);
		result.set("categoryGoodsList", displayService.getCategoryGoodsList(categoryGoods));

		return result;
	}

	/**
	 * 카테고리별상품전시 상품이동/복사
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 15
	 */
	@PostMapping("/category/goods/move")
	@ResponseBody
	public GagaResponse moveCategoryGoods(@RequestBody CategoryGoods categoryGoods){
		categoryGoods.setRegNo(TsaSession.getInfo().getUserNo());
		displayService.moveCategoryGoods(categoryGoods);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 카테고리별상품전시 상품삭제
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 15
	 */
	@PostMapping("/category/goods/delete")
	@ResponseBody
	public GagaResponse deleteCategoryGoods(@RequestBody CategoryGoods categoryGoods){
		categoryGoods.setRegNo(TsaSession.getInfo().getUserNo());
		displayService.deleteCategoryGoods(categoryGoods);
		return super.ok(message.getMessage("SUCC_0003"));
	}

	/**
	 * 카테고리별상품전시 상품추가
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 15
	 */
	@PostMapping("/category/goods/save")
	@ResponseBody
	public GagaResponse insertCategoryGoods(@RequestBody CategoryGoods categoryGoods){
		categoryGoods.setRegNo(TsaSession.getInfo().getUserNo());
		displayService.insertCategoryGoods(categoryGoods);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 전시 상품 순서변경 팝업
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 18
	 */
	@GetMapping("/goods/dispord/change/popup/form")
	public ModelAndView goodsDispOrdChangePopupForm(CategoryGoods categoryGoods) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("params", categoryGoods);
		mav.setViewName("display/GoodsDispOrdChangePopupForm");
		return mav;
	}

	/**
	 * 전시 상품 순서변경 리스트
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 18
	 */
	@PostMapping("/goods/dispord/change/popup/list")
	@ResponseBody
	public GagaMap getGoodsDispOrdChangePopupList(@RequestBody CategoryGoods categoryGoods) {
		GagaMap result = new GagaMap();

		log.info("getGoodsDispOrdChangePopupList categoryGoods::{}",categoryGoods);
		categoryGoods.setRegNo(TsaSession.getInfo().getUserNo());	//엑셀조회시 로그인 사용자의 엑셀 상품조회시 사용
		categoryGoods.setPageable(new TscPageRequest(categoryGoods.getPageNo() - 1, categoryGoods.getPageSize()));
		categoryGoods.getPageable().setTotalCount(displayService.getCategoryGoodsListCount(categoryGoods));

		result.set("pageing", categoryGoods);
		result.set("categoryGoodsList", displayService.getCategoryGoodsList(categoryGoods));

		return result;
	}

	/**
	 * 전시 상품 순서변경 상품삭제
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 19
	 */
	@PostMapping("/goods/dispord/change/delete")
	@ResponseBody
	public GagaResponse deleteDispOrdChangeGoods(@RequestBody CategoryGoods categoryGoods){
		categoryGoods.setRegNo(TsaSession.getInfo().getUserNo());
		displayService.deleteDispOrdChangeGoods(categoryGoods);
		return super.ok(message.getMessage("SUCC_0003"));
	}

	/**
	 * 전시 상품 순서변경 전시초기화
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 19
	 */
	@PostMapping("/category/goods/dispord/init")
	@ResponseBody
	public GagaResponse updateCategoryGoodsDispOrdInit(@RequestBody CategoryGoods categoryGoods){
		categoryGoods.setRegNo(TsaSession.getInfo().getUserNo());
		displayService.updateCategoryGoodsDispOrdInit(categoryGoods);
		return super.ok(message.getMessage("SUCC_0009"));
	}

	/**
	 * 전시 상품 순서변경 최상단/하단 이동
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 19
	 */
	@PostMapping("/category/goods/dispord/updown")
	@ResponseBody
	public GagaResponse updateCategoryGoodsDispOrdUpdown(@RequestBody CategoryGoods categoryGoods){
		int size = categoryGoods.getGoodsCdArr().length-1;

		for(int i=size; i>=0; i--){
			categoryGoods.setGoodsCd(categoryGoods.getGoodsCdArr()[i]);
			categoryGoods.setDispOrd(categoryGoods.getDispOrdArr()[i]);
			displayService.updateCategoryGoodsDispOrdUpdown(categoryGoods);
		}
		return super.ok(message.getMessage("SUCC_0009"));
	}

	/**
	 * 전시 상품 순서변경 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 19
	 */
	@PostMapping("/category/goods/dispord/update")
	@ResponseBody
	public GagaResponse updateCategoryGoodsDispOrd(@RequestBody CategoryGoods categoryGoods){
		displayService.updateCategoryGoodsDispOrd(categoryGoods);
		return super.ok(message.getMessage("SUCC_0009"));
	}

	/**
	 * 메인전시관리 화면
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 22
	 */
	@GetMapping("/main/form")
	public ModelAndView mainListForm() {
		ModelAndView mav = new ModelAndView();
		Category category = new Category();
		Brand brand = new Brand();
		category.setClsLvl(1);
		mav.addObject("mainCategoryList", displayService.getMainCategoryList(category));
		brand.setUseYn("Y");
		brand.setSelfYn("Y");
		mav.addObject("brandCdList", businessService.getBrandList(brand));
		mav.addObject("contentsLocList", rendererService.getAvailCommonCodeList("G028"));

		mav.setViewName("display/MainListForm");
		return mav;
	}

	/**
	 * 메인전시 카테고리 목록
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 22
	 */
	@PostMapping("/main/category/list")
	@ResponseBody
	public Collection<Category> getMainCategoryList(@RequestBody Category category) {
		return displayService.getMainCategoryList(category);
	}

	/**
	 * 메인전시 레이아웃 목록
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 22
	 */
	@PostMapping("/main/layout/list")
	@ResponseBody
	public Collection<MainDisplay> getMainLayoutList(@RequestBody MainDisplay mainDisplay) {
		return displayService.getMainLayoutList(mainDisplay);
	}

	/**
	 * 메인전시 레이아웃 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 22
	 */
	@PostMapping("/main/layout/save")
	@ResponseBody
	public GagaResponse saveMainLayout(@RequestBody Collection<MainDisplay> mainDisplayList) {
		displayService.saveMainLayout(mainDisplayList);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 메인전시 컨텐츠 수정 팝업
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 22
	 */
	@GetMapping("/main/contents/popup/form")
	public ModelAndView mainContentsPopupForm(@RequestParam(value = "cateNo") String cateNo, @RequestParam(value = "contentsLoc") String contentsLoc) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("cateNo", cateNo);
		mav.addObject("contentsLoc", contentsLoc);
		mav.addObject("contentsDesc", displayService.getContentsDesc(contentsLoc));
		mav.addObject("contentsLocList", rendererService.getAvailCommonCodeList("G028"));
		mav.addObject("contentsTitle", displayService.getContentsTitle(cateNo,contentsLoc));
		mav.setViewName("display/MainContentsPopupForm");
		return mav;
	}

	/**
	 * 메인전시 컨텐츠 미리보기 리스트
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 23
	 */
	@PostMapping("/contents/preview/list")
	@ResponseBody
	public Collection<Contents> getContentsPreviewList(@RequestBody Contents contents) {
		return displayService.getContentsPreviewList(contents);
	}

	/**
	 * 메인전시 컨텐츠 미리보기 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 23
	 */
	@PostMapping("/contents/preview/save")
	@ResponseBody
	public GagaResponse saveContentsPreview(@RequestBody Collection<Contents> contentsList) {
		log.info("saveContentsPreview:::{}",contentsList);
		displayService.saveContentsPreview(contentsList);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 메인전시 컨텐츠 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 23
	 */
	@PostMapping("/contents/save")
	@ResponseBody
	public GagaResponse saveContents(@RequestBody Contents contents) {
		log.info("controller saveContents contents::: {}",contents);
		displayService.saveContents(contents);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * CATEGORY4SRCH 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 24
	 */
	@PostMapping("/category4srch/list")
	@ResponseBody
	public Category getCategory4srch(@RequestBody Category category) {
		return displayService.getCategory4srch(category);
	}

	/**
	 * 메인전시 컨텐츠 멀티 수정 팝업
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 24
	 */
	@GetMapping("/main/multicontents/popup/form")
	public ModelAndView mainMultiContentsPopupForm(@RequestParam(value = "cateNo") String cateNo, @RequestParam(value = "contentsLoc") String contentsLoc) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("cateNo", cateNo);
		mav.addObject("contentsLoc", contentsLoc);
		mav.addObject("contentsDesc", displayService.getContentsDesc(contentsLoc));
		mav.addObject("contentsLocList", rendererService.getAvailCommonCodeList("G028"));
		mav.addObject("contentsTitle", displayService.getContentsTitle(cateNo,contentsLoc));
		mav.setViewName("display/MainMultiContentsPopupForm");
		return mav;
	}

}
