package com.dbnt.kcscbackend.standardCode;

import com.dbnt.kcscbackend.config.common.BaseController;
import com.dbnt.kcscbackend.auth.entity.LoginVO;
import com.dbnt.kcscbackend.config.common.ResponseCode;
import com.dbnt.kcscbackend.config.common.ResultVO;
import com.dbnt.kcscbackend.standardCode.entity.TnDocumentCodeList;
import com.dbnt.kcscbackend.standardCode.entity.TnDocumentContent;
import com.dbnt.kcscbackend.standardCode.entity.TnDocumentFavorites;
import com.dbnt.kcscbackend.standardCode.entity.TnDocumentInfo;
import com.dbnt.kcscbackend.standardCode.service.StandardCodeService;
import com.dbnt.kcscbackend.standardCode.service.StandardCodeVO;
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 lombok.RequiredArgsConstructor;
import org.egovframe.rte.fdl.property.EgovPropertyService;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

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

/**
 * 건설기준코드 관리를 위한 컨트롤러 클래스
 *
 * <pre>
 * << 개정이력(Modification Information) >>
 *
 *   수정일      수정자          수정내용
 *  -------    --------    ---------------------------
 *  2023.10.11  최강석          최초 생성
 *
 *  </pre>
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/standardCode")
@Tag(name="StandardCodeController",description = "건설기준코드 관리 컨트롤러")
public class StandardCodeController extends BaseController {

	@Resource(name = "propertiesService")
	protected EgovPropertyService propertyService;
	private final StandardCodeService standardCodeService;



	@Operation(
			summary = "건설기준코드 트리 조회",
			description = "건설기준코드 트리 조회",
			tags = {"StandardCodeController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "조회 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
	})
	@RequestMapping(method = RequestMethod.GET, value = "/code-tree")
	public ResultVO getCodeTree() throws Exception {
		ResultVO resultVO = new ResultVO();
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("codeTree", standardCodeService.selectStandardCodeTree());
		resultVO.setResult(resultMap);
		return resultVO;
	}


	@Operation(
			summary = "건설기준코드 내용 조회",
			description = "건설기준코드 내용 조회",
			tags = {"StandardCodeController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "조회 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
	})
	@PostMapping(value = "/getCodeDetailInfo.do", consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResultVO getCodeDetailInfo(@RequestBody StandardCodeVO param, @AuthenticationPrincipal LoginVO user)
			throws Exception {
		ResultVO resultVO = new ResultVO();
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("document", standardCodeService.selectStandardCodeDocument(param));
		resultVO.setResult(resultMap);
		return resultVO;
	}

	@Operation(
			summary = "건설기준코드 이력 조회",
			description = "건설기준코드 이력 조회",
			tags = {"StandardCodeController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "조회 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
	})
	@GetMapping(value = "/code-info", consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResultVO getCodeInfo(StandardCodeVO param, @AuthenticationPrincipal LoginVO user)
			throws Exception {
		ResultVO resultVO = new ResultVO();
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("docInfo", standardCodeService.selectStandardCodeDocInfo(param));
		resultVO.setResult(resultMap);
		return resultVO;
	}

	@Operation(
			summary = "건설기준코드 에러 문구 수정",
			description = "건설기준코드 에러 문구 수정",
			tags = {"StandardCodeController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "저장 성공"),
			@ApiResponse(responseCode = "300", description = "저장 실패"),
			@ApiResponse(responseCode = "303", description = "만료된 토큰"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
	})
	@RequestMapping(method = RequestMethod.POST, value = "/auth/saveErrorCd.do", consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResultVO saveErrorCd(@RequestBody TnDocumentContent content, @AuthenticationPrincipal LoginVO user, HttpServletRequest req) throws Exception {
		ResultVO resultVO = new ResultVO();
		Map<String, Object> resultMap = new HashMap<>();
		if(user == null){
			resultVO.setResultCode(303);
		}else{
			if(!user.getUserSe().equals("ACC_TP01")){
				resultVO.setResultCode(403);
			}else{
				standardCodeService.saveErrorCd(content);
				resultVO.setResultCode(200);
			}
		}
		return resultVO;
	}

	@Operation(
			summary = "건설기준코드 리스트 조회",
			description = "건설기준코드 리스트 조회",
			tags = {"StandardCodeController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "조회 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
	})
	@GetMapping(value = "/standard-code-list")
	public ResultVO selectStandardCodeList(TnDocumentInfo tnDocumentInfo, @AuthenticationPrincipal LoginVO user)
			throws Exception {
		ResultVO resultVO = new ResultVO();
		Map<String, Object> resultMap = new HashMap<>();

		tnDocumentInfo.makeListCode();
		tnDocumentInfo.setUserSeq(user.getUserSeq());
		resultMap.put("resultList", standardCodeService.selectStandardCodeList(tnDocumentInfo));
		resultMap.put("resultCnt", standardCodeService.selectStandardCodeListCnt(tnDocumentInfo));

		resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
		resultVO.setResultMessage(ResponseCode.SUCCESS.getMessage());
		resultVO.setResult(resultMap);
		return resultVO;
	}

	@Operation(
			summary = "건설기준코드 다운로드 리스트 조회",
			description = "건설기준코드 다운로드 리스트 조회",
			tags = {"StandardCodeController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "조회 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
	})
	@GetMapping(value = "/standard-code-download-list")
	public ResultVO selectStandardCodeDownloadList(TnDocumentInfo tnDocumentInfo, @AuthenticationPrincipal LoginVO user)
			throws Exception {
		ResultVO resultVO = new ResultVO();
		Map<String, Object> resultMap = new HashMap<>();
		String listCode = tnDocumentInfo.getListCode();
		if(listCode.equals("60")){
			listCode += "______";
		}else{
			listCode += "____";
		}
		resultMap.put("resultList", standardCodeService.selectTnDocumentGroupToGroupFullCdLike(listCode));

		resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
		resultVO.setResultMessage(ResponseCode.SUCCESS.getMessage());
		resultVO.setResult(resultMap);
		return resultVO;
	}

	@Operation(
			summary = "건설기준코드 검색조건 옵션 조회",
			description = "건설기준코드 검색조건 옵션 조회",
			tags = {"StandardCodeController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "조회 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
	})
	@GetMapping(value = "/category-option")
	public ResultVO getCategoryOption(TnDocumentInfo tnDocumentInfo) throws Exception {
		ResultVO resultVO = new ResultVO();
		Map<String, Object> resultMap = new HashMap<>();

		resultMap.put("groupList", standardCodeService.selectTnDocumentGroupToGroupFullCdLike(tnDocumentInfo.getListCode()+"__"));

		resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
		resultVO.setResultMessage(ResponseCode.SUCCESS.getMessage());
		resultVO.setResult(resultMap);
		return resultVO;
	}

	@Operation(
			summary = "건설기준코드 즐겨찾기 추가, 삭제",
			description = "건설기준코드 즐겨찾기 추가, 삭제",
			tags = {"StandardCodeController"}
	)
	@ApiResponses(value = {
			@ApiResponse(responseCode = "200", description = "수정 성공"),
			@ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
	})
	@PostMapping(value = "/document-favorite")
	public ResultVO setDocumentFavorite(@RequestBody TnDocumentFavorites favorites, @AuthenticationPrincipal LoginVO loginUser) throws Exception {
		ResultVO resultVO = new ResultVO();
		if(favorites.getActive()==null){
			resultVO.setResultCode(ResponseCode.SAVE_ERROR.getCode());
			resultVO.setResultMessage(ResponseCode.SAVE_ERROR.getMessage());
			return resultVO;
		}

		if(favorites.getActive()){
			standardCodeService.saveFavorites(loginUser.getUserSeq(), favorites.getGroupSeq());
		}else{
			standardCodeService.deleteFavorites(loginUser.getUserSeq(), favorites.getGroupSeq());
		}
		resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
		resultVO.setResultMessage(ResponseCode.SUCCESS.getMessage());
		return resultVO;
	}

	@Operation(
			summary = "건설기준코드 개정이력 조회",
			description = "건설기준코드 개정이력 조회",
			tags = {"StandardCodeController"}
	)
	@PostMapping("/codeListModal.do")
	public ResponseEntity<List<TnDocumentInfo>> testCodeList(@RequestBody Integer groupseq){
		return ResponseEntity.ok(standardCodeService.selectStandardCodeRevisionhistoryList(groupseq));
	}



}