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

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("addB2bManService")
public class AddB2bManService extends BaseService {

	@Autowired
	AdapterGroupService adapterGroupService;

	@Autowired
	ChnlInfoIntegrationService chnlInfoIntegrationService;

	public List<AddB2bUI> selectList(AddB2bSearchUI addB2bSearchUI) throws Exception {

		addB2bSearchUI.setDataDstcd("B2B");

		List<String> chnlList = adapterGroupService.findAdptrNodeChnl(addB2bSearchUI.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(addB2bSearchUI.getDataDstcd());
		chnlInfoIntegrationSearch.setServiceType(addB2bSearchUI.getServiceType());
		chnlInfoIntegrationSearch.setSearchChnnl(addB2bSearchUI.getSearchChannel());

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

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

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

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

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

	public void update(AddB2bUI addB2bUI) throws Exception {

		addB2bUI.setDataDstcd("B2B");

		String serviceType = addB2bUI.getServiceType();

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

		ChnlInfoIntegrationSearch chnlInfoIntegrationSearch = new ChnlInfoIntegrationSearch();
		chnlInfoIntegrationSearch.setSearchDataDstcd(addB2bUI.getDataDstcd());
		chnlInfoIntegrationSearch.setServiceType(addB2bUI.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("B2B");
			chnlInfoIntegrationId.setCodename(newEnrollCode);

			chnlInfoIntegrationEntity.setChnlInfoIntegrationId(chnlInfoIntegrationId);

			chnlInfoIntegrationService.save(chnlInfoIntegrationEntity);
		}

		for (String deleteCode : whatDelete) {

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

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

	}

}