package cn.axzo.msg.center.message.service.impl.oldmsg;

import cn.axzo.basics.profiles.api.UserProfileServiceApi;
import cn.axzo.basics.profiles.dto.basic.IdentityProfileDto;
import cn.axzo.msg.center.common.utils.BizAssertions;
import cn.axzo.msg.center.service.dto.IdentityDTO;
import cn.axzo.msg.center.service.enums.IdentityTypeEnum;
import com.google.common.collect.ImmutableSet;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author yanglin
 */
@Component
@RequiredArgsConstructor
public class OldMsgIdentifyFilter {

    private final UserProfileServiceApi userProfileServiceApi;

    public OldMsgIdentifyInfo getIdentifyInfo(Long personId, boolean asManager)  {
        Set<IdentityTypeEnum> identifiesFilter;
        // cms或者管理端
        if (asManager) {
            identifiesFilter = ImmutableSet.of(IdentityTypeEnum.PRACTITIONER, IdentityTypeEnum.WORKER_LEADER);
        }
        // 工人端
        else  {
            identifiesFilter = ImmutableSet.of(IdentityTypeEnum.WORKER);
        }
        List<IdentityProfileDto> profiles = BizAssertions.assertResponse(userProfileServiceApi
                .getPersonIdentityProfile(personId), "获取身份信息失败, personId={}", personId);
        ArrayList<IdentityDTO> identities = new ArrayList<>();
        for (IdentityProfileDto profile : profiles) {
            IdentityTypeEnum identityType = IdentityTypeEnum
                    .codeOf(profile.getIdentityType().getCode())
                    .orElse(null);
            if (identityType == null || !identifiesFilter.contains(identityType)) {
                continue;
            }
            IdentityDTO identify = new IdentityDTO();
            identify.setId(profile.getId());
            identify.setType(identityType);
            identities.add(identify);
        }
        return new OldMsgIdentifyInfo(personId, identities);
    }
}