package jnpf.cultivate.event.impl.course;

import cn.hutool.core.collection.CollUtil;
import jnpf.cultivate.event.JnpfApplicationEventService;
import jnpf.cultivate.service.FtbCultivatePromotionNewService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.event.dto.course.CourseEventDTO;
import jnpf.model.cultivate.event.dto.course.PositionCourseEventDTO;
import jnpf.permission.vo.v2.user.UserBoundVO;
import jnpf.permission.vo.v2.user.UserPageListVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
@RequiredArgsConstructor
public class JnpfApplicationEventPositionCourseService implements JnpfApplicationEventService {

    private final FtbCultivatePromotionNewService ftbCultivatePromotionNewService;
    private final JnpfApplicationEventCourseService jnpfApplicationEventCourseService;

    @Autowired
    private UserApiV2Util userApiV2Util;

    @Override
    public boolean isSupportedCourseEvent(Object event) {
        return event instanceof PositionCourseEventDTO;
    }

    @Override
    public void handlerCourseEvent(Object courseEvent) {
        PositionCourseEventDTO positionCourseEventDTO = (PositionCourseEventDTO) courseEvent;
        List<String> userIds = null;
        // 确定课程学习人员
        if (positionCourseEventDTO.getEventType() == PositionCourseEventDTO.EventType.USER) {
            userIds = positionCourseEventDTO.getUserIds();
        } else if (positionCourseEventDTO.getEventType() == PositionCourseEventDTO.EventType.POST) {
            // 查询该岗位下所有用户

            List<UserBoundVO> userBaseInfoByPositions = userApiV2Util.getUserListForPositions(positionCourseEventDTO.getPostIds(),null);
            // 根据岗位id获取其所有用户，本岗课程
            userIds = new ArrayList<>();
            if (CollUtil.isNotEmpty(userBaseInfoByPositions)) {
                userIds = userBaseInfoByPositions.stream().map(UserBoundVO::getId).collect(Collectors.toList());
            }
            // 学习地图选择了此岗位且该岗位所对应的学习阶段未完成的用户
            List<String> unfinishedUser = ftbCultivatePromotionNewService.querySelectedPersonnelBasedOnPositionID(positionCourseEventDTO.getPostIds());
            userIds.addAll(unfinishedUser);
            userIds = userIds.stream().distinct().collect(Collectors.toList());
        } else if (positionCourseEventDTO.getEventType() == PositionCourseEventDTO.EventType.ORGANIZATION) {
            // 组织
            List<UserPageListVO> userListForOrgIds = userApiV2Util.getUserListForOrgIds(positionCourseEventDTO.getOrganizationIds(),null);
            if (CollUtil.isNotEmpty(userListForOrgIds)) {
                userIds = userListForOrgIds.stream().map(UserPageListVO::getId).collect(Collectors.toList());
            }
        }

        // 课程进度，参与培训所有人员需要学习的课程,线下培训课程需要和岗位学习课程同步
        jnpfApplicationEventCourseService.handlerCourseEvent(
                CourseEventDTO.builder()
                        .courseIds(positionCourseEventDTO.getCourseIds())
                        .userIds(userIds)
                        .whetherToChange(positionCourseEventDTO.getWhetherToChange())
                        .build());

    }

}
