package com.eactive.eai.rms.common.mmilog;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.StreamSupport;

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 org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.code.Code;
import com.eactive.eai.rms.data.entity.onl.code.CodeId;
import com.eactive.eai.rms.data.entity.onl.code.CodeService;
import com.eactive.eai.rms.data.entity.onl.mmilog.MMILog;
import com.eactive.eai.rms.data.entity.onl.mmilog.MMILogSearch;
import com.eactive.eai.rms.data.entity.onl.mmilog.MMILogService;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class MMILogManService extends BaseService {

	private MMILogService mmiLogService;

	private CodeService codeService;

	private MMILogUIMapper mmiLogUIMapper;

	@Autowired
	public MMILogManService(MMILogService mmiLogService, CodeService codeService, MMILogUIMapper mmiLogUIMapper) {
		this.mmiLogService = mmiLogService;
		this.codeService = codeService;
		this.mmiLogUIMapper = mmiLogUIMapper;
	}

	public Page<MMILogUI> selectList(Pageable pageable, MMILogSearch mmiLogSearch) {
		Page<MMILog> list = mmiLogService.findAll(pageable, mmiLogSearch);

		return list.map(log -> {
			MMILogUI ui = mmiLogUIMapper.toVo(log);

			CodeId codeId = new CodeId("INOUT_TYPE", log.getEaiSevrDstcd());
			String eaiSevrDstcd = codeService.findById(codeId).map(Code::getCodename).orElse("");

			ui.setEaiSevrDstcd("[" + log.getEaiSevrDstcd() + "] " + eaiSevrDstcd);

//			//mmi방식
//			String returnCd = "return_code=\"SUCCESS\"";

//			//eims방식
			String returnCdString = "<Result>True</Result>";
			// RES로 returnCode 받기
			ui.setReturnCd(log.getResDstcd().contains(returnCdString) ? "SUCCESS" : "FAIL");
			ui.setReqDstcd("");
			ui.setResDstcd("");
			return ui;
		});
	}

	public MMILogUI selectDetail(MMILogUI mmiLogUI) {
		MMILog mmiLog = mmiLogService.getById(mmiLogUIMapper.toId(mmiLogUI));
		return mmiLogUIMapper.toVo(mmiLog);
	}

	public Map<String, Object>[] selectListToExcel(MMILogSearch mmiLogSearch) {
		Iterable<MMILog> list = mmiLogService.findAll(mmiLogSearch);

		@SuppressWarnings("unchecked")
		Map<String, Object>[] mapArray = StreamSupport.stream(list.spliterator(), false).map(log -> {
			Map<String, Object> map = new HashMap<>();
			map.put("MSGDPSTHMS", log.getId().getMsgDpstHms());
			map.put("INTERFACEID", log.getInterfaceId());
			map.put("EAISVCNAME", log.getId().getEaiSvcName());
			map.put("EAISEVRDSTCD", log.getEaiSevrDstcd());
			map.put("CNVSNNAME", log.getCnvsnName());
			map.put("LOUTNAME", log.getLoutName());
			map.put("DEPLOYID", log.getDeployId());

			String returnCd = "<Result>True</Result>";
			map.put("RETURNCD", log.getResDstcd().contains(returnCd) ? "SUCCESS" : "FAIL");
			return map;
		}).toArray(size -> new HashMap[size]);
		return mapArray;
	}
}
