package com.hc.web;

import com.hc.business.dto.CourseVideoDto;
import com.hc.business.dto.UserCourseDto;
import com.hc.business.model.Course;
import com.hc.business.model.CourseChapter;
import com.hc.business.model.CourseChapterVideo;
import com.hc.business.model.UserVideoPosition;
import com.hc.business.mapper.CourseChapterVideoMapper;
import com.hc.business.mapper.UserCourseMapper;
import com.hc.business.service.CourseChapterService;
import com.hc.business.service.CourseService;
import com.hc.business.service.UserVideoPositionService;
import com.hc.core.utils.redis.RedisUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;


@SpringBootTest
class CourseServiceTests {

    @Autowired
    CourseChapterService courseChapterService;

    @Autowired
    CourseService courseService;

    @Autowired
    private CourseChapterVideoMapper courseChapterVideoMapper;

    @Autowired
    private UserCourseMapper userCourseMapper;

    @Autowired
    UserVideoPositionService userVideoPositionService;

    private static final String REDIS_KEY_PREFIX = "user:video:position:";

    @Autowired
    RedisUtil redisUtil;


    private Integer _video_id = null;

    private void clean_redis(UserVideoPosition userVideoPosition, Integer courseid){
        //初始化清理掉缓存
        String key = REDIS_KEY_PREFIX + userVideoPosition.getUserId() + ":" + courseid;
        if(redisUtil.hasKey(key)){
            redisUtil.del(key);
        }
    }

    private UserVideoPosition new_position(String videotime, String position, Integer videoid){
        UserVideoPosition userVideoPosition = new UserVideoPosition();
        userVideoPosition.setVideoId(videoid);
        userVideoPosition.setUserId(100);
        userVideoPosition.setVideoPosition(position);
        userVideoPosition.setVideoTime(videotime);
        return  userVideoPosition;
    }


    /*
    插入图书和章节视频，继续教育不区分章节，课程下直接是多个视频
     */

    private Course insert_new_course_and_chapter_video(){
        //外键，首先要插入图书
        Course course = new Course();
        course.setCourseLogo("test");
        course.setCourseHours(Float.valueOf(11));
        course.setCourseName("test");
        course.setCourseTeacher("william");
        course.setCourseDesc("test");
        courseService.insert(course);
//
//        //先插入章节.不要根节点
//        CourseChapter parent = new CourseChapter();
//        parent.setParentId(0);
//        parent.setChapterName("1");
//        parent.setChapterLevel(0);
//        parent.setCourseId(course.getCourseId());
//        courseChapterService.insert(parent);
//        int parent_id  = parent.getCourseChapterId();

        //插入第一章
        CourseChapter child_1 = new CourseChapter();
        child_1.setChapterName("2");
        child_1.setChapterLevel(1);
        child_1.setParentId(0);
        child_1.setCourseId(course.getCourseId());
        courseChapterService.insert(child_1);
        //插入第一章视频
        CourseChapterVideo courseChapterVideo = new CourseChapterVideo();
        courseChapterVideo.setChapterId(child_1.getCourseChapterId());
        courseChapterVideo.setVideoName("1");
        courseChapterVideo.setVideoUrl("1");
        courseChapterVideo.setVideoOriUrl("1");
        courseChapterVideo.setCourseId(course.getCourseId());
        courseChapterVideo.setIsFree(0);
        courseChapterVideo.setFreeTime(10);
        courseChapterVideoMapper.insert(courseChapterVideo);

        //插入用户得视频进度
        UserVideoPosition userVideoPosition = new_position("100","20",courseChapterVideo.getVideoId());
        clean_redis(userVideoPosition,course.getCourseId());
        userVideoPositionService.insertPositionToRedis(userVideoPosition,course.getCourseId());
//
        //插入第二章
        CourseChapter child_2 = new CourseChapter();
        child_2.setChapterName("3");
        child_2.setParentId(0);
        child_2.setChapterLevel(1);
        child_2.setCourseId(course.getCourseId());
        courseChapterService.insert(child_2);
        //插入第二章视频
        courseChapterVideo = new CourseChapterVideo();
        courseChapterVideo.setChapterId(child_2.getCourseChapterId());
        courseChapterVideo.setVideoName("2");
        courseChapterVideo.setVideoUrl("2");
        courseChapterVideo.setVideoOriUrl("2");
        courseChapterVideo.setCourseId(course.getCourseId());
        courseChapterVideo.setIsFree(0);
        courseChapterVideo.setFreeTime(10);
        courseChapterVideoMapper.insert(courseChapterVideo);
        //插入用户得视频进度
        userVideoPosition = new_position("100","20",courseChapterVideo.getVideoId());
        userVideoPositionService.insertPositionToRedis(userVideoPosition,course.getCourseId());
        _video_id = courseChapterVideo.getVideoId();
        return course;
    }

    /*
    测试章节进度统计
     */

//    @Test
//    void test_course_chapter_video(){
//
//        Course course = insert_new_course_and_chapter_video();
//        assert (null != course);
//
//        Integer user_id = 100;
//        Integer class_id = 100;
//        List<CourseVideoDto> list = courseService.getCourseChapterAndVideoPosition(course.getCourseId(),user_id,class_id);
//        //包含两个章节得进度信息
//        assert ((null != list) && (list.size() == 2));
//        //测试课程总体进度，因该是0.2
//        List<UserCourseDto> cardCourseList = userCourseMapper.selectUserCourseProgress(user_id,class_id,course.getCourseId());
//        assert ((null != cardCourseList) && (cardCourseList.size() == 1));
//        assert (cardCourseList.get(0).getCoursePercent() == 0.2);
//    }

    /*
    测试进度更新
     */

    @Test
    void test_course_chapter_video_update(){
        Course course = insert_new_course_and_chapter_video();
        assert (null != course);

        Integer user_id = 100;
        Integer class_id = 100;
        List<CourseVideoDto> list = courseService.getCourseChapterAndVideoPosition(course.getCourseId(),user_id,class_id);
        //包含两个章节得进度信息
        assert ((null != list) && (list.size() == 2));
        //测试课程总体进度，因该是0.2
        List<UserCourseDto> cardCourseList = userCourseMapper.selectUserCourseProgress(user_id,class_id,course.getCourseId());
        assert ((null != cardCourseList) && (cardCourseList.size() == 1));
        assert (cardCourseList.get(0).getCoursePercent() == 0.2);

        //模拟用户观看视频
        UserVideoPosition userVideoPosition = new_position("100","40",_video_id);
        userVideoPositionService.insertPositionToRedis(userVideoPosition,course.getCourseId());

        //触发重新计算
       courseService.getCourseChapterAndVideoPosition(course.getCourseId(),user_id,class_id);

        cardCourseList = userCourseMapper.selectUserCourseProgress(user_id,class_id,course.getCourseId());
        assert ((null != cardCourseList) && (cardCourseList.size() == 1));
        assert (cardCourseList.get(0).getCoursePercent() == 0.3);

        //模拟用户观看视频
        userVideoPosition = new_position("100","40",_video_id);
        userVideoPositionService.insertPositionToRedis(userVideoPosition,course.getCourseId());

        //触发重新计算
       courseService.getCourseChapterAndVideoPosition(course.getCourseId(),user_id,class_id);

        cardCourseList = userCourseMapper.selectUserCourseProgress(user_id,class_id,course.getCourseId());
        assert ((null != cardCourseList) && (cardCourseList.size() == 1));
        assert (cardCourseList.get(0).getCoursePercent() == 0.3);

    }

    /*
    测试update 标记位
     */

//    @Test
//    void test_update_flag(){
//        Course course = insert_new_course_and_chapter_video();
//        assert (null != course);
//        Integer user_id = 100;
//        Integer class_id = 100;
//
//        List<CourseVideoDto> list = courseService.getCourseChapterAndVideoPosition(course.getCourseId(),user_id,class_id);
//        //包含两个章节得进度信息
//        assert ((null != list) && (list.size() == 2));
//        //测试课程总体进度，因该是0.2
//        List<UserCourseDto> cardCourseList = userCourseMapper.selectUserCourseProgress(user_id,class_id,course.getCourseId());
//        assert ((null != cardCourseList) && (cardCourseList.size() == 1));
//        assert (cardCourseList.get(0).getCoursePercent() == 0.2);
//
//        //模拟用户观看视频
//        UserVideoPosition userVideoPosition = new_position("100","40",_video_id);
//        userVideoPositionService.insertPositionToRedis( userVideoPosition,course.getCourseId());
//        //update 标记为因该存在
//        String key = REDIS_KEY_PREFIX + userVideoPosition.getUserId() + ":" + _video_id;
//        assert (true == redisUtil.hashHasKey(key,"update"+_video_id));
//
//        //触发重新计算,update标记位被i删除
//        courseService.getCourseChapterAndVideoPosition(course.getCourseId(),user_id,class_id);
//        assert (false == redisUtil.hashHasKey(key,"update"+_video_id));
//
//        cardCourseList = userCourseMapper.selectUserCourseProgress(user_id,class_id,course.getCourseId());
//        assert ((null != cardCourseList) && (cardCourseList.size() == 1));
//        assert (cardCourseList.get(0).getCoursePercent() == 0.3);
//
//        //再次模拟观看
//        //模拟用户观看视频
//        userVideoPosition = new_position("100","60",_video_id);
//        userVideoPositionService.insertPositionToRedis(userVideoPosition,course.getCourseId());
//        //update 标记为因该存在
//        assert (true == redisUtil.hashHasKey(key,"update"+_video_id));
//
//        //触发重新计算,update标记位被i删除
//        courseService.getCourseChapterAndVideoPosition(course.getCourseId(),user_id,class_id);
//        assert (false == redisUtil.hashHasKey(key,"update"+_video_id));
//
//        cardCourseList = userCourseMapper.selectUserCourseProgress(user_id,class_id,course.getCourseId());
//        assert ((null != cardCourseList) && (cardCourseList.size() == 1));
//        assert (cardCourseList.get(0).getCoursePercent() == 0.4);
//    }




}
