package com.dbnt.kcscbackend.admin.contents.popUp.controller;


import com.dbnt.kcscbackend.admin.committee.schedules.model.CreateScheduleVO;
import com.dbnt.kcscbackend.admin.committee.schedules.model.UpdateScheduleVO;
import com.dbnt.kcscbackend.admin.committee.schedules.service.EgovIndvdlSchdulManageService;
import com.dbnt.kcscbackend.admin.contents.popUp.model.CreatePopupVO;
import com.dbnt.kcscbackend.admin.contents.popUp.model.FileVO;
import com.dbnt.kcscbackend.admin.contents.popUp.model.UpdatePopupVO;
import com.dbnt.kcscbackend.admin.contents.popUp.service.PopUpApiService;
import com.dbnt.kcscbackend.admin.contents.popUp.utils.EgovFileMngUtil;
import com.dbnt.kcscbackend.auth.entity.LoginVO;
import com.dbnt.kcscbackend.config.common.ResponseCode;
import com.dbnt.kcscbackend.config.common.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Api("PopUpApiController")
@RestController
@Tag(name="PopUpApiController",description = "팝업 관리")
public class PopUpApiController {

	@Resource(name = "popUpApiService")
	private PopUpApiService popUpApiService;

	@Resource(name = "EgovFileMngUtil")
	private EgovFileMngUtil fileUtil;

	@Operation(
			summary = "'팝업 관리' 페이지에서 목록 불러오는 API",
			description = "관리자 단에서 '컨텐츠 관리' > '팝업 관리' 페이지에서 목록 불러오는 API",
			tags = {"PopUpApiController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "조회 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
	})
	@GetMapping(value = "/contents/api/popup-manage/list")
	public ResultVO contentsApiPopUpManageList(
			@AuthenticationPrincipal LoginVO user,
			HttpServletRequest request,
			Pageable pageable) throws Exception {

		ResultVO resultVO = new ResultVO();

		try {
			resultVO = popUpApiService.contentsApiPopUpManageList(resultVO, request, user, pageable);
		} catch (Exception e) {
			resultVO.setResultCode(ResponseCode.FAILED.getCode());
			resultVO.setResultMessage(e.getMessage());
		}


		System.out.println(
			"\n--------------------------------------------------------------\n" +
			request.getRequestURI() + " OUT:" +
			"\n--------------------------------------------------------------\n" +
			"resultVO.toString():" + "\n" +
			resultVO.toString() + "\n" +
			"\n--------------------------------------------------------------\n"
		);

		return resultVO;
	}



	@Operation(
			summary = "팝업 추가 API",
			description = "관리자 단에서 '컨텐츠 관리' > '팝업 관리' 페이지에서 팝업을 추가하는 API",
			tags = {"PopUpApiController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "등록 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님"),
			@ApiResponse(responseCode = "900", description = "입력값 무결성 오류")
	})
	@PostMapping(value = "/contents/api/popup-manage")
	public ResultVO contentsApiPopUpManageCreate(
			HttpServletRequest request,
			@AuthenticationPrincipal LoginVO loginVO,
			final MultipartHttpServletRequest multiRequest,
			CreatePopupVO createPopupVO
	) throws Exception {

		ResultVO resultVO = new ResultVO();

		try {
			resultVO = popUpApiService.contentsApiPopUpManageCreate(resultVO, request, loginVO, multiRequest, createPopupVO);
		} catch (Exception e) {
			resultVO.setResultCode(ResponseCode.FAILED.getCode());
			resultVO.setResultMessage(e.getMessage());
		}


		System.out.println(
				"\n--------------------------------------------------------------\n" +
						request.getRequestURI() + " OUT:" +
						"\n--------------------------------------------------------------\n" +
						"resultVO.toString():" + "\n" +
						resultVO.toString() + "\n" +
						"\n--------------------------------------------------------------\n"
		);

		return resultVO;

	}


	@Operation(
			summary = "팝업 수정 API",
			description = "관리자 단에서 '컨텐츠 관리' > '팝업 관리' 페이지에서 팝업을 수정하는 API",
			tags = {"PopUpApiController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "등록 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님"),
	})
	@PutMapping(value = "/contents/api/popup-manage/{popupId}")
	public ResultVO contentsApiPopUpManageUpdate(
			HttpServletRequest request,
			@AuthenticationPrincipal LoginVO loginVO,
			UpdatePopupVO updatePopupVO,
			@PathVariable("popupId") Long popupId
	) throws Exception {

		ResultVO resultVO = new ResultVO();

		try {
			resultVO = popUpApiService.contentsApiPopUpManageUpdate(resultVO, request, loginVO, updatePopupVO, popupId);
		} catch (Exception e) {
			resultVO.setResultCode(ResponseCode.FAILED.getCode());
			resultVO.setResultMessage(e.getMessage());
		}


		System.out.println(
			"\n--------------------------------------------------------------\n" +
			request.getRequestURI() + " OUT:" +
			"\n--------------------------------------------------------------\n" +
			"resultVO.toString():" + "\n" +
			resultVO.toString() + "\n" +
			"\n--------------------------------------------------------------\n"
		);

		return resultVO;

	}


	@Operation(
			summary = "팝업 비활성화 API",
			description = "관리자 단에서 '컨텐츠 관리' > '팝업 관리' 페이지에서 팝업을 비활성화 API. 삭제가 아닌 비활성화 임. 삭제는 없음.",
			tags = {"PopUpApiController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "등록 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님"),
	})
	@DeleteMapping(value = "/contents/api/popup-manage/{popupId}")
	public ResultVO contentsApiPopUpManageDelete
			(
					@AuthenticationPrincipal LoginVO user,
					HttpServletRequest request,
					@PathVariable("popupId") String strPopupId
			) throws Exception {

		ResultVO resultVO = new ResultVO();
		Long popupId = Long.valueOf(strPopupId);
		try {
			resultVO = popUpApiService.contentsApiPopUpManageDelete(resultVO, request, user, popupId);
		} catch (Exception e) {
			resultVO.setResultCode(ResponseCode.FAILED.getCode());
			resultVO.setResultMessage(e.getMessage());
		}

		System.out.println(
				"\n--------------------------------------------------------------\n" +
						request.getRequestURI() + " OUT:" +
						"\n--------------------------------------------------------------\n" +
						"resultVO.toString():" + "\n" +
						resultVO.toString() + "\n" +
						"\n--------------------------------------------------------------\n"
		);

		return resultVO;
	}


	@Operation(
			summary = "팝업 활성화 또는 비활성화 설정하는 API",
			description = "관리자 단에서 '컨텐츠 관리' > '팝업 관리' 페이지에서 '사용여부'를 활성화 또는 비활성화 설정해주는 API.",
			tags = {"PopUpApiController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "등록 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님"),
	})
	@PutMapping(value = "/contents/api/popup-manage/activation-switch/{popupId}")
	public ResultVO contentsApiPopUpManageUpdateActivationSwitch(
			HttpServletRequest request,
			@AuthenticationPrincipal LoginVO loginVO,
			@ApiParam(value="활성화 여부") @RequestParam(required=true) String checked,
			@PathVariable("popupId") Long popupId
	) throws Exception {

		ResultVO resultVO = new ResultVO();

		try {
			resultVO = popUpApiService.contentsApiPopUpManageUpdateActivationSwitch(resultVO, request, loginVO, checked, popupId);
		} catch (Exception e) {
			resultVO.setResultCode(ResponseCode.FAILED.getCode());
			resultVO.setResultMessage(e.getMessage());
		}


		System.out.println(
				"\n--------------------------------------------------------------\n" +
						request.getRequestURI() + " OUT:" +
						"\n--------------------------------------------------------------\n" +
						"resultVO.toString():" + "\n" +
						resultVO.toString() + "\n" +
						"\n--------------------------------------------------------------\n"
		);

		return resultVO;

	}

	@Operation(
			summary = "팝업 내용 불러오기 API",
			description = "관리자 단에서 '컨텐츠 관리' > '팝업 관리' 페이지에서 저장된 팝업을 불러오는 API",
			tags = {"PopUpApiController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "조회 성공")
	})
	@GetMapping(value = "/contents/api/popup-manage/{popupId}")
	public ResultVO contentsApiPopUpManageRead(
			HttpServletRequest request,
			@AuthenticationPrincipal LoginVO loginVO,
			@PathVariable("popupId") Long popupId
	) throws Exception {

		ResultVO resultVO = new ResultVO();

		try {
			resultVO = popUpApiService.contentsApiPopUpManageRead(resultVO, request, loginVO, popupId);
		} catch (Exception e) {
			resultVO.setResultCode(ResponseCode.FAILED.getCode());
			resultVO.setResultMessage(e.getMessage());
		}


		System.out.println(
				"\n--------------------------------------------------------------\n" +
						request.getRequestURI() + " OUT:" +
						"\n--------------------------------------------------------------\n" +
						"resultVO.toString():" + "\n" +
						resultVO.toString() + "\n" +
						"\n--------------------------------------------------------------\n"
		);

		return resultVO;
	}




}