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

import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.oper.InterfaceService;
import com.oqpo.api.web.dto.request.oper.iflink.InterfaceGridRequest;
import com.oqpo.api.web.dto.request.oper.iflink.SaveInterfaceRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.oper.iflink.InterfaceResponse;
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/interface")
public class InterfaceController {

    @Autowired
    private InterfaceService interfaceService;

    /**
     * 설명 : 외부연동 그리드 리스트
     *
     * @param interfaceGridRequest
     * @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 InterfaceGridRequest interfaceGridRequest) throws Exception {
        return ResponseEntity.ok(interfaceService.selectInterfaceGridList(interfaceGridRequest.getFromDt(), interfaceGridRequest.getToDt(),
                interfaceGridRequest.getSLinkInfo(), interfaceGridRequest.getSIfId(), interfaceGridRequest.getSUseYn(),
                interfaceGridRequest.getSIfNm(), interfaceGridRequest.getSComMethCd(),
                interfaceGridRequest.toDTO(interfaceGridRequest)));
    }

    /**
     * 설명 : 외부연동 정보 저장
     *
     * @param userId
     * @param saveInterfaceRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "외부연동 정보 저장")
    @PostMapping("/save-interface")
    public ResponseEntity<SaveResponse> saveInterface(@ApiIgnore String userId, @RequestBody @Valid SaveInterfaceRequest saveInterfaceRequest) throws Exception {
        interfaceService.saveInterface(userId, saveInterfaceRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 외부연동 정보 삭제
     *
     * @param userId
     * @param ifNo
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "ifNo", value = "IF번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "외부연동 정보 삭제")
    @GetMapping("/remove-interface")
    public ResponseEntity<SaveResponse> removeInterface(@ApiIgnore String userId, @RequestParam(value = "ifNo") String ifNo) throws Exception {
        interfaceService.removeInterface(userId, ifNo);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.DELETE_OK));
    }

    /**
     * 설명 : 외부연동 정보
     *
     * @param ifNo
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "ifNo", value = "IF번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "외부연동 정보")
    @GetMapping("/info-interface")
    public ResponseEntity<InterfaceResponse> infoInterface(@RequestParam(value = "ifNo") String ifNo) throws Exception {
        return ResponseEntity.ok(interfaceService.selectInterfaceInfo(ifNo));
    }

}
