package cn.iocoder.byzs.module.web.service.aiCourse;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.byzs.framework.common.enums.CommonStatusEnum;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourse.vo.AiCoursePageReqVO;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseConfig.vo.AiCourseConfigRespVO;
import cn.iocoder.byzs.module.aicourse.service.aiCourse.AiCourseService;
import cn.iocoder.byzs.module.aicourse.service.aiCourseConfig.AiCourseConfigService;
import cn.iocoder.byzs.module.web.controller.admin.aiCourse.vo.WebAiCourseTypeVO;
import cn.iocoder.byzs.module.web.controller.admin.aiCourse.vo.WebAiCourseVO;
import cn.iocoder.byzs.module.web.controller.admin.aiCourseProgress.vo.WebAiCourseProgressDO;
import cn.iocoder.byzs.module.web.dal.mysql.aiCourse.WebAiCourseMapper;
import cn.iocoder.byzs.module.web.service.aiCourseProgress.WebAiCourseProgressServiceImpl;
import cn.iocoder.byzs.module.web.service.role.WebAuthRoleServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

/**
 * web课程 Service 实现类
 *
 * @author lyb
 */
@Service
@Validated
public class WebAiCourseServiceImpl {

    @Resource
    private AiCourseService aiCourseService;
    @Resource
    private AiCourseConfigService aiCourseConfigService;
    @Resource
    private WebAiCourseMapper webAiCourseMapper;
    @Resource
    private WebAiCourseProgressServiceImpl webAiCourseProgressService;
    @Resource
    private WebAuthRoleServiceImpl webAuthRoleService;



    /**
     * 根据课程类型id查询课程列表
     *
     * @param typeId 课程类型id
     * @return 课程列表
     */
    public List<WebAiCourseVO> getAiCourseVoByTypeId(Long typeId) {
        List<AiCoursePageReqVO> aiCourseList = aiCourseService.getAiCourseList(new AiCoursePageReqVO().setAcType(typeId).setAcStatus(String.valueOf(CommonStatusEnum.ENABLE.getStatus())));

        //填充课程配置
        List<Long> idList = aiCourseList.stream()
                .map(AiCoursePageReqVO::getId)
                .collect(Collectors.toList());
        List<AiCourseConfigRespVO> aiCourseConfigQuestionList = aiCourseConfigService.getAiCourseConfigQuestionByCourseIds(idList);
        Map<Long, List<AiCourseConfigRespVO>> aiCourseConfigQuestionMap = aiCourseConfigQuestionList.stream().collect(Collectors.groupingBy(AiCourseConfigRespVO::getCcCourseId));

        //课程小节进度
        List<WebAiCourseProgressDO> progressList = webAiCourseProgressService.getAiCourseProgressByTypeId(typeId);
        Map<Long, WebAiCourseProgressDO> progressMap = progressList.stream().collect(Collectors.toMap(WebAiCourseProgressDO::getArpCourseId, a -> a));


        //重新组装
        List<WebAiCourseVO> aiCourseVoList = new ArrayList<>();
        for (AiCoursePageReqVO aiCourse : aiCourseList) {

            //组装试题配置
            List<AiCourseConfigRespVO> aiCourseConfigQuestion = aiCourseConfigQuestionMap.get(aiCourse.getId());
            List<AiCourseConfigRespVO> list = new ArrayList<>();

            WebAiCourseVO webAiCourseVO = BeanUtils.toBean(aiCourse, WebAiCourseVO.class);
            if (CollUtil.isNotEmpty(aiCourseConfigQuestion)) {
                // 根据 ccTime 升序排序（弹框时间）
                list = aiCourseConfigQuestion.stream().sorted(Comparator.comparing(AiCourseConfigRespVO::getCcTime)).toList();
            }
            webAiCourseVO.setAiCourseConfigList(list);

            //填充进度
            if (progressMap.containsKey(aiCourse.getId())) {
                WebAiCourseProgressDO progress = progressMap.get(aiCourse.getId());
                webAiCourseVO.setProgress(progress.getArpProgress().intValue());
            }else{
                webAiCourseVO.setProgress(0);
            }

            aiCourseVoList.add(webAiCourseVO);
        }

        return aiCourseVoList;
    }

    /**
     * 根据课程类型查询课程类型列表
     * @param themeId
     * @return
     */
    public List<WebAiCourseTypeVO> getTypeByThemeId(Long themeId) {

        //查询课程类型列表
        List<WebAiCourseTypeVO> list = webAiCourseMapper.getTypeByThemeId(new WebAiCourseTypeVO().setCtParentId(themeId));
        List<WebAiCourseTypeVO> aiCourseTypeVoList = BeanUtils.toBean(list, WebAiCourseTypeVO.class);

        //课程小节进度
        List<WebAiCourseProgressDO> progressList = webAiCourseProgressService.getAiCourseProgressByThemeId(themeId);
        Map<Long, WebAiCourseProgressDO> progressMap = progressList.stream().collect(Collectors.toMap(WebAiCourseProgressDO::getArpCtId, a -> a));

        //获得aiCourse数据权限
        Set<Long> aiCourseAuthRoleVO = webAuthRoleService.getAiCourseAuthRoleVO();

        aiCourseTypeVoList.forEach(aiCourseType -> {

            //判断是否有数据权限
            if (aiCourseAuthRoleVO.isEmpty()) {
                aiCourseType.setDataReadonly(false);
            }else aiCourseType.setDataReadonly(!aiCourseAuthRoleVO.contains(aiCourseType.getId()));

            if (progressMap.containsKey(aiCourseType.getId())) {
                WebAiCourseProgressDO progress = progressMap.get(aiCourseType.getId());
                Double arpProgress = progress.getArpProgress();
                //向下取整
                double progressL = Math.floor(arpProgress / aiCourseType.getSectionCount() * 5);
                aiCourseType.setProgress((int) progressL);
            }else{
                aiCourseType.setProgress(0);
            }
        });

        return aiCourseTypeVoList;
    }
}