package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.CourseScheduleReview;
import com.ym.mec.biz.dal.page.CourseReviewQueryInfo;
import com.ym.mec.biz.service.CourseReviewService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@RequestMapping("courseReview")
@Api(tags = "课程评价")
@RestController
public class CourseReviewController extends BaseController {

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private CourseReviewService courseReviewService;


    @ApiOperation(value = "网管课评论列表")
    @GetMapping("getPracticeGroup")
    public HttpResponseResult getPracticeGroup(CourseReviewQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        queryInfo.setTeacherId(sysUser.getId());
        return succeed(courseReviewService.findPracticeGroupReviews(queryInfo));
    }


    @ApiOperation(value = "提交评论")
    @PostMapping(value = "/add")
    public HttpResponseResult add(CourseScheduleReview courseScheduleReview) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(sysUser)) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        courseScheduleReview.setTeacherId(sysUser.getId());
        return succeed(courseReviewService.addPracticeGroupReview(courseScheduleReview));
    }

    @ApiOperation(value = "网管课评论详情")
    @GetMapping("getReviewInfo")
    public HttpResponseResult getReviewInfo(int id) {
        return succeed(courseReviewService.getReviewInfo(id));
    }

    @ApiOperation(value = "获取课程头部信息")
    @GetMapping("getCourseInfoHead")
    public HttpResponseResult getCourseInfoHead(int courseId) {
        return succeed(courseReviewService.getCourseInfoHead(courseId));
    }

    @ApiOperation(value = "修改评论信息")
    @PostMapping("updateReviewInfo")
    public HttpResponseResult updateReviewInfo(CourseScheduleReview courseScheduleReview) {
        return succeed(courseReviewService.updateReview(courseScheduleReview));
    }

    @ApiOperation(value = "获取班级结束课程评价情况")
    @GetMapping("getClassOverCourse")
    public HttpResponseResult getClassOverCourse(Long classGroupId) {
        return succeed(courseReviewService.getClassOverCourse(classGroupId));
    }

    @ApiOperation(value = "获取老师评价和学生评价")
    @GetMapping("getStuAndTeaReview")
    public HttpResponseResult getStuAndTeaReview(Long courseId) {
        return succeed(courseReviewService.getStuAndTeaReview(courseId));
    }

    @ApiOperation(value = "批量添加评价")
    @PostMapping("batchAdd")
    public HttpResponseResult batchAdd(@RequestBody List<CourseScheduleReview> courseScheduleReviews) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(sysUser)) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(courseReviewService.batchAdd(sysUser.getId(),courseScheduleReviews));
    }

    @ApiOperation(value = "线上课评论列表")
    @GetMapping("getOnlineCourse")
    public HttpResponseResult getOnlineCourse(CourseReviewQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        queryInfo.setTeacherId(sysUser.getId());
        return succeed(courseReviewService.findOnlineCourseReviews(queryInfo));
    }

    @ApiOperation(value = "布置线上课作业")
    @PostMapping(value = "/addHomeWork")
    @ApiImplicitParams({ @ApiImplicitParam(name = "courseId", value = "课程编号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "content", value = "作业内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "expiryDate", value = "作业截止日期", required = true, dataType = "Date")})
    public HttpResponseResult addHomeWork(Integer courseId, String content, Date expiryDate) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(sysUser)) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(courseReviewService.addHomeWork(courseId,content,expiryDate));
    }

}
