package egovframework.controller;

import egovframework.service.impl.NoticeServiceImpl;
import egovframework.util.PageCriteria;
import egovframework.vo.NoticeVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
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.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("/notice")
public class NoticeController {

    private final NoticeServiceImpl noticeService;
    private PageCriteria criteria;

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

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

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

    @RequestMapping(value = "/getFile.do", method = RequestMethod.GET, produces = "text/plain; charset=UTF8")
    public void getFile(HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> paramMap) {
        this.noticeService.attatchedFileDownload(request, response, paramMap);
    }

    @RequestMapping(value="/getNoticeList.do", method = RequestMethod.POST)
    @ResponseBody
    public List<NoticeVO> getNoticeListData() {
        return this.noticeService.getNoticeListData();
    }
}