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

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

import javax.transaction.Transactional;

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

import com.eactive.eai.data.entity.onl.b2badaptermapping.B2BAdapterMap;
import com.eactive.eai.data.entity.onl.b2badaptermapping.B2BAdapterMapId;
import com.eactive.eai.data.entity.onl.message.EAIMessageEntity;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.data.entity.onl.b2badaptermap.B2bAdapterMapService;
import com.eactive.eai.rms.data.entity.onl.b2badaptermap.B2bAdapterMapSearch;
import com.eactive.eai.rms.data.entity.onl.eaimsg.EAIMessageService;
import com.eactive.eai.rms.onl.common.service.EaiServerInfoService;
import com.eactive.eai.rms.onl.manage.adapter.adapter.AdapterManService;
import com.eactive.eai.rms.onl.manage.adapter.adapter.ui.AdapterGroupUI;
import com.eactive.eai.rms.onl.manage.adapter.adapter.ui.AdapterUI;

@Service
@Transactional
public class B2bAdapterMapManService extends BaseService {

	@Autowired
	private ComboService comboService;

	@Autowired
	private AdapterManService adapterManService;

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

	@Autowired
	private B2bAdapterMapManMapper mapper;

	@Autowired
	private B2bAdapterMapService service;

	@Autowired
	private EAIMessageService eaiMessageService;

	public Page<B2bAdapterMapUI> selectList(Pageable pageable, B2bAdapterMapSearch uiSearch) {
		Page<B2BAdapterMap> page = service.findPage(pageable, uiSearch);
		return page.map(mapper::toVo);
	}

	public Map<String, List<?>> selectListToExcel(B2bAdapterMapSearch uiSearch) {
		Iterable<B2BAdapterMap> b2bAdapterMaps = service.findAll(uiSearch);
		ArrayList<HashMap<String, Object>> list = new ArrayList<>();
		for(B2BAdapterMap item : b2bAdapterMaps) {
			HashMap<String, Object> row = new HashMap<>();
			row.put("eaisevrinstncname", item.getId().getEaisevrinstncname());
			row.put("eaisvcname", item.getId().getEaisvcname());
			row.put("extnlinstiidnfiname", item.getId().getExtnlinstiidnfiname());
			row.put("dmnddtalsbzwkdsticname", item.getId().getDmnddtalsbzwkdsticname());
			row.put("psvsysadptrbzwkgroupname", item.getPsvsysadptrbzwkgroupname());
			row.put("adptrbzwkname", item.getAdptrbzwkname());
			list.add(row);
		}
		
		HashMap<String, List<?>> map = new HashMap<>();
		map.put("list", list);

		return map;
	}

	public B2bAdapterMapUI selectDetail(B2bAdapterMapUI b2bAdapterMapUI) {
		B2BAdapterMapId id = new B2BAdapterMapId();
		id.setEaisevrinstncname(b2bAdapterMapUI.getEaiSevrInstncName());
		id.setEaisvcname(b2bAdapterMapUI.getEaiSvcName());
		id.setExtnlinstiidnfiname(b2bAdapterMapUI.getExtnlInstiIdnfiName());
		id.setDmnddtalsbzwkdsticname(b2bAdapterMapUI.getDmndDtalsBzwkDsticName());
		B2BAdapterMap adapterMap = service.getById(id);

		B2bAdapterMapUI uiVo = mapper.toVo(adapterMap);

		Optional<EAIMessageEntity> optionalEaiMessageEntity = eaiMessageService.findById(b2bAdapterMapUI.getEaiSvcName());
		optionalEaiMessageEntity.ifPresent(eaiMessageEntity -> uiVo.setEaiSvcDesc(eaiMessageEntity.getEaisvcdesc()));

		return uiVo;
	}

	public void insert(B2bAdapterMapUI uiVo) {
		B2BAdapterMap entity = mapper.toEntity(uiVo);
		service.save(entity);
	}

	public void update(B2bAdapterMapUI uiVo) {
		B2BAdapterMap entity = service.getById(mapper.toEntity(uiVo).getId());
		mapper.updateToEntity(uiVo, entity);
		service.save(entity);
	}

	public void delete(B2bAdapterMapUI b2bAdapterMapUI) {
		B2BAdapterMapId id = new B2BAdapterMapId();
		id.setEaisevrinstncname(b2bAdapterMapUI.getEaiSevrInstncName());
		id.setEaisvcname(b2bAdapterMapUI.getEaiSvcName());
		id.setExtnlinstiidnfiname(b2bAdapterMapUI.getExtnlInstiIdnfiName());
		id.setDmnddtalsbzwkdsticname(b2bAdapterMapUI.getDmndDtalsBzwkDsticName());
		service.deleteById(id);
	}

	public Map<String, List<?>> selectListDetailCombo() {
		List<Map<String, String>> instanceList = eaiServerInfoService.findEaiSevrInstncNames();// EAI 서버 인스턴스명
		List<AdapterGroupUI> ouAdapterList = adapterManService.selectListComboForAdptrIoDstcd("OU");// 수동시스템인터페이스유형
		List<ComboVo> extList = comboService.getFromB2BService();// 기관코드

		HashMap<String, List<?>> map = new HashMap<>();
		map.put("instanceList", instanceList);
		map.put("ouAdapterList", ouAdapterList);
		map.put("extList", extList);

		return map;
	}

	public Map<String, List<?>> selectListDynamicCombo(String psvSysAdptrBzwkGroupName) {
		HashMap<String, List<?>> map = new HashMap<>();
		if(StringUtils.isEmpty(psvSysAdptrBzwkGroupName))
			return map;
		
		List<AdapterUI> ouAdapterList = adapterManService.selectListAdapter(psvSysAdptrBzwkGroupName);
		map.put("ouAdapterList", ouAdapterList);

		return map;
	}

}
