package com.eactive.eai.rms.onl.apim.approvalline;

import java.util.List;
import java.util.stream.Collectors;

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.apim.portal.approvalline.entity.PortalApprovalLine;
import com.eactive.apim.portal.approvalline.entity.PortalApprovalLineUser;
import com.eactive.apim.portal.approvalline.entity.PortalApprovalLineUserId;
import com.eactive.apim.portal.approvalline.service.PortalApprovalLineService;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.man.user.UserInfoService;

import lombok.RequiredArgsConstructor;

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

	private final PortalApprovalLineService portalApprovalLineService;

	private final PortalApprovalLineUIMapper portalApprovalLineUIMapper;
	private final PortalApprovalLineUserUIMapper portalApprovalLineUserUIMapper;
	private final UserInfoService userInfoService;

	public Page<PortalApprovalLineUI> selectList(Pageable pageable) {
		return portalApprovalLineService.findAll(pageable).map(portalApprovalLineUIMapper::toVo);
	}

	public PortalApprovalLineUI selectDetail(String id) {
		PortalApprovalLine portalApprovalLine = portalApprovalLineService.getById(id);
		return portalApprovalLineUIMapper.toVo(portalApprovalLine);
	}

	public void insert(PortalApprovalLineUI portalApprovalLineUI) {
		PortalApprovalLine portalApprovalLine = new PortalApprovalLine();
		portalApprovalLine.setApprovalType(portalApprovalLineUI.getApprovalType());
		portalApprovalLine.setApprovalName(portalApprovalLineUI.getApprovalName());

		portalApprovalLine = portalApprovalLineService.save(portalApprovalLine);

		List<PortalApprovalLineUser> approvalLineUsers = createApprovalLineUsers(portalApprovalLine,
				portalApprovalLineUI.getPortalApprovalLineUsers());
		portalApprovalLine.setPortalApprovalLineUsers(approvalLineUsers);

		portalApprovalLineService.save(portalApprovalLine);
	}

	private List<PortalApprovalLineUser> createApprovalLineUsers(PortalApprovalLine portalApprovalLine,
			List<PortalApprovalLineUserUI> userUIList) {

		return userUIList.stream().map(userUI -> {
			PortalApprovalLineUser approvalLineUser = portalApprovalLineUserUIMapper.toEntity(userUI);

			PortalApprovalLineUserId id = new PortalApprovalLineUserId();
			id.setApprovalLineId(portalApprovalLine.getId());
			id.setUserId(userUI.getUserId());

			approvalLineUser.setId(id);
			approvalLineUser.setUser(userInfoService.getById(userUI.getUserId()));
			approvalLineUser.setApprovalLine(portalApprovalLine);
			return approvalLineUser;
		}).collect(Collectors.toList());

	}

	public void update(PortalApprovalLineUI portalApprovalLineUI) {
		PortalApprovalLine portalApprovalLine = portalApprovalLineService.getById(portalApprovalLineUI.getId());

		portalApprovalLine.setApprovalName(portalApprovalLineUI.getApprovalName());
		portalApprovalLine.setApprovalType(portalApprovalLineUI.getApprovalType());

		portalApprovalLine.getPortalApprovalLineUsers().clear();

		List<PortalApprovalLineUser> portalApprovalLineUsers = createApprovalLineUsers(portalApprovalLine,
				portalApprovalLineUI.getPortalApprovalLineUsers());

		portalApprovalLine.getPortalApprovalLineUsers().addAll(portalApprovalLineUsers);

		portalApprovalLineService.save(portalApprovalLine);
	}

	public void delete(String id) {
		portalApprovalLineService.deleteById(id);
	}

	public List<String> getUsedApprovalTypes() {
		return portalApprovalLineService.findAll().stream()
					.map(PortalApprovalLine::getApprovalType).distinct()
					.collect(Collectors.toList());
	}

}
