package incheon.ags.por.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import incheon.ags.ias.popupNtc.service.PopupNtcService;
import incheon.ags.ias.popupNtc.vo.PopupNtcSearchVO;
import incheon.ags.ias.popupNtc.vo.PopupNtcVO;
import incheon.ags.por.service.PorNoticeService;
import incheon.ags.por.vo.PorNoticeVO;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.com.cmm.exception.BusinessException;
import lombok.RequiredArgsConstructor;

/**
 * @ClassName : PorPopupNoticeController.java
 * @Description : 공간정보포털 컨트롤러
 *
 * @author : 관리자
 * @since : 2025. 07. 17
 * @version : 1.0
 *
 *          <pre>
 * << 개정이력(Modification Information) >>
 *
 *   수정일              수정자               수정내용
 *  -------------  ------------   ---------------------
 *   2023. 10. 10    관리자               최초 생성
 *   2024. 12. 19    관리자               패널 기반 플랫폼으로 전면 개편
 *   2025. 07. 17    관리자               공간정보포털로 전환
 *          </pre>
 *
 */
@Controller
@RequestMapping("/ags/por")
@RequiredArgsConstructor
public class PorPopupNoticeController {

	private final PorNoticeService porNoticeService;
	private final PopupNtcService popupNtcService;

	/**
	 * 공지사항 화면
	 * 
	 * @param model
	 * @return JSP 페이지
	 * @exception Exception
	 */
	@GetMapping("/popupNotice/list.do")
	public String popupNoticeList(@RequestParam(defaultValue = "1") int page, @ModelAttribute PopupNtcSearchVO searchVO,
			ModelMap model) throws Exception {

		searchVO.setSysCd("SGP");

		int totalCount = popupNtcService.selectPopupNtcListTotCnt(searchVO);

		// 페이징정보
		searchVO.setPageIndex(page);
		PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(page);
		paginationInfo.setRecordCountPerPage(searchVO.getRecordCountPerPage());
		paginationInfo.setPageSize(searchVO.getPageSize());
		paginationInfo.setTotalRecordCount(totalCount);

		searchVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
		searchVO.setLastIndex(paginationInfo.getLastRecordIndex());
		searchVO.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());

		List<Map<String, Object>> popupNtcList = popupNtcService.selectPopupNtcList(searchVO);

		model.addAttribute("popupNtcList", popupNtcList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("paginationInfo", paginationInfo);
		model.addAttribute("searchVO", searchVO);

		return "ags/por/popupNotice/list";
	}

	/**
	 * 공지사항 수정 화면
	 * 
	 * @param model
	 * @return JSP 페이지
	 * @exception Exception
	 */
	@GetMapping("/popupNotice/view.do")
	public String popupNoticeView(@RequestParam Long popupNtcSn, ModelMap model) throws Exception {

		PopupNtcVO popupNtcVO = popupNtcService.selectPopupNtcDetail(popupNtcSn);
		model.addAttribute("popupNtcVO", popupNtcVO);

		return "ags/por/popupNotice/view";
	}

	/**
	 * 공지사항 작성 화면
	 * 
	 * @param model
	 * @return JSP 페이지
	 * @exception Exception
	 */
	@GetMapping("/popupNotice/insertForm.do")
	public String popupNoticeInsertForm(ModelMap model) throws Exception {
		return "ags/por/popupNotice/insert";
	}

	/**
	 * 팝업공지 작성
	 * 
	 * @param model
	 * @return 결과
	 * @exception Exception
	 */
	@PostMapping(path = "/popupNotice/insert.do", consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> insert(@ModelAttribute PorNoticeVO porNoticeVO,
			ModelMap model) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (porNoticeVO.getPopupNtcPstgYn() == null) {
			porNoticeVO.setPopupNtcPstgYn(true);
		}

		int result = porNoticeService.insertPopupNotice(porNoticeVO);

		resultMap.put("result", result);

		return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
	}

	/**
	 * 공지사항 수정 화면
	 * 
	 * @param model
	 * @return JSP 페이지
	 * @exception Exception
	 */
	@GetMapping("/popupNotice/updateForm.do")
	public String popupNoticeUpdateForm(@RequestParam Long popupNtcSn, ModelMap model) throws Exception {

		PopupNtcVO popupNtcVO = popupNtcService.selectPopupNtcDetail(popupNtcSn);

		model.addAttribute("popupNtcVO", popupNtcVO);

		return "ags/por/popupNotice/update";

	}

	/**
	 * 팝업공지 수정
	 * 
	 * @param model
	 * @return 결과
	 * @exception Exception
	 */
	@PutMapping(path = "/popupNotice/update.do")
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> update(
			@RequestBody PopupNtcVO popupNtcVO
			, ModelMap model
		) throws Exception {
		popupNtcVO.setSysCd("SGP");

		PopupNtcVO existingPopupNtc = popupNtcService.selectPopupNtcDetail(popupNtcVO.getPopupNtcSn());
		if (existingPopupNtc == null) {
			throw new BusinessException("수정할 팝업공지를 찾을 수 없습니다.");
		}

		int result = popupNtcService.updatePopupNtc(popupNtcVO);

		if (result <= 0) {
			throw new BusinessException("팝업공지 수정에 실패했습니다.");
		} else {
			return ResponseEntity.ok(DefaultApiResponse.success("팝업공지가 성공적으로 수정되었습니다."));
		}
	}

	/**
	 * 팝업공지 삭제
	 * 
	 * @param model
	 * @return 결과
	 * @exception Exception
	 */
	@PostMapping(path = "/popupNotice/insert.do")
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> insert(@RequestBody PopupNtcVO popupNtcVO,
			ModelMap model) throws Exception {

		// 대민 팝업공지 코드 삽입
		popupNtcVO.setSysCd("SGP");

		int result = popupNtcService.insertPopupNtc(popupNtcVO);

		if (result <= 0) {
			throw new BusinessException("팝업공지 등록에 실패했습니다.");
		} else {
			return ResponseEntity.ok(DefaultApiResponse.success("팝업공지가 성공적으로 등록되었습니다."));
		}
	}

	/**
	 * 팝업공지 삭제
	 * 
	 * @param model
	 * @return 결과
	 * @exception Exception
	 */
	@DeleteMapping(path = "/popupNotice/delete.do")
	public ResponseEntity<DefaultApiResponse<Map<String, Object>>> delete(
			@RequestParam("popupNtcSn") Long popupNtcSn
			, ModelMap model) throws Exception {

		if (popupNtcSn == null) {
			throw new BusinessException("삭제할 팝업공지 일련번호가 없습니다.");
		} else {
			int result = popupNtcService.deletePopupNtc(popupNtcSn);
			if (result > 0) {
				return ResponseEntity.ok(DefaultApiResponse.success("팝업공지가 성공적으로 삭제되었습니다."));
			} else {
				throw new BusinessException("팝업공지 삭제에 실패했습니다.");
			}
		}
	}

}