package com.takensoft.cms.popup.web;

import com.takensoft.cms.popup.service.PopupService;
import com.takensoft.cms.popup.vo.PopupVO;
import com.takensoft.common.file.service.FileService;
import com.takensoft.common.util.ResponseData;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.Charset;
import java.util.*;

/**
 * @author  : 박정하
 * @since   : 2024.05.13
 *
 * 팝업 관련 컨트롤러
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/admin/popup")
public class PopupController {
    private final PopupService popupService;
    private final FileService fileService;

    /**
     * @author 박정하
     * @since 2024.05.13
     * @param popupVO
     * @return
     * @throws Exception
     *
     * 팝업 등록
     */
    @PostMapping("/insertProc.file")
    public ResponseEntity<?> insertProc(@RequestPart PopupVO popupVO, List<MultipartFile> multipartFileList) throws Exception {
        HashMap<String, Object> result = popupService.popupInsert(popupVO, multipartFileList);
        int insertResult = (int) result.get("insertResult");

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        if(insertResult > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 등록 처리되었습니다.");
            responseData.setData(result);
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("등록에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author 박정하
     * @since 2024.05.13
     * @param params
     * @return
     * @throws Exception
     *
     * 팝업 목록 조회
     */
    @PostMapping("/listProc.json")
    public ResponseEntity<?> listProc(@RequestBody HashMap<String, String> params) throws Exception {
        HashMap<String, Object> result = popupService.popupList(params);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author 박정하
     * @since 2024.05.13
     * @param popupVO
     * @return
     * @throws Exception
     *
     * 팝업 상세 조회
     */
    @PostMapping("/detailProc.json")
    public ResponseEntity<?> detailProc(@RequestBody PopupVO popupVO) throws Exception {
        HashMap<String, Object> result = popupService.popupDetail(popupVO);
        PopupVO popup = (PopupVO) result.get("popupVO");

        List<HashMap<String, Object>> fileList = new ArrayList<>();
        if (popup.getFileMngId() != null) {
            fileList = fileService.fileSelectList(popup.getFileMngId());
        }
        result.put("fileList", fileList);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author 박정하
     * @since 2024.05.14
     * @param popupVO
     * @return
     * @throws Exception
     *
     * 팝업 수정
     */
    @PostMapping("/updateProc.file")
    public ResponseEntity<?> updateProc(@RequestPart PopupVO popupVO, @RequestParam(required = false) List<MultipartFile> multipartFileList) throws Exception {
        int result = popupService.popupUpdate(popupVO, multipartFileList);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        if(result > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 수정 처리되었습니다.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("수정에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author 박정하
     * @since 2024.05.14
     * @param popupVO
     * @return
     * @throws Exception
     *
     * 팝업 삭제
     */
    @PostMapping("/deleteProc.json")
    public ResponseEntity<?> deleteProc(@RequestBody PopupVO popupVO) throws Exception {
        int result = popupService.popupDelete(popupVO);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        if(result > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 삭제 처리되었습니다.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("삭제에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author 박정하
     * @since 2024.05.22
     * @param popupVO
     * @return
     * @throws Exception
     *
     * 팝업 목록 조회 (팝업창 띄우는 용도)
     */
    @PostMapping("/listByPageProc.json")
    public ResponseEntity<?> listByPageProc(@RequestBody PopupVO popupVO) throws Exception {
        List<PopupVO> result = popupService.popupListByPage(popupVO.getPageType());

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }
}