package com.fs.app.controller;


import cn.hutool.json.JSONUtil;
import com.fs.app.annotation.Login;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.core.utils.OrderCodeUtils;
import com.fs.course.config.CourseConfig;
import com.fs.course.domain.*;
import com.fs.course.param.*;
import com.fs.course.service.*;
import com.fs.course.service.impl.TencentCloudCosService;
import com.fs.course.vo.*;
import com.fs.his.vo.OptionsVO;
import com.fs.system.service.ISysConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Api("h5课堂接口")
@RestController
@RequestMapping(value="/app/course/h5")
public class CourseH5Controller extends  AppBaseController{
    Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    private IFsUserCourseService courseService;

    @Autowired
    private IFsUserCourseVideoService courseVideoService;

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IFsCourseLinkService courseLinkService;

    @Autowired
    private IFsCourseWatchLogService courseWatchLogService;


    @ApiOperation("h5课程简介")
    @GetMapping("/getH5CourseByVideoId")
    public R getCourseByVideoId(@RequestParam("videoId") Long videoId,HttpServletRequest request)
    {
        FsUserCourseVideoH5VO course = courseService.selectFsUserCourseVideoH5VOByVideoId(videoId);
        return R.ok().put("data",course);
    }

    @ApiOperation("h5课程详情")
    @GetMapping("/getH5CourseVideoDetails")
    public R getCourseVideoDetails(FsUserCourseVideoFinishUParam param)
    {
        String json = configService.selectConfigByKey("course.config");
        CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
        FsUserCourseVideoH5DVO course = courseService.selectFsUserCourseVideoH5DVOByVideoId(param.getVideoId());

        Long duration = 0L;
        long tipsTime = 0L;
        int isFinish = 0;
        if (param.getLinkType()!=null&&param.getLinkType()==1){
            return R.ok().put("course",course).put("config",config).put("playDuration",duration).put("tipsTime",tipsTime);
        }
        // 从Redis中获取观看时长
        String redisKey = "h5user:watch:duration:" + param.getQwUserId()+ ":" + param.getQwExternalId() + ":" + param.getVideoId();
        String durationStr = redisCache.getCacheObject(redisKey);
        FsCourseWatchLog log = courseWatchLogService.getWatchCourseVideoH5(param.getVideoId(),param.getQwUserId(),param.getQwExternalId());
        //redis取不到查库
        if (durationStr != null) {
            duration = Long.parseLong(durationStr);
        }else {
            duration = log.getDuration();
        }

        if (course.getDuration()!=null){
            tipsTime = course.getDuration()/2;
        }
        //判断是否完课
        if (log.getLogType()==2){
           isFinish=1;
        }

        //将视频时长也存到redis
        String videoRedisKey = "h5user:video:duration:" + param.getVideoId();
        Long videoDuration = redisCache.getCacheObject(videoRedisKey);
        if (videoDuration==null){
            redisCache.setCacheObject(videoRedisKey,course.getDuration());
        }

        return R.ok().put("course",course).put("config",config).put("playDuration",duration).put("tipsTime",tipsTime).put("isFinish",isFinish);
    }



    @ApiOperation("获取真实链接")
    @GetMapping("/getRealLink")
    public R getRealLink(@RequestParam("sortLink")String link)
    {
        return courseLinkService.getRealLink(link);
    }


    @ApiOperation("更新看课时长")
    @PostMapping("/updateWatchDuration")
    public R updateWatchDuration(@RequestBody FsUserCourseVideoFinishUParam param)
    {
        return courseVideoService.updateWatchDuration(param);
    }


    @ApiOperation("获取缓冲流量")
    @PostMapping("/getInternetTraffic")
    public R getInternetTraffic(@RequestBody FsUserCourseVideoFinishUParam param) {
        return courseVideoService.getInternetTraffic(param);
    }

    @PostMapping("/getErrMsg")
    public void getErrMsg(@RequestParam("msg") String msg) {
        logger.error("zyp \n【h5看课中途报错】：{}",msg);
    }

}
