package jnpf.cultivate.event.impl.trigger.task;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.cultivate.event.base.TriggerProcessor;
import jnpf.cultivate.event.base.course.CourseProcessLearnAbstract;
import jnpf.cultivate.mapper.FtbCultivateCourseMapper;
import jnpf.cultivate.mapper.FtbCultivateLearnTaskCourseMapper;
import jnpf.model.cultivate.bo.TriggerEventBO;
import jnpf.model.cultivate.event.JnpfApplicationEvent;
import jnpf.model.cultivate.event.dto.examidentify.ExamIdentifyTriggerEventDTO;
import jnpf.model.cultivate.event.dto.task.TaskCourseProcessDTO;
import jnpf.model.cultivate.po.course.FtbCultivateCourse;
import jnpf.model.cultivate.po.learn.FtbCultivateLearnTaskCourse;
import jnpf.util.context.SpringContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
@RequiredArgsConstructor
@Slf4j
public class TaskProcessLearnProcessor extends CourseProcessLearnAbstract<TaskCourseProcessDTO> implements TriggerProcessor<TaskCourseProcessDTO> {

    @Autowired
    private  FtbCultivateLearnTaskCourseMapper ftbCultivateLearnTaskCourseMapper;

    @Autowired
    private  FtbCultivateCourseMapper ftbCultivateCourseMapper;

    @Override
    public boolean triggerTypeConfirmation(TriggerEventBO.TriggerEventType triggerEventType) {
        return triggerEventType == TriggerEventBO.TriggerEventType.TASK_COURSE_EVENT;
    }
    @Override
    public void triggerHandle(TaskCourseProcessDTO triggerEvent) {
        super.courseProcessLearn(triggerEvent);
        // 任务必修课程是否全部学完
        LambdaQueryWrapper<FtbCultivateLearnTaskCourse> taskCourseQueryWrapper = Wrappers.lambdaQuery();
        taskCourseQueryWrapper.eq(FtbCultivateLearnTaskCourse::getTaskId, triggerEvent.getTaskId());
        taskCourseQueryWrapper.eq(FtbCultivateLearnTaskCourse::getEnableMark, 0);
        taskCourseQueryWrapper.eq(FtbCultivateLearnTaskCourse::getIsRequired, 0);
        List<FtbCultivateLearnTaskCourse> ftbCultivateLearnTaskCourses = ftbCultivateLearnTaskCourseMapper.selectList(taskCourseQueryWrapper);
        List<String> courseIds = ftbCultivateLearnTaskCourses.stream()
                .map(FtbCultivateLearnTaskCourse::getCourseId)
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(courseIds)) {
            log.info("任务课程为null,不能触发后续逻辑，参数为:{}", triggerEvent);
            return;
        }

        boolean isStartStudyCourse = super.checkCourseStudyStatus(triggerEvent.getUserId(), courseIds);
        if(isStartStudyCourse==false){
            log.info("未开始学习");
            return;
        }

        int needStudyNum = 0;
        LambdaQueryWrapper<FtbCultivateCourse> courseWraper = Wrappers.lambdaQuery();
        courseWraper.in(FtbCultivateCourse::getId, courseIds);
        courseWraper.eq(FtbCultivateCourse::getEnableMark, 0);
        courseWraper.eq(FtbCultivateCourse::getIsGroundIng, 1);//上下架（0下架，1上架）
        List<FtbCultivateCourse> ftbCultivateCourses = ftbCultivateCourseMapper.selectList(courseWraper);
        if(CollUtil.isEmpty(ftbCultivateCourses)){
            needStudyNum=0;
        }else{
            needStudyNum= ftbCultivateCourses.size();
            courseIds = ftbCultivateCourses.stream()
                    .map(FtbCultivateCourse::getId)
                    .collect(Collectors.toList());
        }

        if (needStudyNum==0 || super.hasTheCourseBeenCompleted(triggerEvent.getUserId(), courseIds)) {
            SpringContext.getApplicationContext()
                    .publishEvent(new JnpfApplicationEvent<>(
                            ExamIdentifyTriggerEventDTO.builder()
                                    .taskId(triggerEvent.getTaskId())
                                    .userId(triggerEvent.getUserId())
                                    .triggerEventType(TriggerEventBO.TriggerEventType.TASK)
                                    .build()));
        }
    }

    @Override
    public void courseLearnCompleteHandle(TaskCourseProcessDTO baseData) {
        // ignore 单个课程学完不触发任何任务操作
    }

}
