package egovframework.com.its.web.server.controller;

import egovframework.com.its.web.server.service.NoticeService;
import egovframework.com.its.web.server.util.PageCriteria;
import egovframework.com.its.web.server.vo.NoticeVO;
import egovframework.com.its.web.server.vo.ResultVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Controller
public class NoticeController {

    private final NoticeService noticeService;
    private PageCriteria criteria;

    @PostConstruct
    void init() {
        this.criteria = new PageCriteria();
    }

    //MAIN 페이지의 공지사항
    @RequestMapping(value = {"/notice/noticeList.do", "/notice/noticeList/{page}.do"}, method = RequestMethod.GET)
    public String getNoticeList(Model model, @PathVariable(required = false) String page) {
        HashMap <String, String> paramMap = new HashMap<>();
        paramMap.put("boardId", "1");
        if (page != null) {
            paramMap.put("page", page);
        }
        Map<String, Object> resultMap = noticeService.selectBoardList(paramMap, this.criteria);
        model.addAttribute("siteSubInfo", "sub notice");
        model.addAttribute("subTitle", "공지사항");
        model.addAttribute("noticeList", resultMap.get("noticeList"));
        model.addAttribute("pageMaker", resultMap.get("pageMaker"));
        return "notice/noticeList.tiles";
    }

    @RequestMapping(value = "/notice/noticeView/{boardNo}.do", method = RequestMethod.GET)
    public String getListView(Model model, @PathVariable HashMap<String, String> paramMap) {
        model.addAttribute("siteSubInfo", "sub notice");
        model.addAttribute("subTitle", "공지사항 상세페이지");
        paramMap.put("boardId", "1");
        this.noticeService.updateNoticeReadCount(paramMap);
        NoticeVO result = this.noticeService.selectBoardView(paramMap);
        model.addAttribute("notice", result);
        return "notice/noticeView.tiles";
    }

    @RequestMapping(value = "/notice/getFile.do", method = RequestMethod.POST, produces = "application/json; charset=UTF8")
    @ResponseBody
    public ResultVO getFile(HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> paramMap, @Value("${attach_path}") String attachFilePath) {
        return this.noticeService.attatchedFileDownload(paramMap, attachFilePath);
    }

    @RequestMapping(value = "/notice/getNoticeList.do", method = RequestMethod.POST)
    @ResponseBody
    public  Map<String, Object> getNoticeList(String page, String searchText, String searchType) {
        HashMap <String, String> paramMap = new HashMap<>();
        paramMap.put("boardId", "1");
        paramMap.put("page", page);
        paramMap.put("searchType", searchType);
        paramMap.put("searchText", searchText);
        Map<String, Object> resultMap = noticeService.selectBoardList(paramMap, this.criteria);
        return resultMap;
    }
}