package com.dbnt.sims.publicBoard;

import com.dbnt.sims.codeMgt.service.CodeMgtService;
import com.dbnt.sims.config.Role;
import com.dbnt.sims.publicBoard.model.PublicBoard;
import com.dbnt.sims.publicBoard.model.PublicComment;
import com.dbnt.sims.publicBoard.service.PublicBoardService;
import com.dbnt.sims.userInfo.model.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequiredArgsConstructor
@RequestMapping("/publicBoard")
public class PublicBoardController {
  private final PublicBoardService publicBoardService;
  private final CodeMgtService codeMgtService;

  @GetMapping("/noticePage")
  public ModelAndView organMgtPage(@AuthenticationPrincipal UserInfo loginUser, PublicBoard publicBoard) {
    ModelAndView mav;
    if(publicBoard.getDashboardFlag()){
      mav = new ModelAndView("publicBoard/notice/noticePageDashBoard");
      publicBoard.setRowCnt(5);
    }else{
      mav = new ModelAndView("publicBoard/notice/noticePage");
    }
    publicBoard.setQueryInfo();
    publicBoard.setPublicType("PLB001");
    if (loginUser.getUserRole().contains(Role.SUB_ADMIN.getValue())) {
      publicBoard.setDownOrganCdList(loginUser.getDownOrganCdList());
      mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
    }
    publicBoard.setUpOrganCdList(loginUser.getUpOrganCdList());
    mav.addObject("noticeList", publicBoardService.selectContentList(publicBoard));
    publicBoard.setContentCnt(publicBoardService.selectContentListCnt(publicBoard));
    publicBoard.setPaginationInfo();
    mav.addObject("searchParams", publicBoard);
    return mav;
  }

  @GetMapping("/boardPage")
  public ModelAndView boardPage(@AuthenticationPrincipal UserInfo loginUser, PublicBoard publicBoard) {
    ModelAndView mav = new ModelAndView("publicBoard/board/boardPage");
    publicBoard.setQueryInfo();
    publicBoard.setPublicType("PLB002");
    if (loginUser.getUserRole().contains(Role.SUB_ADMIN.getValue())) {
      publicBoard.setDownOrganCdList(loginUser.getDownOrganCdList());
      mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
    }
    publicBoard.setUpOrganCdList(loginUser.getUpOrganCdList());
    mav.addObject("boardList", publicBoardService.selectContentList(publicBoard));
    publicBoard.setContentCnt(publicBoardService.selectContentListCnt(publicBoard));
    publicBoard.setPaginationInfo();
    mav.addObject("searchParams", publicBoard);
    return mav;
  }


  @GetMapping("/referencePage")
  public ModelAndView referencePage(@AuthenticationPrincipal UserInfo loginUser, PublicBoard publicBoard) {
    ModelAndView mav = new ModelAndView("publicBoard/reference/referencePage");
    publicBoard.setQueryInfo();
    publicBoard.setPublicType("PLB003");
    if (loginUser.getUserRole().contains(Role.SUB_ADMIN.getValue())) {
      publicBoard.setDownOrganCdList(loginUser.getDownOrganCdList());
      mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
    }
    publicBoard.setUpOrganCdList(loginUser.getUpOrganCdList());
    mav.addObject("referenceList", publicBoardService.selectContentList(publicBoard));
    publicBoard.setContentCnt(publicBoardService.selectContentListCnt(publicBoard));
    publicBoard.setPaginationInfo();
    mav.addObject("tabStatusList", codeMgtService.selectCodeMgtList("RPC"));
    mav.addObject("searchParams", publicBoard);
    return mav;
  }


  @GetMapping("/qnaPage")
  public ModelAndView qnaPage(@AuthenticationPrincipal UserInfo loginUser, PublicBoard publicBoard) {
    ModelAndView mav = new ModelAndView("publicBoard/qna/qnaPage");
    publicBoard.setQueryInfo();
    publicBoard.setPublicType("PLB004");
    if (loginUser.getUserRole().contains(Role.SUB_ADMIN.getValue())) {
      publicBoard.setDownOrganCdList(loginUser.getDownOrganCdList());
      mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
    }
    mav.addObject("qnaList", publicBoardService.selectContentList(publicBoard));
    publicBoard.setContentCnt(publicBoardService.selectContentListCnt(publicBoard));
    publicBoard.setPaginationInfo();
    mav.addObject("searchParams", publicBoard);
    return mav;
  }


  @GetMapping("/editModal")
  public ModelAndView editModal(@AuthenticationPrincipal UserInfo loginUser, PublicBoard publicBoard) {
    ModelAndView mav = null;
    switch (publicBoard.getPublicType()) {
      case "PLB001": // 공지사항
        mav = new ModelAndView("publicBoard/notice/noticeEditModal");
        if (publicBoard.getPublicKey() != null) {
          publicBoard = publicBoardService.selectPublicBoard(publicBoard.getPublicKey());
        }
        break;
      case "PLB002": // 공용게시판
        mav = new ModelAndView("publicBoard/board/boardEditModal");
        if (publicBoard.getPublicKey() != null) {
          publicBoard = publicBoardService.selectPublicBoard(publicBoard.getPublicKey());
        }
        break;
      case "PLB003": // 자료실
        mav = new ModelAndView("publicBoard/reference/referenceEditModal");
        if (publicBoard.getPublicKey() != null) {
          publicBoard = publicBoardService.selectPublicBoard(publicBoard.getPublicKey());
        }
        break;
      case "PLB004": // Q&A
        mav = new ModelAndView("publicBoard/qna/qnaEditModal");
        if (publicBoard.getPublicKey() != null) {
          publicBoard = publicBoardService.selectPublicBoard(publicBoard.getPublicKey());
          break;
        }
    }

        if (publicBoard.getPublicKey() == null) {
          publicBoard.setWrtOrgan(loginUser.getOgCd());
          publicBoard.setWrtPart(loginUser.getOfcCd());
          publicBoard.setWrtUserSeq(loginUser.getUserSeq());
          publicBoard.setWrtUserNm(loginUser.getUserNm());
          publicBoard.setWrtDt(LocalDateTime.now());
        }
        mav.addObject("info", publicBoard);
        return mav;
    }


    @GetMapping("/viewModal")
    public ModelAndView viewModal (@AuthenticationPrincipal UserInfo loginUser, PublicBoard publicBoard){
      ModelAndView mav = null;
      switch (publicBoard.getPublicType()) {
        case "PLB001": // 공지사항
          mav = new ModelAndView("publicBoard/notice/noticeViewModal");
          break;
        case "PLB002": // 공용게시판
          mav = new ModelAndView("publicBoard/board/boardViewModal");
          break;
        case "PLB003": // 자료실
          mav = new ModelAndView("publicBoard/reference/referenceViewModal");
          break;
        case "PLB004": // Q&A
          mav = new ModelAndView("publicBoard/qna/qnaViewModal");
          break;
      }
      publicBoard = publicBoardService.selectPublicBoard(publicBoard.getPublicKey());
      mav.addObject("userSeq", loginUser.getUserSeq());
      mav.addObject("info", publicBoard);
      return mav;
    }

    @PostMapping("/saveContent")
    public Integer saveContent (PublicBoard publicBoard,
        MultipartHttpServletRequest request,
        @RequestParam(value = "fileSeq", required = false) List < Integer > deleteFileSeq){
      publicBoard.setMultipartFileList(request.getMultiFileMap().get("uploadFiles"));
      return publicBoardService.saveContent(publicBoard, deleteFileSeq);
    }
    @PostMapping("/saveComment")
    public ModelAndView saveComment (@AuthenticationPrincipal UserInfo loginUser, PublicComment comment){
      comment.setWrtOrgan(loginUser.getOgCd());
      comment.setWrtPart(loginUser.getOfcCd());
      comment.setWrtUserSeq(loginUser.getUserSeq());
      comment.setWrtUserNm(loginUser.getUserNm());
      comment.setWrtDt(LocalDateTime.now());
      publicBoardService.saveComment(comment);
      ModelAndView mav = new ModelAndView("publicBoard/commentDiv");
      mav.addObject("comment", comment);
      mav.addObject("userSeq", loginUser.getUserSeq());
      return mav;
    }

    @PostMapping("/deleteComment")
    @ResponseBody
    public void deleteComment (@RequestBody PublicComment comment){
      publicBoardService.deleteComment(comment.getPublicKey(), comment.getCommentKey());
    }
  }
