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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.flowcontrol.jms.FCQueueReceiver;
import com.eactive.eai.data.entity.onl.flowcontrol.jms.QueueMonitor;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.data.entity.onl.flowcontrol.jms.FCQueueReceiverSearch;
import com.eactive.eai.rms.data.entity.onl.flowcontrol.jms.FCQueueReceiverService;
import com.eactive.eai.rms.data.entity.onl.flowcontrol.jms.QueueMonitorService;
import com.eactive.eai.rms.onl.common.exception.EMSRuntimeException;

/**
 * JMS Queue 수신자 관리 서비스
 */
@Service
@Transactional
public class JmsQueueReceiverManService extends BaseService {

	@Autowired
	private QueueMonitorService queueMonitorService;

	@Autowired
	private JmsQueueReceiverManMapper mapper;

	@Autowired
	private FCQueueReceiverService service;

	public Page<JmsQueueReceiverManUI> selectList(Pageable pageable, FCQueueReceiverSearch uiSearch)
			throws Exception {
		Page<FCQueueReceiver> pageEntity = service.findPage(pageable, uiSearch);
		return pageEntity.map(mapper::toVo);
	}

	public Map<String, List<?>> selectListToExcel(FCQueueReceiverSearch uiSearch) throws Exception {
		Iterable<FCQueueReceiver> qReceivers = service.findAll(uiSearch);

		ArrayList<HashMap<String, Object>> list = new ArrayList<>();
		for (FCQueueReceiver item : qReceivers) {
			HashMap<String, Object> row = new HashMap<>();
			row.put("QUERCVERNAME", item.getQuercvername());
			row.put("BASCQUERCVERYN", item.getBascquercveryn());
			row.put("SEVRINSTNCNAME", item.getEaisevrinstncname());
			row.put("NONSYNCZTRANOBSTCLQUENAME", item.getNonsyncztranobstclquename());
			row.put("NONSYNCZTRANQUENAME", item.getNonsyncztranquename());
			row.put("QUEMSGSELCTCTNT", item.getQuemsgselctctnt());
			row.put("QUERCVERCNT", item.getQuercvercnt());
			row.put("QUERCVERDTALSCTNT", item.getQuercverdtalsctnt());
			row.put("SENDDLAYTTMVAL", item.getSenddlayttmval());
			row.put("SENDRETRALNOTMS", item.getSendretralnotms());
			list.add(row);
		}

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

		return map;
	}

	public JmsQueueReceiverManUI selectDetail(String queRcverName) throws Exception {
		FCQueueReceiver entity = service.getById(queRcverName);
		return mapper.toVo(entity);
	}

	public Map<String, List<ComboVo>> selectQueList(String instance) throws Exception {
		List<String> queusagdstcdList = Arrays.asList("1", "2");
		Iterable<QueueMonitor> queueEntitys = queueMonitorService.findAllOfNormalQueue(queusagdstcdList, instance);
		List<ComboVo> listComboQue = StreamSupport
				.stream(queueEntitys.spliterator(), false)
				.map(mapper::toComboVo)
				.collect(Collectors.toList());

		List<String> errorQueusagdstcdList = Arrays.asList("3", "4");
		Iterable<QueueMonitor> errorQueueEntitys = queueMonitorService
				.findAllOfNormalQueue(errorQueusagdstcdList, instance);
		List<ComboVo> errorQueueListCombo = StreamSupport
				.stream(errorQueueEntitys.spliterator(), false)
				.map(mapper::toComboVo)
				.collect(Collectors.toList());

		HashMap<String, List<ComboVo>> map = new HashMap<>();
		map.put("listQue", listComboQue);
		map.put("listErrQue", errorQueueListCombo);

		return map;
	}

	public void insert(JmsQueueReceiverManUI ui) {
		Optional<FCQueueReceiver> optionalQReceiver = service
				.findByQueueNameAndInstanceName(ui.getNonSynczTranQueName(), ui.getSevrInstncName());
		if (optionalQReceiver.isPresent())
			throw new EMSRuntimeException(
					"이미 등록된 큐명(" + ui.getNonSynczTranQueName() + ", " + ui.getSevrInstncName() + ")입니다.");
		if ("".equals(ui.getNonSynczTranObstclQueName()) || ui.getNonSynczTranObstclQueName() == null)
			ui.setNonSynczTranObstclQueName(" ");
		service.save(mapper.toEntity(ui));
	}

	public void update(JmsQueueReceiverManUI ui) {
		FCQueueReceiver entity = service.getById(ui.getQueRcverName());
		if ("".equals(ui.getNonSynczTranObstclQueName()) || ui.getNonSynczTranObstclQueName() == null)
			ui.setNonSynczTranObstclQueName(" ");
		mapper.updateToEntity(ui, entity);
		service.save(entity);
	}

	public void delete(String queRcverName) {
		service.deleteById(queRcverName);
	}

}
