package com.eactive.eai.rms.onl.common.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.server.EAIServer;
import com.eactive.eai.rms.common.context.MonitoringContext;
import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.data.entity.onl.server.EAIServerService;
import com.eactive.eai.rms.onl.common.dao.ServerInfoDAO;

@Service("eaiServerInfoService")
public class EaiServerInfoServiceImpl implements EaiServerInfoService {

	private static final Logger logger = Logger.getLogger(EaiServerInfoServiceImpl.class);

	/** TSEAIS001DAO table 의 DAO. */
	private ServerInfoDAO serverInfoDAO;

	private MonitoringContext monitoringContext;

	private EAIServerService eaiServerService;

	@Autowired
	public EaiServerInfoServiceImpl(ServerInfoDAO serverInfoDAO, MonitoringContext monitoringContext, EAIServerService eaiServerService) {
		this.serverInfoDAO = serverInfoDAO;
		this.monitoringContext = monitoringContext;
		this.eaiServerService = eaiServerService;
	}
	
	public void setTseais001DAO(ServerInfoDAO tseais001DAO) {
		this.serverInfoDAO = tseais001DAO;
	}

	private List<Map<String, String>> getServerList() {
		List<EAIServer> eaiServers = eaiServerService.selectEaiServerIpList();

		return eaiServers.stream().map(server -> {
			Map<String, String> serverMap = new HashMap<>();
			serverMap.put("EAISVRINSTNM", server.getEaisevrinstncname());
			serverMap.put("EAISVRIP", server.getEaisevrip());
			serverMap.put("EAISVRLSNPORT", server.getSevrlsnportname());
			serverMap.put("FAILOVERSVRNM", server.getFlovrsevrname());
			serverMap.put("HOSTNAME", server.getHostname());

			if (monitoringContext.getPortMapper() != null) {
				serverMap.put("WEBSERVERPORT", monitoringContext.getPortMapper().get(server.getEaisevrip()));
			}
			return serverMap;
		}).collect(Collectors.toList());
	}

	public List<Map<String, String>> getEaiServerIpList() {
		return getServerList();
	}

	public List<Map<String, String>> getEaiServerIpListDB() {
		// BAP인 경우 TSEAIBP03 테이블을 사용
		if (DataSourceContextHolder.getDataSourceType() != null
				&& DataSourceTypeManager.BAP.equals(DataSourceContextHolder.getDataSourceType().getName())) {
			return getBapServerIpList();
		}

		List<EAIServer> serverList = eaiServerService.selectEaiServerIpList();

		if (logger.isDebugEnabled()) {
			logger.debug(serverList);
		}

		return serverList.stream().map(server -> {
			Map<String, String> serverMap = new HashMap<>();
			serverMap.put("EAISVRINSTNM", server.getEaisevrinstncname());
			serverMap.put("EAISVRIP", server.getEaisevrip());
			serverMap.put("EAISVRLSNPORT", server.getSevrlsnportname());
			serverMap.put("FAILOVERSVRNM", server.getFlovrsevrname());
			serverMap.put("HOSTNAME", server.getHostname());

			if (monitoringContext.getPortMapper() != null) {
				serverMap.put("WEBSERVERPORT", monitoringContext.getPortMapper().get(server.getEaisevrip()));
			}
			return serverMap;
		}).collect(Collectors.toList());
	}

	public List<Map<String, String>> getBapServerIpList() {
		List<Map<String, String>> serverList = serverInfoDAO.getBapServerIpList(new HashMap());

		if (logger.isDebugEnabled()) {
			logger.debug(serverList);
		}

		for (Map<String, String> map : serverList) {
			String ip = map.get("EAISVRIP");

			try {
				String port = monitoringContext.getPortMapper().get(ip);
				map.put("WEBSERVERPORT", port);
			} catch (RuntimeException e) {
				logger.warn("", e);
			}
		}

		return serverList;

	}

	public List<Map<String, String>> getEaiServerIpListByHostName(String hostName) {
		List<EAIServer> serverList = eaiServerService.selectEaiServerIpList();

		return serverList.stream().filter(server -> hostName.equals(server.getHostname())).map(server -> {
			Map<String, String> serverMap = new HashMap<>();
			serverMap.put("EAISVRINSTNM", server.getEaisevrinstncname());
			serverMap.put("EAISVRIP", server.getEaisevrip());
			serverMap.put("EAISVRLSNPORT", server.getSevrlsnportname());
			serverMap.put("FAILOVERSVRNM", server.getFlovrsevrname());
			serverMap.put("HOSTNAME", server.getHostname());
			return serverMap;
		}).collect(Collectors.toList());
	}

	public List<Map<String, String>> getBapServerIpListByHostName(String hostName) {

		List<Map<String, String>> instList = new ArrayList<>();

		// 일괄전송 서버는 Query 가 다름.
		List<Map<String, String>> serverList = serverInfoDAO.getBapServerIpList(new HashMap());

		for (Map<String, String> map : serverList) {
			if (hostName.equals(map.get("HOSTNAME"))) {
				instList.add(map);
			}
		}

		return instList;
	}

	public List<Map<String, String>> findEaiSevrInstncNames() {

		List<Map<String, String>> serverIpList = null;
		try {
			serverIpList = getEaiServerIpList();
		} catch (Exception e) {
			logger.error(e);
		}

		List<Map<String, String>> selectOptions = new ArrayList<>();
		if (serverIpList == null) {
			return selectOptions;
		}

		for (Iterator it = serverIpList.iterator(); it.hasNext();) {
			Map<String, String> serverIpMap = (Map<String, String>) it.next();
			Map<String, String> map = new HashMap<>();

			map.put("CODE", serverIpMap.get("EAISVRINSTNM"));
			map.put("NAME", serverIpMap.get("EAISVRINSTNM"));

			selectOptions.add(map);
		}
		return selectOptions;
	}

	/**
	 * @return the eai batch server ip list
	 */
	public List<Map<String, String>> getBatServerIpList() {

		List<Map<String, String>> serverList = serverInfoDAO.getBatServerIpList(new HashMap());

		if (logger.isDebugEnabled()) {
			logger.debug(serverList);
		}

		for (Map<String, String> map : serverList) {
			String ip = map.get("EAISVRIP");

			try {
				String port = monitoringContext.getPortMapper().get(ip);
				map.put("WEBSERVERPORT", port);
			} catch (RuntimeException e) {
				logger.warn("");
			}
		}

		return serverList;

	}

	public List<Map<String, String>> getBatServerIpListByHostName(String hostName) {

		List<Map<String, String>> instList = new ArrayList<>();

		// 일괄전송 서버는 Query 가 다름.
		List<Map<String, String>> serverList = serverInfoDAO.getBatServerIpList(new HashMap());

		for (Map<String, String> map : serverList) {
			if (hostName.equals(map.get("HOSTNAME"))) {
				instList.add(map);
			}
		}

		return instList;
	}

}
