package com.style24.front.biz.web;

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.support.env.TscConstants;
import com.style24.front.biz.service.TsfFaqService;
import com.style24.front.biz.service.TsfNoticeService;
import com.style24.front.support.controller.TsfBaseController;
import com.style24.persistence.TsfPageRequest;
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 TsfFaqService faqService;

	@Autowired
	private TsfNoticeService noticeService;

	/**
	 * 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()));

		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();

		TsfPageRequest pageable = new TsfPageRequest((faq.getPageNo() > 0 ? faq.getPageNo() - 1 : 0), faq.getPageSize(), faq.getPageUnit());
		pageable.setTotalCount(faqService.getFaqTotalCount(faq));
		faq.setPageable(pageable);
		log.debug("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 GagaResponse updateFaqReadCount(@PathVariable Integer faqSq) {
		faqService.updateFaqReadCount(faqSq);
		return super.ok();
	}

	/**
	 * 공지사항
	 * @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();

		TsfPageRequest pageable = new TsfPageRequest((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 GagaResponse updateNoticeReadCount(@PathVariable Integer noticeSq) {
		noticeService.updateNoticeReadCount(noticeSq);
		return super.ok();
	}

}
