package com.keao.edu.user.controller;

import com.keao.edu.auth.api.client.SysUserFeignService;
import com.keao.edu.auth.api.entity.SysUser;
import com.keao.edu.common.controller.BaseController;
import com.keao.edu.common.entity.HttpResponseResult;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.im.api.entity.PublishMessageDto;
import com.keao.edu.user.api.entity.ExamRoomStudentRelation;
import com.keao.edu.user.dto.ExamRoomStudentRelationDto;
import com.keao.edu.user.dto.StuRecordDetailDto;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.page.ExamRoomStudentRelationQueryInfo;
import com.keao.edu.user.service.EmployeeService;
import com.keao.edu.user.service.ExamRoomStudentRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("examRoomStudentRelation")
@Api(tags = "考场与学生关联服务")
public class ExamRoomStudentRelationController extends BaseController {

    @Autowired
    private ExamRoomStudentRelationService examRoomStudentRelationService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeService employeeService;

    /*@ApiOperation("开启/关闭教室")
    @GetMapping(value = "/switchClassRoom")
    public HttpResponseResult switchClassRoom(Integer openFlag,Integer examinationBasicId,Integer studentId) {
        examRoomStudentRelationService.switchClassRoom(openFlag,examinationBasicId,studentId);
        return succeed();
    }*/

    @ApiOperation("签到")
    @PostMapping(value = "/signIn")
    public HttpResponseResult signIn(Long examRegistrationId) {
        examRoomStudentRelationService.signIn(examRegistrationId);
        return succeed();
    }

    @ApiOperation("下一位")
    @PostMapping(value = "/nextBit")
    public HttpResponseResult nextBit(Integer examStatus,Long roomId) {
        examRoomStudentRelationService.nextBit(examStatus,roomId);
        return succeed();
    }

    @ApiOperation("开始考试")
    @PostMapping(value = "/actionExam")
    public HttpResponseResult actionExam(Long roomId) {
        examRoomStudentRelationService.actionExam(roomId);
        return succeed();
    }

    @ApiOperation("监考端选择去录播")
    @PostMapping(value = "/webRecorded")
    public HttpResponseResult webRecorded(Long roomId) {
        examRoomStudentRelationService.recorded(roomId);
        return succeed();
    }

    /*@ApiOperation("学生选择去录播")
    @PostMapping(value = "/stuRecorded")
    public HttpResponseResult<NeedCheckingDetailDto> stuRecorded(Long examRegistrationId) {
        return succeed(examRoomStudentRelationService.stuRecorded(examRegistrationId));
    }*/

    @ApiOperation("学生端录播详情页面")
    @GetMapping(value = "/stuRecordDetail")
    public HttpResponseResult<StuRecordDetailDto> stuRecordDetail(Long examRegistrationId) {
        return succeed(examRoomStudentRelationService.stuRecordDetail(examRegistrationId));
    }

    @ApiOperation("学生端完成录播")
    @PostMapping(value = "/stuEndRecord")
    public HttpResponseResult stuEndRecord(Long examRegistrationId,String videoUrl) {
        examRoomStudentRelationService.stuEndRecord(examRegistrationId,videoUrl);
        return succeed();
    }

    /*@ApiOperation("学生选择重新排队")
    @PostMapping(value = "/againQueue")
    public HttpResponseResult<NeedCheckingDetailDto> againQueue(Long examRegistrationId) {
        return succeed(examRoomStudentRelationService.againQueue(examRegistrationId));
    }*/

    @ApiOperation("获取后台考场待考队列")
    @PostMapping(value = "/queryNeedCheckingList")
    public HttpResponseResult queryNeedCheckingList(Long roomId) {
        return succeed(examRoomStudentRelationService.queryNeedCheckingList(roomId));
    }

    @ApiOperation("获取推送消息内容")
    @PostMapping(value = "api/getPublishMessage")
    public PublishMessageDto getPublishMessage(Long examRegistrationId) {
        return examRoomStudentRelationService.getPublishMessage(examRegistrationId);
    }

    @ApiOperation("获取教室学员关联")
    @PostMapping(value = "api/getExamRoomStudentRelation")
    public ExamRoomStudentRelation getExamRoomStudentRelation(Long registrationId) {
        return examRoomStudentRelationService.getExamRoomStudentRelation(registrationId);
    }

    @ApiOperation("给教室分配学员")
    @PostMapping(value = "/addStudentForRoom")
    public HttpResponseResult addStudentForRoom(Long examRoomId, String registIds){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Integer organId=null;
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.isNull(employee)){
                return failed("用户信息异常");
            }
            organId=employee.getOrganId();
        }
        examRoomStudentRelationService.addStudentForRoom(examRoomId, registIds, organId);
        return succeed();
    }

    @ApiOperation("更换学员考场")
    @PostMapping(value = "/changeStudentExamRoom")
    public HttpResponseResult changeStudentExamRoom(Long registId, Long examRoomId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Integer organId=null;
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.isNull(employee)){
                return failed("用户信息异常");
            }
            organId=employee.getOrganId();
        }
        examRoomStudentRelationService.changeStudentExamRoom(registId, examRoomId, organId);
        return succeed();
    }

    @ApiOperation("获取教室学员")
    @GetMapping(value = "/findExamRoomStudents")
    public HttpResponseResult<PageInfo<ExamRoomStudentRelationDto>> findExamRoomStudents(ExamRoomStudentRelationQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.nonNull(employee)){
                queryInfo.setOrganId(employee.getOrganId());
            }
        }
        return succeed(examRoomStudentRelationService.findExamRoomStudents(queryInfo));
    }

    @ApiOperation("删除指定教室学员")
    @PostMapping(value = "/deleteStudentFromRoom")
    public HttpResponseResult deleteStudentFromRoom(Long examRoomId, String registIds){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Integer organId=null;
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.isNull(employee)){
                return failed("用户信息异常");
            }
            organId=employee.getOrganId();
        }
        examRoomStudentRelationService.deleteStudentFromRoom(examRoomId, registIds, organId);
        return succeed();
    }
}
