package org.xyzh.common.vo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.xyzh.common.dto.BaseDTO;
import org.xyzh.common.dto.study.TbCourse;
import org.xyzh.common.dto.study.TbCourseChapter;
import org.xyzh.common.dto.study.TbCourseNode;
import org.xyzh.common.dto.study.TbLearningRecord;

/**
 * @description 课程项目VO - 包含课程和学习记录的关键信息
 * @filename CourseItemVO.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-28
 */
public class CourseItemVO extends BaseDTO {
    
    private static final long serialVersionUID = 1L;
    
    // ========== 课程基本信息 ==========
    /**
     * @description 课程ID
     */
    private String courseID;
    
    /**
     * @description 课程名称
     */
    private String name;
    
    /**
     * @description 课程封面图片
     */
    private String coverImage;
    
    /**
     * @description 课程描述
     */
    private String description;
    
    /**
     * @description 课程时长（分钟）
     */
    private Integer duration;
    
    /**
     * @description 授课老师
     */
    private String teacher;
    
    /**
     * @description 课程状态（0未上线 1已上线 2已下架）
     */
    private Integer status;
    
    /**
     * @description 浏览次数
     */
    private Integer viewCount;
    
    /**
     * @description 学习人数
     */
    private Integer learnCount;
    
    /**
     * @description 课程创建时间
     */
    private Date createTime;
    
    // ========== 学习记录信息 ==========
    /**
     * @description 学习记录ID
     */
    private String recordID;
    
    /**
     * @description 学习进度（0-100）
     */
    private BigDecimal progress;
    
    /**
     * @description 是否完成
     */
    private Boolean isComplete;
    
    /**
     * @description 学习时长（秒）
     */
    private Integer learningDuration;
    
    /**
     * @description 最后学习时间
     */
    private Date lastLearnTime;
    
    /**
     * @description 完成时间
     */
    private Date completeTime;
    
    // ========== 章节节点信息 ==========
    /**
     * @description 章节ID（当此对象代表章节或节点时使用）
     */
    private String chapterID;
    
    /**
     * @description 节点ID（当此对象代表节点时使用）
     */
    private String nodeID;
    
    /**
     * @description 父级ID（章节的父章节ID）
     */
    private String parentID;
    
    /**
     * @description 节点类型（1视频 2文档 3音频 4图片 5链接）
     */
    private Integer nodeType;
    
    /**
     * @description 节点内容（富文本内容）
     */
    private String content;
    
    /**
     * @description 视频URL
     */
    private String videoUrl;
    
    /**
     * @description 资源ID
     */
    private String resourceID;
    
    /**
     * @description 排序号
     */
    private Integer orderNum;
    
    /**
     * @description 是否必修（1必修 0选修）
     */
    private Integer isRequired;
    
    // ========== 层级结构 ==========
    /**
     * @description 章节列表（课程的章节列表）
     */
    private List<CourseItemVO> chapters;
    
    /**
     * @description 章节节点映射（key: chapterID, value: 该章节下的节点列表）
     */
    private Map<String, List<CourseItemVO>> chapterNodes;

    // ========== Getters and Setters ==========
    
    public String getCourseID() {
        return courseID;
    }

    public void setCourseID(String courseID) {
        this.courseID = courseID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCoverImage() {
        return coverImage;
    }

    public void setCoverImage(String coverImage) {
        this.coverImage = coverImage;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getDuration() {
        return duration;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    public String getTeacher() {
        return teacher;
    }

    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getViewCount() {
        return viewCount;
    }

    public void setViewCount(Integer viewCount) {
        this.viewCount = viewCount;
    }

    public Integer getLearnCount() {
        return learnCount;
    }

    public void setLearnCount(Integer learnCount) {
        this.learnCount = learnCount;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getRecordID() {
        return recordID;
    }

    public void setRecordID(String recordID) {
        this.recordID = recordID;
    }

    public BigDecimal getProgress() {
        return progress;
    }

    public void setProgress(BigDecimal progress) {
        this.progress = progress;
    }

    public Boolean getIsComplete() {
        return isComplete;
    }

    public void setIsComplete(Boolean isComplete) {
        this.isComplete = isComplete;
    }

    public Integer getLearningDuration() {
        return learningDuration;
    }

    public void setLearningDuration(Integer learningDuration) {
        this.learningDuration = learningDuration;
    }

    public Date getLastLearnTime() {
        return lastLearnTime;
    }

    public void setLastLearnTime(Date lastLearnTime) {
        this.lastLearnTime = lastLearnTime;
    }

    public Date getCompleteTime() {
        return completeTime;
    }

    public void setCompleteTime(Date completeTime) {
        this.completeTime = completeTime;
    }

    public String getChapterID() {
        return chapterID;
    }

    public void setChapterID(String chapterID) {
        this.chapterID = chapterID;
    }

    public String getNodeID() {
        return nodeID;
    }

    public void setNodeID(String nodeID) {
        this.nodeID = nodeID;
    }

    public String getParentID() {
        return parentID;
    }

    public void setParentID(String parentID) {
        this.parentID = parentID;
    }

    public Integer getNodeType() {
        return nodeType;
    }

    public void setNodeType(Integer nodeType) {
        this.nodeType = nodeType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getVideoUrl() {
        return videoUrl;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }

    public String getResourceID() {
        return resourceID;
    }

    public void setResourceID(String resourceID) {
        this.resourceID = resourceID;
    }

    public Integer getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(Integer orderNum) {
        this.orderNum = orderNum;
    }

    public Integer getIsRequired() {
        return isRequired;
    }

    public void setIsRequired(Integer isRequired) {
        this.isRequired = isRequired;
    }

    public List<CourseItemVO> getChapters() {
        return chapters;
    }

    public void setChapters(List<CourseItemVO> chapters) {
        this.chapters = chapters;
    }

    public Map<String, List<CourseItemVO>> getChapterNodes() {
        return chapterNodes;
    }

    public void setChapterNodes(Map<String, List<CourseItemVO>> chapterNodes) {
        this.chapterNodes = chapterNodes;
    }

    // ========== 类型转换方法 ==========
    
    /**
     * @description 转换为课程实体对象
     * @return TbCourse 课程实体
     */
    public TbCourse toCourse() {
        TbCourse course = new TbCourse();
        course.setID(this.getID());
        course.setCourseID(this.courseID);
        course.setName(this.name);
        course.setCoverImage(this.coverImage);
        course.setDescription(this.description);
        course.setDuration(this.duration);
        course.setTeacher(this.teacher);
        course.setStatus(this.status);
        course.setViewCount(this.viewCount);
        course.setLearnCount(this.learnCount);
        course.setCreateTime(this.createTime);
        return course;
    }
    
    /**
     * @description 从课程实体创建CourseItemVO
     * @param course 课程实体
     * @return CourseItemVO
     */
    public static CourseItemVO fromCourse(TbCourse course) {
        if (course == null) {
            return null;
        }
        
        CourseItemVO vo = new CourseItemVO();
        vo.setID(course.getID());
        vo.setCourseID(course.getCourseID());
        vo.setName(course.getName());
        vo.setCoverImage(course.getCoverImage());
        vo.setDescription(course.getDescription());
        vo.setDuration(course.getDuration());
        vo.setTeacher(course.getTeacher());
        vo.setStatus(course.getStatus());
        vo.setViewCount(course.getViewCount());
        vo.setLearnCount(course.getLearnCount());
        vo.setCreateTime(course.getCreateTime());
        return vo;
    }
    
    /**
     * @description 转换为学习记录实体对象
     * @return TbLearningRecord 学习记录实体
     */
    public TbLearningRecord toLearningRecord() {
        TbLearningRecord record = new TbLearningRecord();
        if (this.recordID != null) {
            // 如果有recordID，说明是从已有记录转换的，需要设置ID
            record.setID(this.recordID);
        }
        record.setCourseID(this.courseID);
        record.setDuration(this.learningDuration);
        record.setProgress(this.progress);
        record.setIsComplete(this.isComplete);
        record.setCompleteTime(this.completeTime);
        record.setLastLearnTime(this.lastLearnTime);
        return record;
    }
    
    /**
     * @description 设置学习记录信息
     * @param record 学习记录实体
     */
    public void setLearningRecordInfo(TbLearningRecord record) {
        if (record == null) {
            return;
        }
        
        this.recordID = record.getID();
        this.progress = record.getProgress();
        this.isComplete = record.getIsComplete();
        this.learningDuration = record.getDuration();
        this.lastLearnTime = record.getLastLearnTime();
        this.completeTime = record.getCompleteTime();
    }
    
    /**
     * @description 从课程和学习记录创建CourseItemVO
     * @param course 课程实体
     * @param record 学习记录实体
     * @return CourseItemVO
     */
    public static CourseItemVO fromCourseAndRecord(TbCourse course, TbLearningRecord record) {
        CourseItemVO vo = fromCourse(course);
        if (vo != null && record != null) {
            vo.setLearningRecordInfo(record);
        }
        return vo;
    }
    
    /**
     * @description 从章节创建CourseItemVO
     * @param chapter 章节实体
     * @return CourseItemVO
     */
    public static CourseItemVO fromChapter(TbCourseChapter chapter) {
        if (chapter == null) {
            return null;
        }
        
        CourseItemVO vo = new CourseItemVO();
        vo.setID(chapter.getID());
        vo.setChapterID(chapter.getChapterID());
        vo.setCourseID(chapter.getCourseID());
        vo.setParentID(chapter.getParentID());
        vo.setName(chapter.getName());
        vo.setDescription(chapter.getContent());
        vo.setDuration(chapter.getDuration());
        vo.setVideoUrl(chapter.getVideoUrl());
        vo.setResourceID(chapter.getResourceID());
        vo.setNodeType(chapter.getChapterType());
        vo.setOrderNum(chapter.getOrderNum());
        vo.setCreateTime(chapter.getCreateTime());
        return vo;
    }
    
    /**
     * @description 从节点创建CourseItemVO
     * @param node 节点实体
     * @return CourseItemVO
     */
    public static CourseItemVO fromNode(TbCourseNode node) {
        if (node == null) {
            return null;
        }
        
        CourseItemVO vo = new CourseItemVO();
        vo.setID(node.getID());
        vo.setNodeID(node.getNodeID());
        vo.setChapterID(node.getChapterID());
        vo.setName(node.getName());
        
        vo.setContent(node.getContent());
        vo.setDuration(node.getDuration());
        vo.setVideoUrl(node.getVideoUrl());
        vo.setResourceID(node.getResourceID());
        vo.setNodeType(node.getNodeType());
        vo.setOrderNum(node.getOrderNum());
        vo.setIsRequired(node.getIsRequired());
        vo.setCreateTime(node.getCreateTime());
        return vo;
    }
    
    /**
     * @description 转换为章节实体对象
     * @return TbCourseChapter 章节实体
     */
    public TbCourseChapter toChapter() {
        TbCourseChapter chapter = new TbCourseChapter();
        chapter.setID(this.getID());
        chapter.setChapterID(this.chapterID);
        chapter.setCourseID(this.courseID);
        chapter.setParentID(this.parentID);
        chapter.setName(this.name);
        chapter.setContent(this.description);
        chapter.setDuration(this.duration);
        chapter.setVideoUrl(this.videoUrl);
        chapter.setResourceID(this.resourceID);
        chapter.setChapterType(this.nodeType);
        chapter.setOrderNum(this.orderNum);
        return chapter;
    }
    
    /**
     * @description 转换为节点实体对象
     * @return TbCourseNode 节点实体
     */
    public TbCourseNode toNode() {
        TbCourseNode node = new TbCourseNode();
        node.setID(this.getID());
        node.setNodeID(this.nodeID);
        node.setChapterID(this.chapterID);
        node.setName(this.name);
        node.setContent(this.content);
        node.setDuration(this.duration);
        node.setVideoUrl(this.videoUrl);
        node.setResourceID(this.resourceID);
        node.setNodeType(this.nodeType);
        node.setOrderNum(this.orderNum);
        node.setIsRequired(this.isRequired);
        return node;
    }

    @Override
    public String toString() {
        return "CourseItemVO{" +
                "courseID='" + courseID + '\'' +
                ", name='" + name + '\'' +
                ", coverImage='" + coverImage + '\'' +
                ", teacher='" + teacher + '\'' +
                ", duration=" + duration +
                ", status=" + status +
                ", progress=" + progress +
                ", isComplete=" + isComplete +
                ", learningDuration=" + learningDuration +
                ", lastLearnTime=" + lastLearnTime +
                ", viewCount=" + viewCount +
                ", learnCount=" + learnCount +
                '}';
    }
}
