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.message.MessageCode;
import com.takensoft.common.util.ResponseData;
import com.takensoft.common.util.ResponseUtil;
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
 * @modification
 *     since    |    author    | description
 *  2024.05.13  |    박정하     | 최초 등록
 *
 * 팝업 관련 Controller
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/admin/popup")
public class PopupController {
    private final PopupService popupService;
    private final FileService fileService;
    private final ResponseUtil resUtil;

    /**
     * @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");

        // 응답 처리
        if(insertResult > 0) {
            return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
        } else {
            return resUtil.errorRes(MessageCode.COMMON_INSERT_FAIL);
        }
    }

    /**
     * @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);

        // 응답 처리
        return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
    }

    /**
     * @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);

        // 응답 처리
        return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
    }

    /**
     * @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);

        // 응답 처리
        if(result > 0) {
            return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
        } else {
            return resUtil.errorRes(MessageCode.COMMON_UPDATE_FAIL);
        }
    }

    /**
     * @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);

        // 응답 처리
        if(result > 0) {
            return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
        } else {
            return resUtil.errorRes(MessageCode.COMMON_DELETE_FAIL);
        }
    }

    /**
     * @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());

        // 응답 처리
        return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
    }
}