package com.eactive.eai.rms.onl.manage.serviceinfo.addfailover;

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.adapter.AdapterGroupService;
import com.eactive.eai.rms.data.entity.onl.chnlInfoIntegration.ChnlInfoIntegrationEntity;
import com.eactive.eai.rms.data.entity.onl.chnlInfoIntegration.ChnlInfoIntegrationId;
import com.eactive.eai.rms.data.entity.onl.chnlInfoIntegration.ChnlInfoIntegrationSearch;
import com.eactive.eai.rms.data.entity.onl.chnlInfoIntegration.ChnlInfoIntegrationService;
import edu.emory.mathcs.backport.java.util.Arrays;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service("addFailoverManService")
public class AddFailoverManService extends BaseService {

	@Autowired
	AdapterGroupService adapterGroupService;

	@Autowired
	ChnlInfoIntegrationService chnlInfoIntegrationService;

	public List<AddFailoverUI> selectList(AddFailoverSearchUI addFailoverSearchUI) throws Exception {

		addFailoverSearchUI.setDataDstcd("FAILOVER");

		List<String> chnlList = adapterGroupService.findAdptrNodeChnl(addFailoverSearchUI.getSearchChannel());
		List<HashMap<String, String>> returnList = new ArrayList<>();

		for (String tmpStr : chnlList) {
			HashMap<String, String> addListItem = new HashMap<>();

			addListItem.put("CHNNL", tmpStr);
			returnList.add(addListItem);
		}
		ChnlInfoIntegrationSearch chnlInfoIntegrationSearch = new ChnlInfoIntegrationSearch();

		chnlInfoIntegrationSearch.setSearchDataDstcd(addFailoverSearchUI.getDataDstcd());
		chnlInfoIntegrationSearch.setServiceType(addFailoverSearchUI.getServiceType());
		chnlInfoIntegrationSearch.setSearchChnnl(addFailoverSearchUI.getSearchChannel());

		List<ChnlInfoIntegrationEntity> enrollList = chnlInfoIntegrationService.selectList(chnlInfoIntegrationSearch);

		List<AddFailoverUI> resultList = new ArrayList<>();

		for (HashMap chnl : returnList) {
			AddFailoverUI temp = new AddFailoverUI();

			boolean match = enrollList.stream().map(enroll -> enroll.getId().getCodename())
					.anyMatch(Predicate.isEqual(chnl.get("CHNNL")));

			if (addFailoverSearchUI.getSearchEnrolledYN().equals("Y") && match) {
				temp.setChnnlNm((String) chnl.get("CHNNL"));
				temp.setEnrolled(match ? "Y" : "N");
				resultList.add(temp);
			} else if (addFailoverSearchUI.getSearchEnrolledYN().equals("N") && !match) {
				temp.setChnnlNm((String) chnl.get("CHNNL"));
				temp.setEnrolled(match ? "Y" : "N");
				resultList.add(temp);
			}
		}
		return resultList;
	}

	public void update(AddFailoverUI addFailoverUI) throws Exception {

		addFailoverUI.setDataDstcd("FAILOVER");

		String serviceType = addFailoverUI.getServiceType();

		String newEnroll = addFailoverUI.getPrpty2Val();
		List<String> newEnrollList = Arrays.asList(newEnroll.split(";")); // newEnroll From View

		ChnlInfoIntegrationSearch chnlInfoIntegrationSearch = new ChnlInfoIntegrationSearch();
		chnlInfoIntegrationSearch.setSearchDataDstcd(addFailoverUI.getDataDstcd());
		chnlInfoIntegrationSearch.setServiceType(addFailoverUI.getServiceType());

		List<ChnlInfoIntegrationEntity> enrolledList = chnlInfoIntegrationService.selectList(chnlInfoIntegrationSearch);

		List<String> whatNewEnroll = newEnrollList.stream().filter(codeName -> enrolledList.stream()
				.map(map -> map.getId().getCodename()).noneMatch(Predicate.isEqual(codeName)))
				.collect(Collectors.toList());

		List<String> whatDelete = enrolledList.stream().map(map -> map.getId().getCodename())
				.filter(codeName -> newEnrollList.stream().noneMatch(Predicate.isEqual(codeName)))
				.collect(Collectors.toList());

		for (String newEnrollCode : whatNewEnroll) {

			if (StringUtils.isEmpty(newEnrollCode)) {
				continue;
			}

			ChnlInfoIntegrationEntity chnlInfoIntegrationEntity = new ChnlInfoIntegrationEntity();
			ChnlInfoIntegrationId chnlInfoIntegrationId = new ChnlInfoIntegrationId();

			chnlInfoIntegrationId.setServicetype(serviceType);
			chnlInfoIntegrationId.setDatadstcd("FAILOVER");
			chnlInfoIntegrationId.setCodename(newEnrollCode);

			chnlInfoIntegrationEntity.setChnlInfoIntegrationId(chnlInfoIntegrationId);

			chnlInfoIntegrationService.save(chnlInfoIntegrationEntity);
		}

		for (String deleteCode : whatDelete) {

			if (StringUtils.isEmpty(deleteCode)) {
				continue;
			}

			chnlInfoIntegrationService.delete("FAILOVER", serviceType, deleteCode);
		}
	}

}