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.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.POST, value = "/getCodeTree.do", consumes = MediaType.APPLICATION_JSON_VALUE)
	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 = "인가된 사용자가 아님")
	})
	@PostMapping(value = "/getCodeInfo.do", consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResultVO getCodeInfo(@RequestBody 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("ADM")){
				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 = "인가된 사용자가 아님")
	})
	@PostMapping(value = "/selectStandardCodeList.do", consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResultVO selectStandardCodeList(@RequestBody TnDocumentInfo tnDocumentInfo, @AuthenticationPrincipal LoginVO user)
			throws Exception {
		ResultVO resultVO = new ResultVO();
		Map<String, Object> resultMap = new HashMap<>();

		PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(tnDocumentInfo.getPageIndex());
		paginationInfo.setRecordCountPerPage(propertyService.getInt("Globals.pageUnit"));
		paginationInfo.setPageSize(propertyService.getInt("Globals.pageSize"));
		resultMap.put("paginationInfo", paginationInfo);

		String tab = tnDocumentInfo.getTab() != null ? tnDocumentInfo.getTab() : "";
		String category1 = tnDocumentInfo.getCategory1() != null ? tnDocumentInfo.getCategory1() : "";
		String category2 = tnDocumentInfo.getCategory2() != null ? tnDocumentInfo.getCategory2() : "";
		String category3 = tnDocumentInfo.getCategory3() != null ? tnDocumentInfo.getCategory3() : "";

		System.out.println("@@@ tab : " + tab);
		System.out.println("@@@ category1 : " + category1);
		System.out.println("@@@ category2 : " + category2);
		System.out.println("@@@ category3 : " + category3);
		System.out.println("@@@ searchWrd : " + tnDocumentInfo.getSearchWrd());

		Integer categorySeq1 = standardCodeService.selectStandardCodeGroupSeq(tab);
		Integer categorySeq2 = standardCodeService.selectStandardCodeGroupSeq(tab + category1);
		Integer categorySeq3 = standardCodeService.selectStandardCodeGroupSeq(tab + category1 + category2);
		System.out.println("@@@ categorySeq1 : " + categorySeq1);
		System.out.println("@@@ categorySeq2 : " + categorySeq2);
		System.out.println("@@@ categorySeq3 : " + categorySeq3);
		resultMap.put("category1List", standardCodeService.selectStandardCodeCategoryList(categorySeq1));
		resultMap.put("category2List", standardCodeService.selectStandardCodeCategoryList(categorySeq2));
		resultMap.put("category3List", standardCodeService.selectStandardCodeCategoryList(categorySeq3));

		tnDocumentInfo.setListCode(tab + category1 + category2 + category3);
		List<TnDocumentCodeList> tnDocumentCodeList = standardCodeService.selectStandardCodeList(tnDocumentInfo);
		resultMap.put("resultList", tnDocumentCodeList);
		Integer totCnt = tnDocumentCodeList.get(0).getContentcount();
		System.out.println("@@@ resultCnt : " + totCnt);
		resultMap.put("resultCnt", totCnt);
		resultMap.put("user", user);

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

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



}