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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
 * @author 박정하
 * @since 2024.05.27
 * @modification
 *     since    |    author    | description
 *  2024.05.27  |    박정하     | 최초 등록
 *
 * 팝업 관련 Controller
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/sys/popup")
public class SysPopupController {
    private final PopupService popupService;
    private final FileService fileService;
    private final ResponseUtil resUtil;

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

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