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.RequestParam;
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.TsaClauseService;
import com.style24.admin.biz.service.TsaEnvsetService;
import com.style24.admin.biz.service.TsaRendererService;
import com.style24.admin.support.controller.TsaBaseController;
import com.style24.core.biz.service.TscEnvsetService;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.persistence.domain.AnswerPhase;
import com.style24.persistence.domain.Clause;
import com.style24.persistence.domain.CustGradePolicy;
import com.style24.persistence.domain.Envset;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 환경설정 Controller
 *
 * @author gagamel
 * @since 2020. 10. 21
 */
@Controller
@RequestMapping("/envset")
@Slf4j
public class TsaEnvsetController extends TsaBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TscEnvsetService cenvsetService;

	@Autowired
	private TsaEnvsetService envsetService;

	@Autowired
	private TsaRendererService rendererService;

	@Autowired
	private TsaClauseService clauseService;

	@Autowired
	private TsaAnswerPhaseService ansPhaseService;

	/**
	 * 기본환경설정 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 21
	 */
	@GetMapping("/basic/form")
	public ModelAndView basicEnvsetForm() {
		ModelAndView mav = new ModelAndView();

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

		mav.setViewName("envset/BasicEnvsetForm");

		return mav;
	}

	/**
	 * 사이트별 환경설정유형에 따른 환경설정 정보
	 * @param siteCd - 사이트코드
	 * @param envsetType - 환경설정유형
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 21
	 */
	@GetMapping("/{siteCd}/{envsetType}")
	@ResponseBody
	public Envset getEnvset(@PathVariable String siteCd, @PathVariable String envsetType) {
		return cenvsetService.getEnvset(siteCd, envsetType);
	}

	/**
	 * 환경설정 정보 저장
	 * @param envset - 환경설정 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 21
	 */
	@PostMapping("/create")
	@ResponseBody
	public GagaResponse createEnvset(@RequestBody Envset envset) {
		if (envset.getEnvsetType().equals("B10")) {
			// 메타 정보 수정 시는 캐싱 삭제되도록
			// 중요하지 않은 정보로서 캐싱 삭제해도 됨.
			envsetService.createEnvsetMetaInfo(envset);
		} else {
			// 그 외는 캐싱 삭제 안 함
			// 정책은 중요한 정보로서 캐싱 문제 발생 시 심각해질 수 있으므로 캐싱처리 안 함(2020.05.14. gagamel)
			envsetService.createEnvset(envset);
		}

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

	/**
	 * 사이트별 환경설정유형에 따른 환경설정이력 화면
	 * @param siteCd - 사이트코드
	 * @param envsetType - 환경설정유형
	 * @param envsetTypeNm - 환경설정유형명
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 21
	 */
	@GetMapping("/history/form")
	public ModelAndView envsetHistoryForm(@RequestParam(value = "siteCd", required = true) String siteCd, @RequestParam(value = "envsetType", required = true) String envsetType, @RequestParam(value = "envsetTypeNm", required = true) String envsetTypeNm) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("siteCd", siteCd);
		mav.addObject("envsetType", envsetType);
		mav.addObject("envsetTypeNm", envsetTypeNm);

		mav.setViewName("envset/EnvsetHistoryForm");

		return mav;
	}

	/**
	 * 사이트별 환경설정유형에 따른 환경설정이력 목록
	 * @param siteCd - 사이트코드
	 * @param envsetType - 환경설정유형
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 21
	 */
	@GetMapping("/history/{siteCd}/{envsetType}")
	@ResponseBody
	public Collection<Envset> getEnvsetHistoryList(@PathVariable String siteCd, @PathVariable String envsetType) {
		return envsetService.getEnvsetHistoryList(siteCd, envsetType);
	}

	/**
	 * 회원혜택설정 화면
	 * @return
	 * @author gagamel
	 * @since 2021. 2. 25
	 */
	@GetMapping("/benefit/form")
	public ModelAndView benefitEnvsetForm() {
		ModelAndView mav = new ModelAndView();

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

		mav.setViewName("envset/BenefitEnvsetForm");

		return mav;
	}

	/**
	 * 회원등급정책관리 화면
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 6
	 */
	@GetMapping("/customer/grade/policy/form")
	public ModelAndView customerGradePolicyForm() {
		ModelAndView mav = new ModelAndView();

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

		// 회원등급
		mav.addObject("custGradeList", rendererService.getAvailCommonCodeList("G110"));

		mav.setViewName("envset/CustomerGradePolicyForm");

		return mav;
	}

	/**
	 * 회원등급정책 목록
	 * @param policy - 회원등급정책 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 7
	 */
	@PostMapping("/customer/grade/policy/list")
	@ResponseBody
	public Collection<CustGradePolicy> getCustomerGradePolicyList(@RequestBody CustGradePolicy policy) {
		return envsetService.getCustomerGradePolicyList(policy);
	}

	/**
	 * 회원등급정책 저장
	 * @param policy - 회원등급정책 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 7
	 */
	@PostMapping("/customer/grade/policy/create")
	@ResponseBody
	public GagaResponse createCustomerGradePolicy(@RequestBody CustGradePolicy policy) {
		envsetService.createCustomerGradePolicy(policy);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 약관관리 화면
	 * @return ModelAndView
	 * @author gagamel
	 * @since 2020. 10. 29
	 */
	@GetMapping("/clause/form")
	public ModelAndView clauseForm() {
		ModelAndView mav = new ModelAndView();

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

		// 약관유형 목록
		mav.addObject("clauseTypeList", rendererService.getAvailCommonCodeList("G057"));

		mav.setViewName("envset/ClauseForm");

		return mav;
	}

	/**
	 * 약관관리 목록
	 * @param clause - 약관 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 29
	 */
	@PostMapping("/clause/list")
	@ResponseBody
	public Collection<Clause> getClauseList(@RequestBody Clause clause) {
		return clauseService.getClauseList(clause);
	}

	/**
	 * 약관관리상세 화면
	 * @param mode - 모드(N:신규, U:상세)
	 * @param clauseSq - 약관일련번호
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 29
	 */
	@GetMapping("/clause/detail/form")
	public ModelAndView clauseDetailForm(@RequestParam(value = "mode") String mode, @RequestParam(value = "clauseSq", required = false) Integer clauseSq) {
		ModelAndView mav = new ModelAndView();

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

		// 약관유형 목록
		mav.addObject("clauseTypeList", rendererService.getAvailCommonCodeList("G057"));

		// 모드 값
		mav.addObject("mode", mode);

		if ("U".equals(mode)) {
			mav.addObject("clauseInfo", clauseService.getClauseDetail(clauseSq));
		}

		mav.setViewName("envset/ClauseDetailForm");

		return mav;
	}

	/**
	 * 약관 생성/수정
	 * @param clause - 약관 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 29
	 */
	@PostMapping("/clause/save")
	@ResponseBody
	public GagaResponse saveClause(@RequestBody Clause clause) {
		clauseService.saveClause(clause);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 답변문구관리
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 29
	 */
	@GetMapping("/answer/phase/form")
	public ModelAndView answerPhaseForm() {
		ModelAndView mav = new ModelAndView();

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

		// 답변종류콤보
		mav.addObject("ansClsfList", rendererService.getAvailCommonCodeList("G061"));

		mav.setViewName("envset/AnswerPhaseForm");

		return mav;
	}

	/**
	 * 답변문구 목록
	 * @param ansPhase - 답변문구 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 29
	 */
	@PostMapping("/answer/phase/list")
	@ResponseBody
	public Collection<AnswerPhase> getAnswerPhaseList(@RequestBody AnswerPhase ansPhase) {
		return ansPhaseService.getAnswerPhaseList(ansPhase);
	}

	/**
	 * 답변문구 저장
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 29
	 */
	@PostMapping("/answer/phase/save")
	@ResponseBody
	public GagaResponse saveAnswerPhase(@RequestBody AnswerPhase ansPhase) {
		ansPhaseService.saveAnswerPhase(ansPhase);
		return super.ok(message.getMessage("SUCC_0001"));
	}

}
