package com.style24.admin.biz.web;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
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.admin.biz.service.TsaAnswerPhaseService;
import com.style24.admin.biz.service.TsaCounselService;
import com.style24.admin.biz.service.TsaRendererService;
import com.style24.admin.support.controller.TsaBaseController;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.persistence.domain.AnswerPhase;
import com.style24.persistence.domain.Counsel;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 고객 Controller
 *
 * @author gagamel
 * @since 2020. 12. 24
 */
@Controller
@RequestMapping("/customer")
@Slf4j
public class TsaCustomerController extends TsaBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TsaCounselService counselService;

	@Autowired
	private TsaRendererService rendererService;

	@Autowired
	private TsaAnswerPhaseService answerPhaseService;

	/**
	* 1:1문의관리 화면
	* @return
	* @author gagamel
	* @since 2020. 12. 24
	*/
	@GetMapping("/onetoone/qna/form")
	public ModelAndView oneToOneQnaForm() {
		ModelAndView mav = new ModelAndView();

		// 사이트 목록
		mav.addObject("siteList", rendererService.getAvailCommonCodeList("G000"));

		// 상담분류
		mav.addObject("counselClsfList", rendererService.getCommonCodeList("G059", "Y", new String[] {"G596"}));

		// 상담상태 목록
		mav.addObject("ansStatList", rendererService.getAvailCommonCodeList("G060"));

		mav.setViewName("customer/OneToOneQnaForm");

		return mav;
	}

	/**
	 * 1:1문의 목록
	 * @param counsel - 상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 24
	 */
	@PostMapping("/onetoone/qna/list")
	@ResponseBody
	public Collection<Counsel> getOneToOneQnaList(@RequestBody Counsel counsel) {
		return counselService.getOneToOneQnaList(counsel);
	}

	/**
	 * 1:1문의상세 화면
	 * @param counselSq -상담일련번호
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 24
	 */
	@GetMapping("/onetoone/qna/detail/form/{counselSq}")
	public ModelAndView oneToOneQnaDetailForm(@PathVariable Integer counselSq) {
		ModelAndView mav = new ModelAndView();

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

		// 1:1문의용 답변문구
		mav.addObject("ansPhaseList", rendererService.getAnswerPhaseList("20"));

		mav.setViewName("customer/OneToOneQnaDetailForm");

		return mav;
	}

	/**
	 * 1:1문의 답변문구 조회
	 * @param ansSq - 답변일련번호
	 * @param ansClsf - 답변종류
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 24
	 */
	@GetMapping("/onetoone/qna/answerphase/{ansSq}/{ansClsf}")
	@ResponseBody
	public AnswerPhase getOneToOneQnaAnswerPhase(@PathVariable Integer ansSq, @PathVariable String ansClsf) {
		AnswerPhase ansPhase = new AnswerPhase();
		ansPhase.setAnsSq(ansSq);
		ansPhase.setAnsClsf(ansClsf);
		return answerPhaseService.getOneToOneQnaAnswerPhase(ansPhase);
	}

	/**
	 * 문의 답변 저장
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 24
	 */
	@PostMapping("/qna/answer/save")
	@ResponseBody
	public GagaResponse saveQnaAnswer(@RequestBody Counsel counsel) {
		counsel.setAnsNo(TsaSession.getInfo().getUserNo());
		counsel.setUpdNo(TsaSession.getInfo().getUserNo());

		counselService.updateQnaAnswer(counsel);

		// 일대일문의상세 조회
		Counsel tCounsel = counselService.getOneToOneQna(counsel.getCounselSq());

//		try {
//			// 일대일문의 답변 메일 발송
//			mailService.sendOneToOneAnswer(tCounsel);
//		} catch (Exception e) {
//			// Do nothing
//		}
//
//		try {
//			// 일대일문의 답변 카카오알림톡 발송
//			kakaoService.sendOnetoOneAnswer(tCounsel);
//		} catch (Exception e) {
//			// Do nothing
//		}

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

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

		// 사이트 목록
		mav.addObject("siteList", rendererService.getAvailCommonCodeList("G000"));

		// 답변의뢰업체 목록
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TsaSession.getInfo().getSupplyCompCd(), "N"));

		// 상담상태 목록
		mav.addObject("ansStatList", rendererService.getAvailCommonCodeList("G060"));

		mav.setViewName("customer/GoodsQnaForm");

		return mav;
	}

	/**
	 * 상품문의 목록
	 * @param counsel - 상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 24
	 */
	@PostMapping("/goods/qna/list")
	@ResponseBody
	public Collection<Counsel> getGoodsQnaList(@RequestBody Counsel counsel) {
		return counselService.getGoodsQnaList(counsel);
	}

	/**
	 * 상품문의상세 화면
	 * @param counselSq -상담일련번호
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 24
	 */
	@GetMapping("/goods/qna/detail/form/{counselSq}")
	public ModelAndView goodsQnaDetailForm(@PathVariable Integer counselSq) {
		ModelAndView mav = new ModelAndView();

		// 답변의뢰업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TsaSession.getInfo().getSupplyCompCd(), "N"));

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

		// 상품문의용 답변문구
		mav.addObject("ansPhaseList", rendererService.getAnswerPhaseList("20"));

		mav.setViewName("customer/GoodsQnaDetailForm");

		return mav;
	}

	/**
	 * 상품문의 입점업체에 답변의뢰 (입점상품일 경우)
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 24
	 */
	@PostMapping("/goods/qna/answer/transfer/save")
	@ResponseBody
	public GagaResponse saveGoodsQnaAnswerTransfer(@RequestBody Counsel counsel) {
		counsel.setAnsTransNo(TsaSession.getInfo().getUserNo());
		counsel.setUpdNo(TsaSession.getInfo().getUserNo());

		counselService.updateGoodsQnaAnswerTransfer(counsel);

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

}
