package com.its.web.controller.notice;

import com.its.web.dto.notice.AttachFileDto;
import com.its.web.service.notice.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/notice")
@Api(tags = "04.공지사항")
public class NoticeController {

    private final NoticeService service;

    @ApiOperation(value = "01.첨부파일 다운로드")
    @PostMapping(value = "/attach", produces = {"application/json; charset=utf8"})
    @ResponseBody
    public AttachFileDto getAttachFile(@Param("boardNo") String boardNo,
                                       @Param("fileId") String fileId) {
        return this.service.getAttachFile(boardNo, fileId);
    }

    @ApiOperation(value = "02.공지사항 글작성")
    @PostMapping(value = "/writeNotice")
    @ResponseBody
    public Map<String, String> writeNotice(@RequestParam Map<String, Object> paramMap,
                                           @Param("attachFile") MultipartFile attachFile,
                                           HttpSession session) {
        paramMap.put("webUserId", session.getAttribute("UUID"));
        return this.service.writeNotice(paramMap, attachFile);
    }

    @ApiOperation(value = "03.공지사항 글수정")
    @PostMapping(value = "/modifyNotice")
    @ResponseBody
    public Map<String, String> modifyNotice(@RequestParam Map<String, Object> paramMap,
                                           @Param("attachFile") MultipartFile attachFile) {
        return this.service.modifyNotice(paramMap, attachFile);
    }

    @ApiOperation(value = "04.공지사항 삭제")
    @DeleteMapping(value = "/deleteNotice", produces = {"application/json; charset=utf8"})
    @ResponseBody
    public Map<String, String> deleteNotice(@Param("boarNo") String boardNo) {
        return this.service.deleteBoard(boardNo);
    }

    @ApiOperation(value = "05.공지사항 횟수 증가")
    @PostMapping(value = "/update-read-count/{boarNo}", produces = {"application/json; charset=utf8"})
    @ResponseBody
    public int updateNoticeReadCount(@PathVariable("boarNo") String boardNo) {
        return this.service.updateNoticeReadCount(boardNo);
    }

    @ApiOperation(value = "게시판 이미지 업로드")
    @PostMapping(value = "/image-upload")
    @ResponseBody
    public String noticeImageUpload(HttpServletRequest req) {
        return this.service.imageUpload(req);
    }

    @ApiOperation(value= "업로드 이미지 소스 가져오기")
    @GetMapping(value="/upload/{imageName}")
    @ResponseBody
    public byte[] getImage(@PathVariable("imageName") String imageName) {
        return this.service.getImage(imageName);
    }

}
