package com.style24.scm.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.core.biz.service.TscAnswerPhaseService;
import com.style24.core.biz.service.TscKakaotalkService;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.persistence.domain.AnswerPhase;
import com.style24.persistence.domain.Counsel;
import com.style24.scm.biz.service.TssCounselService;
import com.style24.scm.biz.service.TssRendererService;
import com.style24.scm.support.controller.TssBaseController;
import com.style24.scm.support.security.session.TssSession;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 고객 Controller
 *
 * @author gagamel
 * @since 2020. 12. 28
 */
@Controller
@RequestMapping("/customer")
@Slf4j
public class TssCustomerController extends TssBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TssCounselService counselService;

	@Autowired
	private TssRendererService rendererService;

	@Autowired
	private TscAnswerPhaseService ansPhaseService;
	
	@Autowired
	private TscKakaotalkService kakaotalkService;

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

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

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

		mav.setViewName("customer/GoodsQnaForm");

		return mav;
	}

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

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

		// 상품문의 상세
		mav.addObject("counselInfo", counselService.getGoodsQna(counselSq));

		// 문의용 답변문구
		mav.addObject("ansPhaseList", rendererService.getQnaAnswerPhaseList("G061_20"));

		mav.setViewName("customer/GoodsQnaDetailForm");

		return mav;
	}

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

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

		counselService.updateGoodsQnaAnswer(counsel);

		// 상품문의상세 조회
		Counsel tCounsel = counselService.getGoodsQna(counsel.getCounselSq());

		// TODO. 고객이 SMS답변수신을 요청했을 때
		if (counsel.getSmsReqYn().equals("Y")) {
			try {
				if (counsel.getCounselType().equals("G")) {
					//답변완료안내 카카오알림톡 발송
					kakaotalkService.sendGoodsAskNotify(tCounsel,tCounsel.getCustNo());
				}
				// 		
			} catch (Exception e) {
				// Do nothing
			}
		}

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

}
