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.core.domain.ResponseResult;
import com.fs.course.config.CourseConfig;
import com.fs.course.domain.FsCourseWatchLog;
import com.fs.course.param.FsCourseQuestionAnswerUParam;
import com.fs.course.param.FsCourseSendRewardUParam;
import com.fs.course.param.FsUserCourseVideoFinishUParam;
import com.fs.course.param.newfs.FsUserCourseAddCompanyUserParam;
import com.fs.course.param.newfs.FsUserCourseBeMemberParam;
import com.fs.course.param.newfs.FsUserCourseVideoLinkParam;
import com.fs.course.param.newfs.FsUserCourseVideoUParam;
import com.fs.course.service.*;
import com.fs.course.vo.FsUserCourseVideoH5VO;
import com.fs.course.vo.newfs.FsUserCourseVideoDetailsVO;
import com.fs.course.vo.newfs.FsUserCourseVideoLinkDetailsVO;
import com.fs.store.service.IFsUserService;
import com.fs.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api("会员-h5看课接口")
@RestController
@RequestMapping(value = "/app/course/wx/h5")
public class CourseWxH5Controller 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;

    @Autowired
    private IFsCourseQuestionBankService questionBankService;

    @Autowired
    private IFsUserService fsUserService;


    @Login
    @ApiOperation("判断是否添加客服（是否关联销售）")
    @PostMapping("/isAddKf")
    public ResponseResult<Boolean> isAddCompanyUser(@Valid @RequestBody FsUserCourseAddCompanyUserParam param) {
        Long userId = Long.parseLong(getUserId());
        param.setUserId(userId);
        return courseVideoService.isAddCompanyUser(param);
    }

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

    @Login
    @ApiOperation("H5课程详情")
    @GetMapping("/videoDetails")
    public ResponseResult<FsUserCourseVideoLinkDetailsVO> getCourseVideoDetails(FsUserCourseVideoLinkParam param) {
        param.setFsUserId(Long.parseLong(getUserId()));
        return courseVideoService.getLinkCourseVideoDetails(param);
    }

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

    @ApiOperation("更新看课时长")
    @PostMapping("/updateWatchDuration")
    public R updateWatchDuration(@RequestBody FsUserCourseVideoUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return courseVideoService.updateWatchDurationWx(param);
    }


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


    @ApiOperation("答题")
    @PostMapping("/courseAnswer")
    public R courseAnswer(@RequestBody FsCourseQuestionAnswerUParam param){
        param.setUserId(Long.parseLong(getUserId()));
        logger.info("zyp \n【答题】：{}",param.getQuestions());
        if (param.getDuration()==null){
            logger.info("zyp \n【未识别到时长】：{}",param.getUserId());
        }
        return questionBankService.courseAnswer(param, true);
    }

    @ApiOperation("发放奖励")
    @PostMapping("/sendReward")
    @RepeatSubmit
    public R sendReward(@RequestBody FsCourseSendRewardUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        logger.info("zyp \n【发放奖励】：{}",param);
        return courseVideoService.sendRewardByFsUser(param);
    }


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


}
