package com.oqpo.api.web.controller.system;

import com.oqpo.api.enums.DataMessageCode;
import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.system.CodeService;
import com.oqpo.api.web.dto.request.code.CodeGridRequest;
import com.oqpo.api.web.dto.request.code.SaveCodeRequest;
import com.oqpo.api.web.dto.response.DataResponse;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.code.CodeInfoResponse;
import com.oqpo.api.web.dto.response.code.CodeSearchListResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/api/code")
public class CodeController {
    @Autowired
    private CodeService codeService;

    /**
     * 설명 : 코드 중복 체크
     *
     * @param codeCd
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "codeCd", value = "코드", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "코드 중복 체크")
    @GetMapping("/check-duplicate")
    public ResponseEntity<DataResponse> checkDuplicate(@RequestParam(value = "codeCd") String codeCd) throws Exception {
        String dupYn = codeService.selectCodeDupYn(codeCd);
        DataMessageCode message = null;
        if ("Y".equals(dupYn)) {
            message = DataMessageCode.DUPLICATE_YES;
        } else {
            message = DataMessageCode.DUPLICATE_NO;
        }
        return ResponseEntity.ok(DataResponse.toDTO(message));
    }

    /**
     * 설명 : 코드 검색
     *
     * @param sCodeNm
     * @param sCodeTpCd
     * @param sUseYn
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "sCodeNm", value = "코드/코드명", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sCodeTpCd", value = "코드구분", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sUseYn", value = "사용여부", required = false, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "코드 검색")
    @GetMapping("/code-search")
    public ResponseEntity<CodeSearchListResponse> searchCodeList(@RequestParam(value = "sCodeNm", required = false) String sCodeNm,
                                                                 @RequestParam(value = "sCodeTpCd", required = false) String sCodeTpCd,
                                                                 @RequestParam(value = "sUseYn", required = false) String sUseYn) throws Exception {
        return ResponseEntity.ok(codeService.searchCodeList(sCodeNm, sCodeTpCd, sUseYn));
    }

    /**
     * 설명 : 코드정보 저장
     *
     * @param userId
     * @param saveCodeRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "코드정보 저장")
    @PostMapping("/save-code")
    public ResponseEntity<SaveResponse> saveCode(@ApiIgnore String userId, @RequestBody @Valid SaveCodeRequest saveCodeRequest) throws Exception {
        codeService.saveCode(userId, saveCodeRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 코드 헤더 정보
     *
     * @param codeCd
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "codeCd", value = "코드", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "코드 헤더 정보")
    @GetMapping("/info-code")
    public ResponseEntity<CodeInfoResponse> infoCode(@RequestParam(value = "codeCd") String codeCd) throws Exception {
        return ResponseEntity.ok(codeService.selectCodeInfo(codeCd));
    }

    /**
     * 설명 : 코드 상세 그리드 리스트
     *
     * @param codeGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "코드 상세 그리드 리스트")
    @PostMapping("/detail-grid-list")
    public ResponseEntity<GridResponse> detailGridList(@RequestBody @Valid CodeGridRequest codeGridRequest) throws Exception {
        return ResponseEntity.ok(codeService.codeDtlGridList(codeGridRequest.getCodeCd(), codeGridRequest.getUseYn(), codeGridRequest.toDTO(codeGridRequest)));
    }
}
