package com.eactive.eai.rms.onl.manage.comm.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

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

import com.eactive.eai.data.entity.onl.server.EAIServer;
import com.eactive.eai.rms.common.base.OnlBaseService;
import com.eactive.eai.rms.data.entity.onl.adapter.AdapterGroupService;
import com.eactive.eai.rms.data.entity.onl.server.EAIServerService;
import com.eactive.eai.rms.onl.common.exception.BizException;

@Service
@Transactional
public class ServerManService extends OnlBaseService {

	private EAIServerService eaiServerService;

	private EAIServerUIMapper eaiServerUIMapper;

	private AdapterGroupService adapterGroupService;

	@Autowired
	public ServerManService(EAIServerService eaiServerService, EAIServerUIMapper eaiServerUIMapper,
			AdapterGroupService adapterGroupService) {
		this.eaiServerService = eaiServerService;
		this.eaiServerUIMapper = eaiServerUIMapper;
		this.adapterGroupService = adapterGroupService;
	}

	public Page<EAIServerUI> selectList(Pageable pageable) {
		Page<EAIServerUI> page = eaiServerService.findAll(pageable).map(eaiServerUIMapper::toVo);

		page.stream().forEach(ui -> {
			Optional<String> socketMonitorPortOptional = adapterGroupService
					.getSocketMonitorPort(ui.getEaiSevrInstncName());
			ui.setSocketport(socketMonitorPortOptional.orElse(null));
		});

		return page;
	}

	public List<Map<String, Object>> selectListToExcel() {

		Pageable pageable = Pageable.unpaged();
		pageable.getSort().and(Sort.by("adptrbzwkgroupname"));

		return selectList(pageable).stream().map(eaiServerUI -> {
			Map<String, Object> map = new HashMap<>();
			map.put("EAISEVRINSTNCNAME", eaiServerUI.getEaiSevrInstncName());
			map.put("EAISEVRIP", eaiServerUI.getEaiSevrIp());
			map.put("SEVRLSNPORTNAME", eaiServerUI.getSevrLsnPortName());
			map.put("FLOVRSEVRNAME", eaiServerUI.getFlovrSevrName());
			map.put("HOSTNAME", eaiServerUI.getHostName());
			map.put("CLOUDROUTERURL", eaiServerUI.getCloudRouterUrl());
			map.put("SOCKETPORT", eaiServerUI.getSocketport());
			return map;
		}).collect(Collectors.toList());
	}

	public EAIServerUI selectDetail(String eaiSevrInstncName) {
		EAIServer eaiServer = eaiServerService.getById(eaiSevrInstncName);
		return eaiServerUIMapper.toVo(eaiServer);
	}

	public void update(EAIServerUI eaiServerUI) {
		EAIServer eaiServer = eaiServerService.getById(eaiServerUI.getEaiSevrInstncName());
		eaiServerUIMapper.updateEntity(eaiServerUI, eaiServer);
		eaiServerService.save(eaiServer);
	}

	public void insert(EAIServerUI eaiServerUI) {
		String eaiSevrInstncName = eaiServerUI.getEaiSevrInstncName();
		if (eaiServerService.existsById(eaiSevrInstncName)) {
			throw new BizException("서버 인스턴스명[" + eaiSevrInstncName + "]은 이미 등록되어 있습니다.");
		}

		EAIServer eaiServer = eaiServerUIMapper.toEntity(eaiServerUI);
		eaiServerService.save(eaiServer);
	}

	public void delete(String eaiSevrInstncName) {
		eaiServerService.deleteById(eaiSevrInstncName);
	}

}
