package com.arms.api.serverinfo.controller;

import com.arms.api.serverinfo.model.dto.ServerInfoDTO;
import com.arms.api.serverinfo.model.vo.ServerInfoVO;
import com.arms.api.serverinfo.model.entity.ServerInfoEntity;
import com.arms.api.serverinfo.service.ServerInfoService;
import com.arms.api.util.errors.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/engine/serverInfo")
public class ServerInfoController {

    @Autowired
    private ServerInfoService serverInfoService;

    @ResponseBody
    @RequestMapping(
            value = {""},
            method = {RequestMethod.POST}
    )
    public ServerInfoEntity saveOrUpdateServerInfo(@RequestBody ServerInfoDTO serverInfoDTO) {

        if (serverInfoDTO == null) {
            throw new IllegalArgumentException(ErrorCode.SERVER_INFO_ERROR.getErrorMsg());
        }
        else if (serverInfoDTO.getConnectId() == null) {
            throw new IllegalArgumentException(ErrorCode.SERVER_ID_INFO_ERROR.getErrorMsg());
        }
        else if (StringUtils.isEmpty(serverInfoDTO.getUri())) {
            throw new IllegalArgumentException(ErrorCode.SERVER_URI_INFO_ERROR.getErrorMsg());
        }
        else if (StringUtils.isEmpty(serverInfoDTO.getUserId())) {
            throw new IllegalArgumentException(ErrorCode.SERVER_ID_INFO_ERROR.getErrorMsg());
        }
        else if (StringUtils.isEmpty(serverInfoService.getDecryptPasswordOrToken(serverInfoDTO.getPasswordOrToken()))) {
            throw new IllegalArgumentException(ErrorCode.SERVER_PW_OR_API_TOKEN_INFO_ERROR.getErrorMsg());
        }
        else if (StringUtils.isEmpty(serverInfoDTO.getType())) {
            throw new IllegalArgumentException(ErrorCode.SERVER_TYPE_INFO_ERROR.getErrorMsg());
        }

        log.info("[ServerInfoController :: saveOrUpdateServerInfo] :: {}", serverInfoDTO);

        return serverInfoService.saveOrUpdateServerInfo(serverInfoDTO);
    }

    @GetMapping("/listByConnectIds")
    public List<ServerInfoVO> serverInfoListByConnectIds(@RequestParam("connectIds") List<String> connectIds) {
        log.info("[ServerInfoController :: serverInfoListByConnectIds] :: {}", connectIds.toString());

        return serverInfoService.serverInfoListByConnectIds(connectIds);
    }


    @PostMapping(value = {"/backup/scheduler"})
    public void serverInfoBackup() {
        serverInfoService.serverInfoBackup();
    }

    /*
    * 삭제 관련 차후 설계 후 개발 진행
    * */
    @ResponseBody
    @RequestMapping(
            value = {""},
            method = {RequestMethod.DELETE}
    )
    public ServerInfoVO deleteServerInfo(@RequestBody ServerInfoVO serverInfoVO) throws Exception {

        if (serverInfoVO == null || serverInfoVO.getConnectId() == null) {
            throw new Exception("삭제 요청 데이터가 없습니다.");
        }

        log.info("[ServerInfoController :: deleteServerInfo] :: {}", serverInfoVO.getConnectId());

        serverInfoService.deleteServerInfo(serverInfoVO.getConnectId());

        return serverInfoVO;

    }

    @ResponseBody
    @RequestMapping(
            value = {"/serverTypeMap"},
            method = {RequestMethod.GET}
    )
    public ResponseEntity<Map<String, String>> getServerTypeMap() {

        log.info("[ServerInfoController :: getServerTypeMap]");

        Map<String, String> serverTypeMap = serverInfoService.getServerIdToTypeMap();
        return ResponseEntity.ok(serverTypeMap);
    }
}
