package com.siginfo.schedule;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.siginfo.common.AppUtils;
import com.siginfo.dao.CommonDao;
import com.siginfo.dao.MainDao;
import com.siginfo.vo.SignalInfoVO;
import com.siginfo.vo.TbIntPhaseVO;
import com.siginfo.vo.TbIntStatusVO;
import com.siginfo.vo.TbIntVO;
import com.siginfo.vo.TbRegionCenterVO;
import com.siginfo.vo.TbSigunguVO;

@EnableScheduling
@Service
public class SignalSchedule {
	private static final Logger logger = LoggerFactory.getLogger(SignalSchedule.class);
	
	@Autowired
	private MainDao mainDao;
	
	@Autowired
	private CommonDao commonDao;
	
	@Autowired
	SigInfoRepository sigInfoRepository;
	
	@Scheduled(fixedDelay = 1000)
	public void run() {
		Map<String, Object> paramMap = new HashMap<>();
		try {
//			Long start= System.currentTimeMillis();
			paramMap.put("queryId", "getSignalInfoAll");
			List<SignalInfoVO> result = mainDao.getSignalInfoAll();
			paramMap.put("queryId", "getOnlineCenterStatus");
			List<Map<String, Object>> centerStatus = commonDao.list(paramMap);
//			Long end = System.currentTimeMillis();
			sigInfoRepository.setSigInfoList(result);
			sigInfoRepository.setCenterStatusList(centerStatus);
//			Long end2 = System.currentTimeMillis();
			
//			logger.info("쿼리 요청 시간 : {} ms ", (end - start));
//			logger.info("쿼리 요청 시간2 : {} ms",  (end2 - start));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
//	@Scheduled(fixedDelay = 1000 * 60 * 10)
	public void setSignalInfo() {
		try {
			Long start= System.currentTimeMillis();
			List<TbRegionCenterVO> regionCenterList = mainDao.getRegionCenterAll();
			List<TbIntVO> intList = mainDao.getIntListAll();
			List<TbIntPhaseVO> intPhaseList = mainDao.getIntPhaseList();
			List<TbSigunguVO> sigungu = mainDao.getSigungu();
			
			Long end = System.currentTimeMillis();
			
			for (TbRegionCenterVO vo : regionCenterList) {
				for (TbIntVO intVo : intList) {
					if (vo.getRegionCd().equals(intVo.getRegionCd())) {
						intVo.setRegionNm(vo.getRegionNm());
						intVo.setRegionId(vo.getRegionId());
						intVo.setCommState(vo.getCommState());
						vo.getIntMap().put(intVo.getIntNo(), intVo);
					}
				}
			}
			sigInfoRepository.setRegionCenterMap(regionCenterList);
			sigInfoRepository.setIntMap(intList);
			sigInfoRepository.setIntPhaseMap(intPhaseList);
			sigInfoRepository.setSigunguMap(sigungu);
			Long end2 = System.currentTimeMillis();
//			logger.info("setSignalInfo 쿼리 요청 시간 : {} ms", (end - start));
//			logger.info("setSignalInfo 쿼리 요청 시간2 : {} ms",  (end2 - start));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
//	@Scheduled(fixedDelay = 1000)
	public void setIntStatus() {
		try {
			Long start= System.currentTimeMillis();
			List<TbIntStatusVO> intStatus = mainDao.getIntStatus();
			Long end = System.currentTimeMillis();
			if (sigInfoRepository.intMap != null && sigInfoRepository.intMap.size() > 0 && sigInfoRepository.intPhaseMap != null && sigInfoRepository.intPhaseMap.size() > 0) {
				for (TbIntStatusVO vo : intStatus) {
					String key = vo.getRegionCd() + "_" + vo.getIntNo() + "_" + vo.getMapNo();
					if (sigInfoRepository.intPhaseMap.get(key) != null) {
						TbIntPhaseVO phase = sigInfoRepository.intPhaseMap.get(key);
						vo.setaRing1PhaseConfCd(phase.getaRing1PhaseConfCd());
						vo.setaRing2PhaseConfCd(phase.getaRing2PhaseConfCd());
						vo.setaRing3PhaseConfCd(phase.getaRing3PhaseConfCd());
						vo.setaRing4PhaseConfCd(phase.getaRing4PhaseConfCd());
						vo.setaRing5PhaseConfCd(phase.getaRing5PhaseConfCd());
						vo.setaRing6PhaseConfCd(phase.getaRing6PhaseConfCd());
						vo.setaRing7PhaseConfCd(phase.getaRing7PhaseConfCd());
						vo.setaRing8PhaseConfCd(phase.getaRing8PhaseConfCd());
						vo.setbRing1PhaseConfCd(phase.getbRing1PhaseConfCd());
						vo.setbRing2PhaseConfCd(phase.getbRing2PhaseConfCd());
						vo.setbRing3PhaseConfCd(phase.getbRing3PhaseConfCd());
						vo.setbRing4PhaseConfCd(phase.getbRing4PhaseConfCd());
						vo.setbRing5PhaseConfCd(phase.getbRing5PhaseConfCd());
						vo.setbRing6PhaseConfCd(phase.getbRing6PhaseConfCd());
						vo.setbRing7PhaseConfCd(phase.getbRing7PhaseConfCd());
						vo.setbRing8PhaseConfCd(phase.getbRing8PhaseConfCd());
						vo.setIntMainphase(phase.getIntMainphase());
						if (sigInfoRepository.intMap.get(vo.getRegionCd() + "_" + vo.getIntNo()) != null) {
							sigInfoRepository.intMap.get(vo.getRegionCd() + "_" + vo.getIntNo()).setStatus(vo);
						}
					}
				}
				sigInfoRepository.setIntStatusMap(intStatus);
			}
			Long end2 = System.currentTimeMillis();
//			logger.info("setIntStatus 쿼리 요청 시간 : {} ms", (end - start));
//			logger.info("setIntStatus 쿼리 요청 시간2 : {} ms",  (end2 - start));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
