package com.eactive.eai.rms.onl.manage.adapter.sna;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.adapter.AdapterProp;
import com.eactive.eai.data.entity.onl.adapter.AdapterPropId;
import com.eactive.eai.data.entity.onl.server.EAIServer;
import com.eactive.eai.rms.common.acl.user.BizDao;
import com.eactive.eai.rms.common.base.OnlBaseService;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.data.entity.onl.adapter.AdapterPropService;
import com.eactive.eai.rms.data.entity.onl.server.EAIServerService;
import com.eactive.eai.rms.onl.manage.adapter.adapter.AdapterPropGroupDao;

import edu.emory.mathcs.backport.java.util.Collections;

@Service("snaAdapterService")
public class SnaAdapterService extends OnlBaseService {
	@Autowired
	private ComboService comboService;

	@Autowired
	@Qualifier("snaAdapterDao")
	private SnaAdapterDao dao;

	@Autowired
	private BizDao bizDao;

	@Autowired
	@Qualifier("adapterPropGroupDao")
	private AdapterPropGroupDao adapterPropGroupDao;
	
	@Autowired
	private AdapterPropService adapterPropService;
	
	@Autowired
	private EAIServerService eaiServerService;

	public HashMap selectList(int startNum, int endNum, HashMap<String, Object> paramMap) throws Exception {

		// 전체 목록수 얻기
		int totalCount = dao.selectListCount(paramMap);

		paramMap.put("startNum", startNum);// 시작 record
		paramMap.put("endNum", endNum);// 끝 record

		List<HashMap<String, Object>> list = dao.selectList(paramMap);

		HashMap map = new HashMap();

		map.put("totalCount", totalCount);
		map.put("list", list);

		return map;
	}

	public HashMap selectAppCode(HashMap<String, Object> paramMap) throws Exception {
		String apCode = (String) paramMap.get("apCode");

		List<HashMap> apCodeList = dao.selectApCodeList(paramMap);
		List<EAIServer> eaiServers = eaiServerService.selectEaiServerIpList();
		
		
		HashMap codeMap = null;

		apCode = apCode.toUpperCase();

		ArrayList apList = new ArrayList();
		for (HashMap codeListMap : apCodeList) {
			String codes = (String) codeListMap.get("PRPTY2VAL");
			if (null != codes) {
				String[] code = codes.split(",");
				for (int i = 0; i < code.length; i++) {
					codeMap = new HashMap();
					if (apCode.length() > 0) {
						if (code[i].startsWith(apCode) || code[i].endsWith(apCode)) {
							codeMap.put("apCode", code[i]);
							codeMap.put(codeListMap.get("EAISEVRINSTNCNAME"), 1);
							apList.add(codeMap);
						}
					} else {
						codeMap.put("apCode", code[i]);
						codeMap.put(codeListMap.get("EAISEVRINSTNCNAME"), 1);
						apList.add(codeMap);
					}
				}
			}
		}

		HashMap apMap = null;
		HashMap tmpMap = null;
		String keyI = "";
		String keyJ = "";
		ArrayList list = new ArrayList();
		HashMap resMap = null;
		for (int i = 0; i < apList.size(); i++) {
			apMap = new HashMap();
			apMap = (HashMap) apList.get(i);
			keyI = (String) apMap.get("apCode");
			resMap = new HashMap();
			resMap.put("apCode", keyI);
			for (int j = 0; j < apList.size(); j++) {
				tmpMap = new HashMap();
				tmpMap = (HashMap) apList.get(j);
				keyJ = (String) tmpMap.get("apCode");
				if (keyI.equals(keyJ)) {
					
					for (EAIServer eaiServer : eaiServers) {
						String eaisevrinstncname = eaiServer.getEaisevrinstncname();
						if (tmpMap.get(eaisevrinstncname) != null && (Integer) tmpMap.get(eaisevrinstncname) == 1) {
							resMap.put(eaisevrinstncname, 1);
						}
					}
				}
			}
			list.add(resMap);
		}

		// List 중복제거
		List resList = (List) list.stream().distinct().collect(Collectors.toCollection(ArrayList::new));
		// List 정렬
		Collections.sort(resList, new Comparator<HashMap<String, Object>>() {
			@Override
			public int compare(HashMap<String, Object> obj1, HashMap<String, Object> obj2) {
				String objStr = (String) obj1.get("apCode");
				return objStr.compareTo((String) obj2.get("apCode"));
			}

		});

		HashMap map = new HashMap();
		map.put("data", resList);
		return map;
	}

	public HashMap selectListToExcel(HashMap<String, Object> paramMap) throws Exception {

		List list = dao.selectListToExcel(paramMap);

		HashMap map = new HashMap();

		map.put("list", list);

		return map;
	}

	public List selectAdapterBizList(HashMap<String, Object> paramMap) throws Exception {

		return bizDao.selectAdapterBizList(paramMap);
	}

	public void updateOrDeleteAdapterProperty(HashMap paramMap) throws Exception {

		String prptygroupname = (String) paramMap.get("PRPTYGROUPNAME");
		String prptyname = (String) paramMap.get("PRPTYNAME");
		String prpty2val = (String) paramMap.get("PRPTY2VAL");

		AdapterPropId id = new AdapterPropId(prptygroupname, prptyname);

		if (StringUtils.isBlank(prpty2val)) {
			adapterPropService.deleteById(id);
			return;
		}

		AdapterProp adapterProp = adapterPropService.findById(id).orElse(new AdapterProp());
		adapterProp.setId(id);
		adapterProp.setPrpty2val(prpty2val);

		adapterPropService.save(adapterProp);
	}

}
