package com.style24.front.biz.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.Device;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.style24.core.support.env.TscConstants;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.front.biz.service.TsfCounselService;
import com.style24.front.biz.service.TsfFaqService;
import com.style24.front.biz.service.TsfNoticeService;
import com.style24.front.biz.service.TsfRendererService;
import com.style24.front.support.controller.TsfBaseController;
import com.style24.front.support.security.session.TsfSession;
import com.style24.persistence.TscPageRequest;
import com.style24.persistence.domain.Counsel;
import com.style24.persistence.domain.Faq;
import com.style24.persistence.domain.Notice;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 고객센터 Controller
 * 
 * @author gagamel
 * @since 2020. 12. 23
 */
@Controller
@RequestMapping("/callcenter")
@Slf4j
public class TsfCallcenterController extends TsfBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TsfFaqService faqService;

	@Autowired
	private TsfNoticeService noticeService;

	@Autowired
	private TsfCounselService counselService;

	@Autowired
	private TsfRendererService rendererService;

	/**
	 * FAQ
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 23
	 */
	@GetMapping("/faq/form")
	public ModelAndView faqForm() {
		ModelAndView mav = new ModelAndView();

		// FAQ TOP 10 목록
		mav.addObject("faqTop10List", faqService.getFaqLikesList(TscConstants.Site.STYLE24.value()));

		// FAQ유형
		mav.addObject("faqTypeList", rendererService.getAvailCommonCodeList("G046"));

		mav.setViewName(super.getDeviceViewName("callcenter/FaqForm"));

		return mav;
	}

	/**
	 * FAQ 목록
	 * @param faq - FAQ 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 23
	 */
	@PostMapping("/faq/list")
	@ResponseBody
	public GagaMap getFaqList(@RequestBody Faq faq) {
		faq.setSiteCd(TscConstants.Site.STYLE24.value());

		GagaMap result = new GagaMap();

		TscPageRequest pageable = new TscPageRequest((faq.getPageNo() > 0 ? faq.getPageNo() - 1 : 0), faq.getPageSize());
		pageable.setTotalCount(faqService.getFaqTotalCount(faq));
		faq.setPageable(pageable);
		log.info("pageable: {}", pageable);

		result.set("paging", faq);
		result.set("dataList", faqService.getFaqList(faq));

		return result;
	}

	/**
	 * FAQ 조회수 갱신
	 * @param faqSq - FAQ일련번호
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 23
	 */
	@PostMapping("/faq/readcount/update/{faqSq}")
	@ResponseBody
	public void updateFaqReadCount(@PathVariable Integer faqSq) {
		faqService.updateFaqReadCount(faqSq);
	}

	/**
	 * 공지사항
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 23
	 */
	@GetMapping("/notice/form")
	public ModelAndView noticeForm() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName(super.getDeviceViewName("callcenter/NoticeForm"));
		return mav;
	}

	/**
	 * 공지사항 목록
	 * @param notice - 공지사항 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 23
	 */
	@PostMapping("/notice/list")
	@ResponseBody
	public GagaMap getNoticeList(@RequestBody Notice notice) {
		notice.setSiteCd(TscConstants.Site.STYLE24.value());

		GagaMap result = new GagaMap();

		TscPageRequest pageable = new TscPageRequest((notice.getPageNo() > 0 ? notice.getPageNo() - 1 : 0), notice.getPageSize(), notice.getPageUnit());
		pageable.setTotalCount(noticeService.getNoticeTotalCount(notice));
		notice.setPageable(pageable);
		log.debug("pageable: {}", pageable);

		result.set("paging", notice);
		result.set("dataList", noticeService.getNoticeList(notice));

//		// 긴급(중요) 공지사항 목록
//		result.set("urgentList", noticeService.getUrgentNoticeList(notice));

		return result;
	}

	/**
	 * 공지사항 조회수 갱신
	 * @param noticeSq - 공지사항일련번호
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 23
	 */
	@PostMapping("/notice/readcount/update/{noticeSq}")
	@ResponseBody
	public void updateNoticeReadCount(@PathVariable Integer noticeSq) {
		noticeService.updateNoticeReadCount(noticeSq);
	}

	/**
	 * 1:1문의 화면
	 * @param device - 디바이스 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 24
	 */
	@GetMapping("/onetoone/qna/form")
	public ModelAndView oneToOneQnaForm(Device device) {
		ModelAndView mav = new ModelAndView();

		// 1:1문의 건수 정보
		mav.addObject("qnaCountInfo", counselService.getOneToOneQnaCountInfo());

		if (device.isMobile() || device.isTablet()) { // 모바일이면
			// 문의유형
			mav.addObject("counselClsfList", rendererService.getCommonCodeList("G059", "Y"));
		}

		mav.setViewName(super.getDeviceViewName("callcenter/OneToOneQnaForm"));

		return mav;
	}

	/**
	 * 1:1문의 등록 화면
	 * @return
	 * @author gagamel
	 * @since 2021. 2. 19
	 */
	@GetMapping("/onetoone/qna/register/form")
	public ModelAndView oneToOneQnaRegisterForm() {
		ModelAndView mav = new ModelAndView();

		// 문의유형
		mav.addObject("counselClsfList", rendererService.getCommonCodeList("G059", "Y", new String[] {"G596"}));

		mav.setViewName(super.getDeviceViewName("callcenter/OneToOneQnaRegisterForm"));

		return mav;
	}

	/**
	 * 1:1문의 등록 처리
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@PostMapping("/onetoone/qna/create")
	@ResponseBody
	public GagaResponse createOneToOneQna(@RequestBody Counsel counsel) {
		counselService.createOneToOneQna(counsel);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 1:1문의 목록
	 * @param counsel - 상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@PostMapping("/onetoone/qna/list")
	@ResponseBody
	public GagaMap getOneToOneQnaList(@RequestBody Counsel counsel) {
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setSiteCd(TscConstants.Site.STYLE24.value());

		GagaMap result = new GagaMap();

		TscPageRequest pageable = new TscPageRequest((counsel.getPageNo() > 0 ? counsel.getPageNo() - 1 : 0), counsel.getPageSize(), counsel.getPageUnit());
		pageable.setTotalCount(counselService.getOneToOneQnaTotalCount(counsel));
		counsel.setPageable(pageable);
		log.debug("pageable: {}", pageable);

		result.set("paging", counsel);
		result.set("dataList", counselService.getOneToOneQnaList(counsel));

		return result;
	}

	/**
	 * 1:1문의 삭제 처리
	 * 		진행중 상태일 때 삭제하는 것이 아니라 답변완료 상태일 때만 삭제하는 것으로 함 (기획서)
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@PostMapping("/onetoone/qna/delete")
	@ResponseBody
	public GagaResponse deleteOneToOneQna(@RequestBody Counsel counsel) {
		counselService.deleteOneToOneQna(counsel);
		return super.ok(message.getMessage("SUCC_0003"));
	}

	/**
	 * 1:1문의 건수 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 4. 27
	 */
	@GetMapping("/onetoone/qna/count/info")
	@ResponseBody
	public Counsel getOneToOneQnaCountInfo() {
		return counselService.getOneToOneQnaCountInfo();
	}

	/**
	 * 1:1문의 상세 화면. 목록 화면에 상세 데이터 노출로 해당 로직 사용 안 함.
	 * @param counselSq - 상담일련번호
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@GetMapping("/onetoone/qna/detail/form/{counselSq}")
	public ModelAndView oneToOneQnaDetailForm(@PathVariable Integer counselSq) {
		ModelAndView mav = new ModelAndView();

		// 1:1문의 상세
		mav.addObject("qnaDetailInfo", counselService.getOneToOneQna(counselSq));

		mav.setViewName(super.getDeviceViewName("callcenter/OneToOneQnaDetailForm"));

		return mav;
	}

	/**
	 * 1:1문의 수정 처리
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@PostMapping("/onetoone/qna/update")
	@ResponseBody
	public GagaResponse updateOneToOneQna(@RequestBody Counsel counsel) {
		counselService.updateOneToOneQna(counsel);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 1:1문의 이미지 삭제 처리
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@PostMapping("/onetoone/qna/image/delete")
	@ResponseBody
	public GagaResponse deleteOneToOneQnaImage(@RequestBody Counsel counsel) {
		counselService.deleteOneToOneQnaImage(counsel);
		return super.ok(message.getMessage("SUCC_0003"));
	}

	/**
	 * 1:1 문의 이미지상세 -  댓글 상세 레이어
	 * @return
	 * @author sowon
	 * @since 2021. 6. 14
	 */
	@PostMapping("/onetoone/photo/detail/layer")
	public ModelAndView onetoonePhotoDetailForm(Counsel counsel) {
		ModelAndView mav = new ModelAndView();

		// 상품 기본정보
		mav.addObject("counsel", counsel);
		mav.addObject("imgList", counselService.getOneToOneImgList(counsel));
		mav.setViewName(super.getDeviceViewName("callcenter/OneToOnePhotoForm"));
		return mav;
	}

	/**
	 * 1:1문의용 답변템플릿 조회
	 * @param counselClsf - 문의유형
	 * @return
	 * @author gagamel
	 * @since 2021. 9. 28
	 */
	@GetMapping("/onetoone/qna/answer/template/{counselClsf}")
	@ResponseBody
	public String getOnetooneQnaAnswerTemplate(@PathVariable String counselClsf) {
		return counselService.getOnetooneQnaAnswerTemplate(counselClsf);
	}

	/**
	 * 상품문의 등록 처리
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@PostMapping("/goods/qna/create")
	@ResponseBody
	public GagaResponse createGoodsQna(@RequestBody Counsel counsel) {
		log.info("createGoodsQna counsel = {}", counsel);
		counselService.createGoodsQna(counsel);
		return super.ok("상품 문의가 등록되었습니다. 빠른 시일 내에 답변드리겠습니다.");
	}

	/**
	 * 상품문의 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@GetMapping("/goods/qna/form")
	public ModelAndView goodsQnaForm() {
		ModelAndView mav = new ModelAndView();

		// 상품문의 건수 정보
		mav.addObject("qnaCountInfo", counselService.getGoodsQnaCountInfo());

		mav.setViewName(super.getDeviceViewName("callcenter/GoodsQnaForm"));

		return mav;
	}

	/**
	 * 상품문의 목록
	 * @param counsel - 상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@PostMapping("/goods/qna/list")
	@ResponseBody
	public GagaMap getGoodsQnaList(@RequestBody Counsel counsel) {
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setSiteCd(TscConstants.Site.STYLE24.value());

		GagaMap result = new GagaMap();

		TscPageRequest pageable = new TscPageRequest((counsel.getPageNo() > 0 ? counsel.getPageNo() - 1 : 0), counsel.getPageSize(), counsel.getPageUnit());
		pageable.setTotalCount(counselService.getGoodsQnaTotalCount(counsel));
		counsel.setPageable(pageable);
		log.debug("pageable: {}", pageable);

		result.set("paging", counsel);
		result.set("dataList", counselService.getGoodsQnaList(counsel));

		return result;
	}

	/**
	 * 상품문의 삭제 처리
	 * 		진행중 상태일 때 삭제하는 것이 아니라 답변완료 상태일 때만 삭제하는 것으로 함 (기획서)
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@PostMapping("/goods/qna/delete")
	@ResponseBody
	public GagaResponse deleteGoodsQna(@RequestBody Counsel counsel) {
		counselService.deleteGoodsQna(counsel);
		return super.ok(message.getMessage("SUCC_0003"));
	}

	/**
	 * 상품문의 건수 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 4. 27
	 */
	@GetMapping("/goods/qna/count/info")
	@ResponseBody
	public Counsel getGoodsQnaCountInfo() {
		return counselService.getGoodsQnaCountInfo();
	}

}
