package com.dbnt.kcscbackend.admin.standardResearch;


import com.dbnt.kcscbackend.admin.contents.popUp.model.CreatePopupVO;
import com.dbnt.kcscbackend.admin.contents.popUp.model.UpdatePopupVO;
import com.dbnt.kcscbackend.admin.contents.popUp.utils.EgovFileMngUtil;
import com.dbnt.kcscbackend.admin.standardResearch.model.CreateStandardResearchVO;
import com.dbnt.kcscbackend.admin.standardResearch.model.UpdateStandardResearchVO;
import com.dbnt.kcscbackend.admin.standardResearch.service.AdminStandardResearchService;
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.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Api("AdminStandardResearchController")
@RestController
@Tag(name="AdminStandardResearchController",description = "건설기준연구 관리")
public class AdminStandardResearchController {

	@Resource(name = "adminStandardResearchService")
	private AdminStandardResearchService adminStandardResearchService;

	@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/standard-research/list")
	public ResultVO contentsStandardResearchList(
			@AuthenticationPrincipal LoginVO user,
			HttpServletRequest request,
			Pageable pageable) throws Exception {

		ResultVO resultVO = new ResultVO();

		try {
			resultVO = adminStandardResearchService.contentsStandardResearchList(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/standard-research")
	public ResultVO contentsStandardResearchCreate(
			HttpServletRequest request,
			@AuthenticationPrincipal LoginVO loginVO,
			final MultipartHttpServletRequest multiRequest,
			CreateStandardResearchVO createStandardResearchVO
	) throws Exception {

		ResultVO resultVO = new ResultVO();

		try {
			resultVO = adminStandardResearchService.contentsStandardResearchCreate(resultVO, request, loginVO, multiRequest, createStandardResearchVO);
		} 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/standard-research/{popupId}")
	public ResultVO contentsStandardResearchUpdate(
			HttpServletRequest request,
			@AuthenticationPrincipal LoginVO loginVO,
			UpdateStandardResearchVO updateStandardResearchVO,
			@PathVariable("popupId") Long popupId
	) throws Exception {

		ResultVO resultVO = new ResultVO();

		try {
			resultVO = adminStandardResearchService.contentsStandardResearchUpdate(resultVO, request, loginVO, updateStandardResearchVO, 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/standard-research/{popupId}")
	public ResultVO contentsStandardResearchDelete
			(
					@AuthenticationPrincipal LoginVO user,
					HttpServletRequest request,
					@PathVariable("popupId") String strPopupId
			) throws Exception {

		ResultVO resultVO = new ResultVO();
		Long popupId = Long.valueOf(strPopupId);
		try {
			resultVO = adminStandardResearchService.contentsStandardResearchDelete(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 = "조회 성공")
	})
	@GetMapping(value = "/contents/standard-research/{popupId}")
	public ResultVO contentsStandardResearchRead(
			HttpServletRequest request,
			@AuthenticationPrincipal LoginVO loginVO,
			@PathVariable("popupId") Long popupId
	) throws Exception {

		ResultVO resultVO = new ResultVO();

		try {
			resultVO = adminStandardResearchService.contentsStandardResearchRead(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;
	}




}