package com.ningdatech.pmapi.meeting.helper;

import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.helper.RegionCacheHelper;
import com.ningdatech.pmapi.common.model.entity.ViewRegionDTO;
import com.ningdatech.pmapi.sys.model.entity.Role;
import com.ningdatech.pmapi.user.entity.enumeration.RoleEnum;
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * MeetingDataScopeHelper
 * </p>
 *
 * @author WendyYang
 * @since 2023/4/27
 **/
@Component
@AllArgsConstructor
public class MeetingDataScopeHelper {

    private final RegionCacheHelper regionCacheHelper;

    private static RoleEnum getRolePriorityMax() {
        List<Role> roleList = LoginUserUtil.loginUserDetail().getUserRoleList();
        List<String> roleCodeList = CollUtils.fieldList(roleList, Role::getCode);
        if (roleCodeList.contains(RoleEnum.SUPER_ADMIN.name())) {
            return RoleEnum.SUPER_ADMIN;
        }
        if (roleCodeList.contains(RoleEnum.REGION_MANAGER.name())) {
            return RoleEnum.REGION_MANAGER;
        }
        return RoleEnum.EXPERT_ADMIN;
    }

    public ViewRegionDTO meetingListViewRegions() {
        RoleEnum role = getRolePriorityMax();
        UserInfoDetails user = LoginUserUtil.loginUserDetail();
        String regionCode = user.getRegionCode();
        Integer regionLevel = user.getRegionLevel();
        Collection<String> regions;
        switch (role) {
            case SUPER_ADMIN:
                regions = null;
                break;
            case REGION_MANAGER:
                regions = regionCacheHelper.listChildRegionCodeList(regionCode, regionLevel);
                break;
            case EXPERT_ADMIN:
                regions = Collections.singletonList(regionCode);
                break;
            default:
                regions = Collections.emptyList();
                break;
        }
        return new ViewRegionDTO(regions, regionLevel);
    }

}
