package com.tsi.monitoring.controller;

import com.tsi.monitoring.repository.GlobalStatusInfoRepository;
import com.tsi.monitoring.service.SectionService;
import com.tsi.monitoring.service.UtisService;
import com.tsi.monitoring.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

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

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/api/monitoring")
public class MonitoringController {
    private final SectionService sectionService;
    private final UtisService utisService;
    private final GlobalStatusInfoRepository repository;

    @PostMapping("/getLocalCenterLinkInfos")
    @ResponseBody
    public List<LocalCenterLinkVo> getCenterInfos() {
        return this.repository.getLocalCenterValue();
    }

    @PostMapping("/getLogHistoryInfos")
    @ResponseBody
    public LogHistoryVo getLogHistoryInfos(@RequestBody Map<String, Object> paramMap) {
        return this.sectionService.getLogHistory(paramMap);
    }

    @PostMapping("/getProcessStatus")
    @ResponseBody
    public List<ProcessStatusVo> getProcessStatus() {
        return this.repository.getProcessStatusValue();
    }

    @PostMapping("/getAllProcessStatus")
    @ResponseBody
    public List<ProcessStatusVo> getAllProcessStatusValue() {
        return this.utisService.getAllProcessStatusValue();
    }

    @PostMapping("/refreshLocalCenterLinkInfos")
    @ResponseBody
    public List<LocalCenterLinkVo> refreshCenterInfos() {
        return this.sectionService.refreshCenterInfos();
    }

    @PostMapping("/refreshProcessStatus")
    @ResponseBody
    public List<ProcessStatusVo> refreshProcessStatus() {
        return this.utisService.getRefreshStatusValue();
    }

    @PostMapping("/getProcessLogHistoryInfos")
    @ResponseBody
    public List<SendRcvHistVo> getProcessLogHistoryInfos(@RequestBody Map<String, Object> paramMap) {
        List<ProcessStatusVo> prList = this.repository.getProcessStatusValue();
        List<SendRcvHistVo> result = new ArrayList<>();
        if (prList != null && prList.size() > 0) {
            for (ProcessStatusVo vo : prList) {
                if (vo.getLinkHistory() == null) continue;
                if (paramMap.get("processId").equals(vo.getProcessId())) {
                    result = vo.getLinkHistory();
                }
            }
        }
        return result;
    }

    @PostMapping("/getAllStatistics")
    @ResponseBody
    public AllStatisticsVo getAllStatistics(@RequestBody Map<String, Object> paramMap) {
        return this.sectionService.getAllStatistics((String)paramMap.get("centerId"));
    }

    @PostMapping("/getLinkedAllStatistics")
    @ResponseBody
    public AllStatisticsVo getLinkedAllStatistics() {
//        return this.sectionService.getAllStatistics();
        return this.utisService.getAllStatistics();
    }
}
