package com.tsiwebserver.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;
import com.tsiwebserver.VO.AlarmConfigVO;
import com.tsiwebserver.VO.AlarmVO;
import com.tsiwebserver.VO.ApiInvokeVO;
import com.tsiwebserver.VO.CommunicationVO;
import com.tsiwebserver.VO.CpuUseVO;
import com.tsiwebserver.VO.KafkaStatusVO;
import com.tsiwebserver.VO.ServerStatusVO;
import com.tsiwebserver.VO.TscApiClientVO;
import com.tsiwebserver.mapper.DashBoardMapper;
import com.tsiwebserver.service.DashBoardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class DashBoardServiceImpl implements DashBoardService {
	
	private final DashBoardMapper dm;

	public List<KafkaStatusVO> getKafkaStatus() {
		return dm.getKafkaStatus();
	}

	public List<CommunicationVO> getCommuticationVO() {
		return dm.getCommuticationVO();
	}

	public KafkaStatusVO getOneKafkaStatus() {
		return dm.getOneKafkaStatus();
	}

	public List<CpuUseVO> getUseCpuVo() {
		return dm.getUseCpuVo();
	}

	public CommunicationVO getOneCommunicationStatus() {
		return dm.getOneCommunicationStatus();
	}

	public List<ApiInvokeVO> getApiInvokeVO() {
		return dm.getApiInvokeVO();
	}

	public List<AlarmVO> getAlarmVO() {
		return dm.getAlarmVO();
	}

	public int alarmChecker(JSONArray array) {
		int affectedRow=0;
		for(int ii = 0; ii < array.size(); ii++) {
			Map<String, Object> map = new HashMap<String, Object>();
			JSONObject ob = (JSONObject)array.get(ii);
			map.put("alarmTarget", (String)ob.get("target"));
			map.put("alarmCode", (String)ob.get("code"));
			map.put("occrDt", LocalDateTime.parse((String)ob.get("time"),DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			log.info(map.toString());
			affectedRow += dm.alarmChecker(map);
		}
		return affectedRow;
	}

	public List<AlarmVO> getSearchService(String inputVal) {
		return dm.getAlarmCodeSearch(inputVal);
	}

	public List<ServerStatusVO> getServerStat() {
		return dm.getServerStat();
	}

	@Override
	public List<AlarmConfigVO> getAllAlarmCode() {
		return dm.getAllAlarmCode();
	}

	@Override
	public List<ServerStatusVO> getOneCpuUse() {
		return dm.getOneCpuUse();
	}

	@Override
	public List<ServerStatusVO> getOneMemUse() {
		return dm.getOneMemUse();
	}
	
	@Override
	public List<ServerStatusVO> getOneDiskUse() {
		return dm.getOneDiskUse();
	}

	@Override
	public List<ServerStatusVO> getOneResourceUse() {
		return dm.getOneResourceUse();
	}

	@Override
	public List<AlarmConfigVO> getAlarmWarning(){
		return dm.getAlarmWarning();
	}
}
