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.AuthService;
import com.oqpo.api.service.system.CodeService;
import com.oqpo.api.web.dto.request.auth.AuthGridRequest;
import com.oqpo.api.web.dto.request.auth.AuthSearchGridRequest;
import com.oqpo.api.web.dto.request.auth.SaveAuthRequest;
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.auth.AuthInfoResponse;
import com.oqpo.api.web.dto.response.auth.AuthSearchListResponse;
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/auth")
public class AuthController {
    @Autowired
    private AuthService authService;

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


    /**
     * 설명 : 권한 검색
     *
     * @param sTopMenuNo
     * @param sAuthTpCd
     * @param sUseYn
     * @param sAuthNm
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "topMenuNo", value = "상위메뉴번호", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "authTpCd", value = "권한타입코드", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "useYn", value = "사용여부", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "authNm", value = "권한명", required = false, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "권한 검색")
    @GetMapping("/auth-search")
    public ResponseEntity<AuthSearchListResponse> searchAuthList(@RequestParam(value = "topMenuNo", required = false) String topMenuNo,
                                                                 @RequestParam(value = "authTpCd", required = false) String authTpCd,
                                                                 @RequestParam(value = "useYn", required = false) String useYn,
                                                                 @RequestParam(value = "authNm", required = false) String authNm) throws Exception {
        return ResponseEntity.ok(authService.searchAuthList(topMenuNo, authTpCd, useYn, authNm));
    }  


    /**
     * 설명 : 권한정보 저장
     *
     * @param userId
     * @param saveAuthRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "권한정보 저장")
    @PostMapping("/save-auth")
    public ResponseEntity<SaveResponse> saveAuth(@ApiIgnore String userId, @RequestBody @Valid SaveAuthRequest saveAuthRequest) throws Exception {
        authService.saveAuth(userId, saveAuthRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }


    /**
     * 설명 : 권한 헤더 정보
     *
     * @param authNo
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "authNo", value = "권한코드", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "권한 헤더 정보")
    @GetMapping("/info-auth")
    public ResponseEntity<AuthInfoResponse> infoAuth(@RequestParam(value = "authNo") String authNo) throws Exception {
        return ResponseEntity.ok(authService.selectAuthInfo(authNo));
    }


    /**
     * 설명 : 권한 상세 그리드 리스트
     *
     * @param authGridRequest
     * @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 AuthGridRequest authGridRequest) throws Exception {
        return ResponseEntity.ok(authService.authDtlGridList(authGridRequest.getAuthNo(), authGridRequest.toDTO(authGridRequest)));
    }

    /**
     * 설명 : 권한 팝업 검색 그리드 리스트
     *
     * @param authSearchGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "권한 팝업 검색 그리드 리스트")
    @PostMapping("/pop-grid-list")
    public ResponseEntity<GridResponse> detailGridList(@ApiIgnore String authTpCd, @RequestBody @Valid AuthSearchGridRequest authSearchGridRequest) throws Exception {
        return ResponseEntity.ok(authService.authSearchGridList(authTpCd, authSearchGridRequest.getSAuthTpCd(), authSearchGridRequest.getSAuthNm(), authSearchGridRequest.toDTO(authSearchGridRequest)));
    }
}
