package com.style24.admin.biz.web;

import java.io.IOException;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.TsaFaqService;
import com.style24.admin.biz.service.TsaNoticeService;
import com.style24.admin.biz.service.TsaRendererService;
import com.style24.admin.support.controller.TsaBaseController;
import com.style24.admin.support.env.TsaConstants;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.persistence.domain.Faq;
import com.style24.persistence.domain.Notice;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 게시판 Controller
 *
 * @author gagamel
 * @since 2020. 10. 30
 */
@Controller
@RequestMapping("/board")
@Slf4j
public class TsaBoardController extends TsaBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TsaNoticeService noticeService;

	@Autowired
	private TsaRendererService rendererService;

	@Value("${upload.default.target.path}")
	private String uploadTargetPath;

	@Autowired
	private TsaFaqService faqService;

	/**
	 * 공지사항 화면
	 * @param noticeType - 공지유형(10:사이트공지, 20:내부공지)
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 30
	 */
	@GetMapping("/notice/form/{noticeType}")
	public ModelAndView noticeForm(@PathVariable String noticeType) {
		ModelAndView mav = new ModelAndView();

		// 공지유형
		mav.addObject("noticeType", noticeType);

		// 수신자 목록
		// 공지유형이 "10:사이트공지"일 때는 사이트 목록
		// 공지유형이 "20:내부공지"일 때는 공지수신자-조직 목록
		if (noticeType.equals(TsaConstants.NoticeType.SITE.value())) {
			mav.addObject("noticeReceiverList", rendererService.getAvailCommonCodeList("G000"));
		} else if (noticeType.equals(TsaConstants.NoticeType.INNER.value())) {
			mav.addObject("noticeReceiverList", rendererService.getAvailCommonCodeList("G048"));
		}

		mav.setViewName("board/NoticeForm");

		return mav;
	}

	/**
	 * 사이트 공지 목록
	 * @param notice - 공지사항 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 30
	 */
	@PostMapping("/notice/list")
	@ResponseBody
	public Collection<Notice> getNoticeList(@RequestBody Notice notice) {
		return noticeService.getNoticeList(notice);
	}

	/**
	 * 공지사항 수신자 목록
	 * @param noticeSq - 공지사항일련번호
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 30
	 */
	@GetMapping("/notice/receiver/list/{noticeSq}")
	@ResponseBody
	public Collection<Notice> getNoticeReceiverList(@PathVariable Integer noticeSq) {
		return noticeService.getNoticeReceiverList(noticeSq);
	}

	/**
	 * 공지사항 파일목록 조회
	 * @param noticeSq - 공지사항일련번호
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 30
	 */
	@GetMapping("/notice/file/list/{noticeSq}")
	@ResponseBody
	public Collection<Notice> getNoticeFileList(@PathVariable Integer noticeSq) {
		return noticeService.getNoticeFileList(noticeSq);
	}

	/**
	 * 공지사항 저장
	 * @param notice - 공지사항 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 30
	 */
	@PostMapping("/notice/save")
	@ResponseBody
	public GagaResponse saveNotice(@RequestBody Notice notice) {
		noticeService.saveNotice(notice);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 첨부파일 삭제
	 * @param notice - 공지사항 정보
	 * @return
	 * @throws IOException
	 * @author gagamel
	 * @since 2020. 3. 24
	 */
	@PostMapping("/notice/file/delete")
	@ResponseBody
	public GagaResponse deleteNoticeFile(@RequestBody Notice notice) throws IOException {
		// 파일 삭제
		String fileName = GagaFileUtil.getConcatenationPath(uploadTargetPath, "board", notice.getSysFileNm());

		GagaFileUtil.deleteFile(fileName);

		// 삭제된 파일 DB 처리
		noticeService.deleteNoticeFile(notice);

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

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

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

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

		mav.setViewName("board/FaqForm");

		return mav;
	}

	/**
	 * FAQ 목록
	 * @param faq - FAQ 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 3
	 */
	@PostMapping("/faq/list")
	@ResponseBody
	public Collection<Faq> getFaqList(@RequestBody Faq faq) {
		return faqService.getFaqList(faq);
	}

	/**
	 * FAQ 등록/수정
	 * @param faq - FAQ 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 3
	 */
	@PostMapping("/faq/save")
	@ResponseBody
	public GagaResponse saveFaq(@RequestBody Faq faq) {
		faq.setRegNo(TsaSession.getInfo().getUserNo());
		faq.setUpdNo(TsaSession.getInfo().getUserNo());
		faqService.saveFaq(faq);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * FAQ 상세
	 * @param mode - 모드(N:신규, U:상세)
	 * @param faqSq - FAQ일련번호
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 3
	 */
	@GetMapping("/faq/detail/form")
	public ModelAndView faqDetailForm(@RequestParam(value = "mode") String mode, @RequestParam(value = "faqSq", required = false) Integer faqSq) {
		ModelAndView mav = new ModelAndView();

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

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

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

		if ("U".equals(mode)) {
			mav.addObject("faqInfo", faqService.getFaq(faqSq));
		}

		mav.setViewName("board/FaqDetailForm");

		return mav;
	}

}
