package com.fs.app.controller.course;



import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fs.app.annotation.UserOperationLog;
import com.fs.app.controller.AppBaseController;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.core.domain.ResponseResult;
import com.fs.app.annotation.Login;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.utils.CloudHostUtils;
import com.fs.common.utils.SecurityUtils;
import com.fs.course.dto.BatchSendCourseDTO;
import com.fs.course.param.*;
import com.fs.course.param.newfs.FsUserCourseAddCompanyUserParam;
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.FsUserCourseVideoLinkDetailsVO;
import com.fs.his.domain.FsUser;
import com.fs.his.enums.FsUserOperationEnum;
import com.fs.im.dto.OpenImResponseDTO;
import com.fs.im.service.OpenIMService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

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

    @Autowired
    private IFsUserCourseVideoService courseVideoService;


    @Autowired
    private IFsCourseLinkService courseLinkService;


    @Autowired
    private IFsCourseQuestionBankService questionBankService;



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

    /**
     * 绑定关系
     *
     * @param param
     * @return
     */
    @PostMapping("/isSaveKf")
    public R isSaveKf(@RequestBody FsUserCourseVideoAddKfUParam param){
        Long userId = Long.parseLong(getUserId());
        param.setUserId(userId);
        return courseVideoService.isSaveKf(param);
    }


    @ApiOperation("h5课程简介")
    @GetMapping("/getH5CourseByVideoId")
    public R getCourseByVideoId(@RequestParam("videoId") Long videoId)
    {
        FsUserCourseVideoH5VO course = courseService.selectFsUserCourseVideoH5VOByVideoId(videoId);
        if (CloudHostUtils.hasCloudHostName("金牛明医","康年堂")){
            course.setCourseName("");
        }
        return R.ok().put("data",course);
    }

    @Login
    @ApiOperation("H5课程详情")
    @GetMapping("/videoDetails")
    @UserOperationLog(operationType = FsUserOperationEnum.STUDY)
    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")
    @Login
    public R updateWatchDuration(@RequestBody FsUserCourseVideoUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return courseVideoService.updateWatchDurationWx(param);
    }


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


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

    @ApiOperation("发放奖励")
    @PostMapping("/sendReward")
    @UserOperationLog(operationType = FsUserOperationEnum.SENDREWARD)
    @RepeatSubmit
    public R sendReward(@RequestBody FsCourseSendRewardUParam param)
    {
        if (ObjectUtil.isEmpty(param.getUserId())){
            Long userId = Long.parseLong(getUserId());
            param.setUserId(userId);
        }
        logger.info("zyp \n【发放奖励】2：{}",param);
        return courseVideoService.sendRewardByFsUser(param);
    }


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

}
