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

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

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.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.apim.portal.jpa.PersonalDataEncryptConverter;
import com.eactive.apim.portal.user.entity.UserInfo;
import com.eactive.eai.data.converter.LocalDateTimeFormatters;
import com.eactive.eai.data.entity.onl.unifbwk.UnifBwkTp;
import com.eactive.eai.rms.common.acl.user.BizManService;
import com.eactive.eai.rms.common.acl.user.UserDao;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.man.user.UserInfoService;
import com.eactive.eai.rms.data.entity.onl.unifbwk.UnifBwkTpService;

@Service
@Transactional
public class UnifbwkManService extends BaseService {

	private final UnifBwkTpService unifBwkTpService;
	private final UnifbwkUIMapper unifbwkUIMapper;
	private final BizManService bizManService;
	private final UserInfoService userInfoService;	
	private final UserDao userDao;	
	private final PersonalDataEncryptConverter encryptConverter = new PersonalDataEncryptConverter();

	@Autowired
	public UnifbwkManService(UnifBwkTpService unifBwkTpService,
							 UnifbwkUIMapper unifbwkUIMapper,
							 BizManService bizManService,
							 UserInfoService userInfoService,							 
							 @Qualifier("userDao") UserDao userDao) {
		this.unifBwkTpService = unifBwkTpService;
		this.unifbwkUIMapper = unifbwkUIMapper;
		this.bizManService = bizManService;
		this.userInfoService = userInfoService;		
		this.userDao = userDao;
	}

	public Page<UnifbwkUI> selectList(Pageable pageable, UnifbwkUISearch unifbwkUISearch) {
		Page<UnifBwkTp> page = unifBwkTpService.findPageWithSearch(pageable, unifbwkUISearch);
		return page.map(this::getUserOrgInfo);
	}

	private UnifbwkUI getUserOrgInfo(UnifBwkTp unifBwkTp) {
		UnifbwkUI unifbwkUI = unifbwkUIMapper.toVo(unifBwkTp);
		String rspblpsncntpctnt = encryptConverter.convertToEntityAttribute(unifBwkTp.getRspblpsncntpctnt());
		unifbwkUI.setRsPblpsncntpcTnt(rspblpsncntpctnt);
		Optional<UserInfo> userInfo = Optional.empty();

		if (StringUtils.isNotBlank(rspblpsncntpctnt)) {
			userInfo = userInfoService.findByOfctelno(rspblpsncntpctnt);
		}
		if (userInfo.isPresent()) {
			unifbwkUI.setTrackAsisKey1Name(userInfo.get().getDvsnname());
			unifbwkUI.setTrackAsisKey2Name(userInfo.get().getTeamname());
		}
		return unifbwkUI;
	}

	public long selectCnt(Map<String, String> paramMap) {
		return unifBwkTpService.selectCnt(paramMap);
	}

	public UnifbwkUI selectDetail(String eaiBzwkDstcd) {
		UnifBwkTp unifBwkTp = unifBwkTpService.getById(eaiBzwkDstcd);
		UnifbwkUI unifbwkUI = unifbwkUIMapper.toVo(unifBwkTp);

		String rspblpsncntpctnt = encryptConverter.convertToEntityAttribute(unifBwkTp.getRspblpsncntpctnt());
		unifbwkUI.setRsPblpsncntpcTnt(rspblpsncntpctnt);

		Optional<UserInfo> optionalUserInfo = Optional.empty();

		if (StringUtils.isNotBlank(rspblpsncntpctnt)) {
			optionalUserInfo = userInfoService.findByOfctelno(rspblpsncntpctnt);
		}
		if (optionalUserInfo.isPresent()) {
			UserInfo userInfo = optionalUserInfo.get();
			unifbwkUI.setTrackAsisKey1Name(userInfo.getDvsnname());
			unifbwkUI.setTrackAsisKey2Name(userInfo.getTeamname());
		}
		return unifbwkUI;
	}

	public void insert(UnifbwkUI unifbwkUI) {
		UnifBwkTp unifBwkTp = unifbwkUIMapper.toEntity(unifbwkUI);
		unifBwkTp.setRspblpsncntpctnt(encryptConverter.convertToDatabaseColumn(unifBwkTp.getRspblpsncntpctnt()));
		unifBwkTpService.save(unifBwkTp);

		bizManService.transactionAdminBiz(unifbwkUI.getEaiBzwkDstcd());
	}

	public void update(UnifbwkUI unifbwkUI) {
		UnifBwkTp unifBwkTp = unifBwkTpService.getById(unifbwkUI.getEaiBzwkDstcd());
		String rspblPsnCntpCtnt = unifbwkUI.getRsPblpsncntpcTnt();

		String trackAsisKey1Name = "";
		String trackAsisKey2Name = "";

		if (StringUtils.isNotBlank(rspblPsnCntpCtnt)) {
			Optional<UserInfo> optionalUserInfo = userInfoService.findByOfctelno(rspblPsnCntpCtnt);
			if (optionalUserInfo.isPresent()) {
				trackAsisKey1Name = optionalUserInfo.get().getDvsnname();
				trackAsisKey2Name = optionalUserInfo.get().getTeamname();
			}
		}
		unifbwkUI.setTrackAsisKey1Name(trackAsisKey1Name);
		unifbwkUI.setTrackAsisKey2Name(trackAsisKey2Name);

		unifbwkUIMapper.updateToEntity(unifbwkUI, unifBwkTp);
		unifBwkTp.setRspblpsncntpctnt(encryptConverter.convertToDatabaseColumn(rspblPsnCntpCtnt));
		unifBwkTpService.save(unifBwkTp);
	}

	public void delete(String eaiBzwkDstcd) {
		unifBwkTpService.deleteById(eaiBzwkDstcd);
	}

	public Map<String, Object>[] selectListToExcel() {
		@SuppressWarnings("unchecked")
		Map<String, Object>[] mapArray = unifBwkTpService.findAll(Sort.by("eaibzwkdstcd")).stream().map(e -> {

			Map<String, Object> map = new HashMap<>();
			map.put("EAIBZWKDSTCD", e.getEaibzwkdstcd());
			map.put("BZWKDSTICNAME", e.getBzwkdsticname());
			map.put("APPLCD", e.getApplcd());
			map.put("BZWKDSTICDESC", e.getBzwkdsticdesc());
			map.put("TRACKASISKEY1NAME", e.getTrackasiskey1name());
			map.put("TRACKASISKEY2NAME", e.getTrackasiskey2name());
			map.put("RSEMPNAME", e.getRsempname());
			map.put("RSPBLPSNCNTPCTNT", encryptConverter.convertToEntityAttribute(e.getRspblpsncntpctnt()));
			map.put("GROUPCOCD", e.getGroupcocd());
			if (e.getLastModDtm() != null) {
				map.put("LAST_MOD_DTM",
						e.getLastModDtm().format(LocalDateTimeFormatters.FORMATTER_YYYY_MM_DD_HH_MM_SS_19));
			} else {
				map.put("LAST_MOD_DTM", "");
			}
			map.put("DISCARDDATE", e.getDiscarddate());
			map.put("INPUTDSTCD", e.getInputdstcd());
			return map;
		}).toArray(HashMap[]::new);

		return mapArray;
	}

	public void insertIqms(Map<String, String> paramMap) {
		UnifbwkUI unifbwkUI = new UnifbwkUI();
		unifbwkUI.setEaiBzwkDstcd(paramMap.get("eaiBzwkDstcd"));
		unifbwkUI.setBzwkDsticName(paramMap.get("bzwkDsticName"));
		unifbwkUI.setApplCd(paramMap.get("applCd"));
		unifbwkUI.setRsEmpName(paramMap.get("rsEmpName"));
		unifbwkUI.setRsPblpsncntpcTnt(paramMap.get("rsPblpsncntpcTnt"));
		unifbwkUI.setGroupCoCd(paramMap.get("groupCoCd"));

		if (StringUtils.isNotBlank(paramMap.get("bzwkDsticDesc"))) {
			unifbwkUI.setBzwkDsticDesc(paramMap.get("bzwkDsticDesc"));
		}
		if (StringUtils.isNotBlank(paramMap.get("trackAsisKey1Name"))) {
			unifbwkUI.setTrackAsisKey1Name(paramMap.get("trackAsisKey1Name"));
		}
		if (StringUtils.isNotBlank(paramMap.get("trackAsisKey2Name"))) {
			unifbwkUI.setTrackAsisKey2Name(paramMap.get("trackAsisKey2Name"));
		}
		if (StringUtils.isNotBlank(paramMap.get("inputDstCd"))) {
			unifbwkUI.setInputDstCd(paramMap.get("inputDstCd"));
		}
		if (StringUtils.isNotBlank(paramMap.get("discardDate"))) {
			unifbwkUI.setDiscardDate(paramMap.get("discardDate"));
		}

		UnifBwkTp unifBwkTp = unifbwkUIMapper.toEntity(unifbwkUI);
		unifBwkTp.setRspblpsncntpctnt(encryptConverter.convertToDatabaseColumn(unifBwkTp.getRspblpsncntpctnt()));
		unifBwkTpService.save(unifBwkTp);
	}

	public void updateIqms(Map<String, String> paramMap) {
		Optional<UnifBwkTp> optionalUnifBwkTp = unifBwkTpService.findById(paramMap.get("eaiBzwkDstcd"));

		if (optionalUnifBwkTp.isPresent()) {
			UnifbwkUI unifbwkUI = new UnifbwkUI();
			unifbwkUI.setEaiBzwkDstcd(paramMap.get("eaiBzwkDstcd"));
			unifbwkUI.setBzwkDsticName(paramMap.get("bzwkDsticName"));
			unifbwkUI.setApplCd(paramMap.get("applCd"));
			unifbwkUI.setRsPblpsncntpcTnt(paramMap.get("rsPblpsncntpcTnt"));
			unifbwkUI.setRsEmpName(paramMap.get("rsEmpName"));
			unifbwkUI.setGroupCoCd(paramMap.get("groupCoCd"));

			if (StringUtils.isNotBlank(paramMap.get("bzwkDsticDesc"))) {
				unifbwkUI.setBzwkDsticDesc(paramMap.get("bzwkDsticDesc"));
			}
			if (StringUtils.isNotBlank(paramMap.get("trackAsisKey1Name"))) {
				unifbwkUI.setTrackAsisKey1Name(paramMap.get("trackAsisKey1Name"));
			}
			if (StringUtils.isNotBlank(paramMap.get("trackAsisKey2Name"))) {
				unifbwkUI.setTrackAsisKey2Name(paramMap.get("trackAsisKey2Name"));
			}
			if (StringUtils.isNotBlank(paramMap.get("inputDstCd"))) {
				unifbwkUI.setInputDstCd(paramMap.get("inputDstCd"));
			}
			if (StringUtils.isNotBlank(paramMap.get("discardDate"))) {
				unifbwkUI.setDiscardDate(paramMap.get("discardDate"));
			}

			unifbwkUIMapper.updateToEntity(unifbwkUI, optionalUnifBwkTp.get());
			optionalUnifBwkTp.get().setRspblpsncntpctnt(encryptConverter.convertToDatabaseColumn(unifbwkUI.getRsPblpsncntpcTnt()));
			unifBwkTpService.save(optionalUnifBwkTp.get());
		}
	}

	public List<UnifbwkUI> selectBizCodeList() {
		List<UnifBwkTp> unifBwkTps = unifBwkTpService.findAll();
		return unifBwkTps.stream().map(entity -> {
			UnifbwkUI unifbwkUI = new UnifbwkUI();
			unifbwkUI.setEaiBzwkDstcd(entity.getEaibzwkdstcd());
			unifbwkUI.setApplCd(entity.getApplcd());
			return unifbwkUI;
		}).collect(Collectors.toList());
	}

}
