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

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

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

import com.eactive.eai.rms.onl.common.service.EaiServerInfoService;
import com.eactive.eai.rms.onl.common.util.DataSetUtil;
import com.eactive.eai.rms.onl.common.vo.HostInfomationForm;

@Service("OnlJspCallAgentUtilService")
public class OnlJspCallAgentUtilServiceImpl extends AbstractJspCallAgentUtilService {

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

	@Autowired
	@Qualifier("eaiServerInfoService")
	private EaiServerInfoService eaiServerInfoService;

	@Override
	public List<Map<String, Object>> agentCallByGetMethod(String resourceName, Map<String, String> parameters,
			Map<String, String> filter) {

		List<Map<String, String>> serverList = eaiServerInfoService.getEaiServerIpList();
//      List<Map<String, String>> hostList    = eaiServerInfoService.getHostServerIpList();

		List<String> urlList = new ArrayList<>();

		for (Map<String, String> serverMap : serverList) {
			urlList.add("http://" + serverMap.get("EAISVRIP") + ":" + serverMap.get("EAISVRLSNPORT")
					+ HostInfomationForm.WEB_AGENT_CONTEXT_PATH + resourceName);
		}

		String[] url = urlList.toArray(new String[] {});

		DataSetUtil gUtil = new DataSetUtil();
		gUtil.setFilter(new HashMap<>(filter));
		List<HashMap<String, Object>> list = gUtil.flushXmlToClient(url);

		return new ArrayList<>(list);
	}

	@Override
	public List<Map<String, Object>> agentCallByGetMethod(String resourceName, List<String> specificInstanceNames,
			Map<String, String> parameters, Map<String, String> filter) {

		List<Map<String, String>> serverList = eaiServerInfoService.getEaiServerIpList();
//        List<Map<String, String>> hostList    = eaiServerInfoService.getHostServerIpList();

		List<String> urlList = new ArrayList<>();

		for (Map<String, String> serverMap : serverList) {
			if (specificInstanceNames.contains(serverMap.get("EAISVRINSTNM"))) {
				urlList.add("http://" + serverMap.get("EAISVRIP") + ":" + serverMap.get("EAISVRLSNPORT")
						+ HostInfomationForm.WEB_AGENT_CONTEXT_PATH + resourceName);
			}
		}

		String[] url = urlList.toArray(new String[] {});

		DataSetUtil gUtil = new DataSetUtil();
		gUtil.setFilter(new HashMap<>(filter));
		List<HashMap<String, Object>> list = gUtil.flushXmlToClient(url);

		return new ArrayList<>(list);
	}

}