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

import com.eactive.apim.portal.agreements.entity.AgreementType;
import com.eactive.apim.portal.portalorg.entity.PortalOrg;
import com.eactive.apim.portal.portaluser.entity.PortalUser;
import com.eactive.apim.portal.portaluser.entity.PortalUserPrivacyAgreement;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.apim.portaluser.PortalUserService;
import com.eactive.eai.rms.data.entity.onl.apim.portaluserterms.PortalUserTermsSearch;
import com.eactive.eai.rms.data.entity.onl.apim.portaluserterms.PortalUserTermsService;
import com.eactive.eai.rms.onl.apim.masking.MaskingUtils;
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 org.springframework.util.StringUtils;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class PortalUserTermsManService extends BaseService {
	private final PortalUserTermsService portalUserTermsService;
	private final PortalUserService portalUserService;

	@Autowired
	public PortalUserTermsManService(PortalUserTermsService portalUserTermsService,
									 PortalUserService portalUserService){
		this.portalUserTermsService = portalUserTermsService;
		this.portalUserService = portalUserService;
	}

	private PortalUser findUser(String userId) {
		return portalUserService.getById(userId);
	}

	public Page<PortalUserTermsUI> selectList(Pageable pageable, PortalUserTermsSearch portalUserTermsSearch) {
		Page<PortalUserPrivacyAgreement> page = portalUserTermsService.findAll(pageable, portalUserTermsSearch);
		return page.map(agreement -> convertToUI(agreement, true));
	}

	private PortalUserTermsUI convertToUI(PortalUserPrivacyAgreement agreement, boolean isMasked) {
		PortalUserTermsUI ui = new PortalUserTermsUI();

		// 약관정보
		ui.setId(agreement.getId());
		ui.setTermsType(agreement.getTermsType());
		ui.setTermsVersion(agreement.getTermsVersion());
		ui.setAgreementDate(agreement.getAgreementDate());
		ui.setTermsTypeName(AgreementType.valueOf(agreement.getTermsType()).getDescription());

		// 사용자정보
		PortalUser user = portalUserService.getById(agreement.getCreatedBy());
		ui.setCreatedByName(isMasked ? MaskingUtils.maskName(user.getUserName()) : user.getUserName());
		ui.setCreatedByLoginId(isMasked ? MaskingUtils.maskEmailId(user.getLoginId()) : user.getLoginId());

		// 법인명
		PortalOrg portalOrg = user.getPortalOrg();
		ui.setUserOrgName((portalOrg != null && StringUtils.hasText(user.getPortalOrg().getOrgName())) ? user.getPortalOrg().getOrgName() : "");

		return ui;
	}

	public Page<PortalUserTermsUI> selectListUnmask(Pageable pageable, PortalUserTermsSearch portalUserTermsSearch) {
		Page<PortalUserPrivacyAgreement> page = portalUserTermsService.findAll(pageable, portalUserTermsSearch);
		return page.map(agreement -> convertToUI(agreement, false));
	}

}
