package jnpf.model.cultivate.v2.course.vo.app;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import jnpf.model.cultivate.po.course.FtbCultivateCourseChapter;
import jnpf.model.cultivate.po.position.FtbCultivatePositionCourceChapterLearning;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Data
public class V2CourseOutlineAppVo {
    /**
     * 课程ID
     */
    private String courseId;

    /**
     * 课程名称
     */
    private String courseName;

    /**
     * 课程亮点
     */
    private String highLights;

    /** 任务封面Url */
    private String coverUrl;
    /**
     * 章节总数
     */
    private Integer chapterTotalNumber;

    /**
     * 已完成数
     */
    private Integer chapterFinishedNumber;

    /**
     * 章节信息
     */
    private List<V2ChapterVo> chapterVoList;

    public void initData() {
        this.chapterTotalNumber = 0;
        this.chapterFinishedNumber = 0;
        this.chapterVoList = new ArrayList<>();
    }

    /**
     * 将章节列表转换为树形结构
     *
     * @param chapterVoList 章节列表
     * @return 树形结构
     */
    private static List<V2ChapterVo> toChapterVoTree(List<V2ChapterVo> chapterVoList) {
        // 使用Map存储所有章节，key为章节ID，value为章节对象
        Map<String, V2ChapterVo> chapterMap = new HashMap<>();
        for (V2ChapterVo chapter : chapterVoList) {
            chapterMap.put(chapter.getId(), chapter);
        }

        // 构建树形结构
        List<V2ChapterVo> rootChapters = new ArrayList<>();
        for (V2ChapterVo chapter : chapterVoList) {
            if (chapter.getParentId() == null || chapter.getParentId().equals("0") || chapter.getParentId().isEmpty()) {
                // 根节点
                rootChapters.add(chapter);
            } else {
                // 非根节点，找到其父节点并添加到children中
                V2ChapterVo parent = chapterMap.get(chapter.getParentId());
                if (parent != null) {
                    if (parent.getChild() == null) {
                        parent.setChild(new ArrayList<>());
                    }
                    parent.getChild().add(chapter);
                }
            }
        }

        return rootChapters;
    }

    /**
     * 将章节列表转换为树形结构
     *
     * @param chapters 章节列表
     * @return 树形结构
     */
    private static List<V2ChapterVo> convertChaptersVo(List<FtbCultivateCourseChapter> chapters, Map<String, FtbCultivatePositionCourceChapterLearning> chapterLearningMap) {
        List<V2ChapterVo> chapterVoList = new ArrayList<>();
        for (FtbCultivateCourseChapter chapter : chapters) {
            V2ChapterVo chapterVo = new V2ChapterVo();
            chapterVo.setId(chapter.getId());
            chapterVo.setChapterName(chapter.getName());
            chapterVo.setSortCode(chapter.getSortCode());
            chapterVo.setChapterType(chapter.getChapterType());
            chapterVo.setParentId(chapter.getParentId());
            chapterVo.setPath(chapter.getPath());
            chapterVo.setVideoActionType(chapter.getVideoActionType());
            chapterVo.setSize(chapter.getSize());
            FtbCultivatePositionCourceChapterLearning chapterLearning = chapterLearningMap.get(chapter.getId());
            if (chapterLearning != null) {
                chapterVo.setChapterTime(chapterLearning.getChapterTime());
                chapterVo.setState(chapterLearning.getState());
            } else {
                chapterVo.setChapterTime(0);
                chapterVo.setState(0);
            }
            chapterVoList.add(chapterVo);
        }
        return chapterVoList;
    }


    /**
     * 构建树形结构
     *
     * @param chapters 章节列表
     * @return 树形结构
     */
    public static List<V2ChapterVo> buildChapterVoTree(List<FtbCultivateCourseChapter> chapters, Map<String, FtbCultivatePositionCourceChapterLearning> chapterLearningMap) {
        return toChapterVoTree(convertChaptersVo(chapters, chapterLearningMap));
    }

    /**
     * 计算已完成章节数
     *
     * @param chapterLearningMap 章节学习记录Map
     * @return 已完成章节数
     */
    public static Integer calCompleteChapterNumber(Map<String, FtbCultivatePositionCourceChapterLearning> chapterLearningMap) {
        if (CollUtil.isEmpty(chapterLearningMap)) {
            return 0;
        }
        return (int) chapterLearningMap.values().stream()
                .filter(chapterLearning -> chapterLearning.getState() == 1)
                .count();
    }
}
