package gmt.status;

import gmt.common.type.LogLevelType;
import gmt.common.type.STAStatusType;
import gmt.common.util.GmtUtils;
import gmt.logger.GmtLogManager;
import gmt.status.event.IGmtHeartBeatHandler;
import gmt.status.event.IGmtServicesStatusHandler;
import gmt.status.event.IGmtSysStatusHandler;
import gmt.status.vo.CurrentSTAVO;
import gmt.status.vo.STAObjVO;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;

public class GmtSTAManager {

	private Thread statusSendThread;
	private IGmtSysStatusHandler sysStatus;
	private IGmtHeartBeatHandler hrbStatus;
	private IGmtServicesStatusHandler serStatus;
	private GmtLogManager logger;
	private ArrayList<STAObjVO> staList;
	private CurrentSTAVO currentStatus;

	public GmtSTAManager(IGmtSysStatusHandler sysHandler, IGmtHeartBeatHandler hrbHandler,
                         IGmtServicesStatusHandler serHandler) {
		this.sysStatus = sysHandler;
		this.hrbStatus = hrbHandler;
		this.serStatus = serHandler;
		
//		GmtLogManager.setpath("");
		logger = GmtLogManager.getInstance();
		staList = new ArrayList<STAObjVO>();
		currentStatus = new CurrentSTAVO();

		statusSendThread = new Thread(new Runnable() {

			@Override
			public void run() {
				LocalDateTime checkDT = LocalDateTime.now();
				LocalDateTime hrbDT = LocalDateTime.now();
				LocalDateTime serviceDT = LocalDateTime.now();

				while (true) { // ?
					try {
						Duration duration1 = Duration.between(checkDT, LocalDateTime.now());
						Duration duration2 = Duration.between(hrbDT, LocalDateTime.now());
						Duration duration3 = Duration.between(serviceDT, LocalDateTime.now());
						
						if (duration1.getSeconds() >= 2) {
							checkDT = LocalDateTime.now();
	
							if (sysStatus != null) {
								sysStatus.sendStatus();
							}
						}
						Thread.sleep(1000);
						
						if (duration2.getSeconds() >= 30) {
							hrbDT = LocalDateTime.now();
							if (hrbStatus != null) {
								hrbStatus.sendHRB();
							}
						}
						Thread.sleep(1000);
						
						if (duration3.getSeconds() >= 60) {
							serviceDT = LocalDateTime.now();
							if (serStatus != null) {
								serStatus.sendServiceStatus();
							}
						}
						
						Thread.sleep(1);
						
					} catch (Exception e) {
						logger.writeLevelLog(GmtUtils.getStatckTrace(e), LogLevelType.LOG_ERROR, "AllLog");
					}
				}
			}
		});
	}

	public void start() {
		statusSendThread.start();
	}

	private void checkStatus() {
		checkSysStatus();
		checkStatusAll();
	}

	private void checkNetStatus() {
		//
	}

	private void checkSysStatus() {
		if (staList != null) {
			return;
		}

		STAObjVO vo = new STAObjVO();

		vo.setId("");
		vo.setName("");
		vo.setRemark("");
		vo.setStatus(STAStatusType.ssNormal);

		for (int i = 0; i < staList.size(); i++) {
			if (!staList.get(i).isUse()) {
				currentStatus.setStatus(STAStatusType.ssNormal);
				currentStatus.setRemark("정상");
				currentStatus.setUpdateDT(LocalDateTime.now());

				continue;
			}

			if (staList.get(i).getStatus() == STAStatusType.ssNormal) {
				currentStatus.setStatus(STAStatusType.ssNormal);
				currentStatus.setRemark("정상");
				currentStatus.setUpdateDT(LocalDateTime.now());
			} else {
				vo = staList.get(i);
				currentStatus.setStatus(vo.getStatus());
				currentStatus.setRemark(vo.getRemark());
				currentStatus.setUpdateDT(LocalDateTime.now());
				return;
			}
		}

		if (vo.getStatus() == STAStatusType.ssNormal) {
			vo.setRemark("정상");
		}

		String staMsg = "";

	}

	private void checkStatusAll() {

	}

}
