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

import com.eactive.apim.portal.agreements.entity.AgreementType;
import com.eactive.apim.portal.agreements.entity.Agreements;
import com.eactive.apim.portal.agreements.entity.AgreementsId;
import com.eactive.apim.portal.agreements.service.AgreementsService;
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.apim.portalterms.PortalTermsService;
import org.apache.commons.lang.StringEscapeUtils;
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;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class PortalTermsManService extends BaseService {
	private final PortalTermsService portalTermsService;
	private final PortalTermsUIMapper portalTermsUIMapper;
	private final AgreementsService agreementsService;
	private final UserInfoService userInfoService;

	@Autowired
	public PortalTermsManService(PortalTermsService portalTermsService,
								 PortalTermsUIMapper portalTermsUIMapper,
								 AgreementsService agreementsService,
								 UserInfoService userInfoService){
		this.portalTermsService = portalTermsService;
		this.portalTermsUIMapper = portalTermsUIMapper;
		this.agreementsService = agreementsService;
		this.userInfoService = userInfoService;
	}

	private String findName(String userId) {
		return userInfoService.getById(userId).getUsername();
	}

	public Page<PortalTermsUI> selectList(Pageable pageable, String searchAgreementsType) {
		return portalTermsService.findAll(pageable, searchAgreementsType)
				.map(agreements -> {
					PortalTermsUI portalTermsUI = portalTermsUIMapper.toVo(agreements);
					portalTermsUI.setCreatedByName(findName(agreements.getCreatedBy()));
					portalTermsUI.setLastModifiedByName(findName(agreements.getLastModifiedBy()));
					portalTermsUI.setName(agreements.getAgreementsType().getDescription());
					return portalTermsUI;
				});
	}

	public PortalTermsUI selectDetail(String agreementsType, int revision) {
		AgreementsId agreementsId = new AgreementsId(AgreementType.fromCode(agreementsType), revision);
		Agreements agreements = portalTermsService.getById(agreementsId);
		PortalTermsUI portalTermsUI = portalTermsUIMapper.toVo(agreements);
		portalTermsUI.setCreatedByName(findName(agreements.getCreatedBy()));
		portalTermsUI.setLastModifiedByName(findName(agreements.getLastModifiedBy()));
		portalTermsUI.setName(agreements.getAgreementsType().getDescription());
		return portalTermsUI;
	}

	public void insert(PortalTermsUI portalTermsUI) {
		portalTermsUI.setName(AgreementType.fromCode(portalTermsUI.getAgreementsType()).getDescription());
		portalTermsUI.setContents(StringEscapeUtils.unescapeHtml(portalTermsUI.getContents()));
		Agreements agreements = portalTermsUIMapper.toEntity(portalTermsUI);
		agreementsService.save(agreements);
	}

	public void update(PortalTermsUI portalTermsUI) {
		Agreements agreements = agreementsService.findById(portalTermsUI.getAgreementsType(), portalTermsUI.getRevision());
		agreements.setName(AgreementType.fromCode(portalTermsUI.getAgreementsType()).getDescription());
		agreements.setContents(StringEscapeUtils.unescapeHtml(portalTermsUI.getContents()));
		agreements.setPublishedOn(portalTermsUI.getPublishedOn());
		agreementsService.save(agreements);
	}

	public void delete(String agreementsType, Integer revision) {
		agreementsService.delete(agreementsType, revision);
	}
}
