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.user.api.entity.ExamRoom;
import com.keao.edu.user.dto.ExamRoomDto;
import com.keao.edu.user.dto.ExamRoomListDto;
import com.keao.edu.user.dto.ExamRoomStatisticsDto;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.entity.Teacher;
import com.keao.edu.user.page.ExamRoomListQueryInfo;
import com.keao.edu.user.page.ExamRoomQueryInfo;
import com.keao.edu.user.service.EmployeeService;
import com.keao.edu.user.service.ExamRoomService;
import com.keao.edu.user.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("examRoom")
@Api(tags = "考场服务")
public class ExamRoomController extends BaseController {

    @Autowired
    private ExamRoomService examRoomService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private TeacherService teacherService;

    @ApiOperation("分页查询监考列表")
    @GetMapping(value = "/list")
    public HttpResponseResult<PageInfo<ExamRoomListDto>> getList(ExamRoomListQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(!sysUser.getIsSuperAdmin() && Objects.isNull(queryInfo.getOrganId())){
            Teacher teacher = teacherService.get(sysUser.getId());
            if(Objects.isNull(teacher)){
                return failed("用户信息异常");
            }
            queryInfo.setOrganId(teacher.getOrganId());
        }
        return succeed(examRoomService.queryExamRoomPage(queryInfo));
    }

    @ApiOperation("发送考试安排")
    @GetMapping(value = "/sendExamPlan")
    public HttpResponseResult sendExamPlan(Integer examId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Employee employee = employeeService.get(sysUser.getId());
        if(Objects.isNull(employee)){
            return failed("用户信息异常");
        }
        return examRoomService.sendExamPlan(employee.getOrganId(), examId, sysUser.getId());
    }

    @ApiOperation("获取考级项目教室列表")
    @GetMapping(value = "/queryExamRooms")
    public HttpResponseResult<PageInfo<ExamRoomDto>> queryExamRooms(ExamRoomQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(!sysUser.getIsSuperAdmin()&&Objects.isNull(queryInfo.getOrganId())){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.isNull(employee)){
                return failed("用户信息异常");
            }
            queryInfo.setOrganId(employee.getOrganId());
        }
        return succeed(examRoomService.queryExamRooms(queryInfo));
    }

    @ApiOperation("更新考场")
    @PostMapping(value = "/update")
    public HttpResponseResult update(@RequestBody ExamRoom examRoom){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Employee employee = employeeService.get(sysUser.getId());
        if(Objects.isNull(employee)){
            return failed("用户信息异常");
        }
        examRoom.setOrganId(employee.getOrganId());
        examRoomService.update(examRoom);
        return succeed();
    }

    @ApiOperation("创建教室")
    @PostMapping(value = "/createExamRoom")
    public HttpResponseResult createExamRoom(@RequestBody ExamRoom examRoom){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Employee employee = employeeService.get(sysUser.getId());
        if(Objects.isNull(employee)){
            return failed("用户信息异常");
        }
        examRoom.setOrganId(employee.getOrganId());
        examRoomService.createExamRoom(examRoom);
        return succeed();
    }

    @ApiOperation("更新教室信息")
    @PostMapping(value = "/updateExamRoom")
    public HttpResponseResult<ExamRoom> updateExamRoom(@RequestBody ExamRoom examRoom){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Employee employee = employeeService.get(sysUser.getId());
        if(Objects.isNull(employee)){
            return failed("用户信息异常");
        }
        examRoom.setOrganId(employee.getOrganId());
        return succeed(examRoomService.updateExamRoom(examRoom));
    }

    @ApiOperation("教室状态变更（关闭教室，开启教室）")
    @PostMapping(value = "/changeExamRoom")
    public HttpResponseResult changeExamRoom(Long examRoomId,Integer openFlag){
        examRoomService.changeExamRoom(examRoomId,openFlag);
        return succeed();
    }

    @ApiOperation("删除教室")
    @PostMapping(value = "/deleteExamRooms")
    public HttpResponseResult deleteExamRooms(String examRoomIds){
        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();
        }
        examRoomService.deleteExamRooms(organId,examRoomIds);
        return succeed();
    }

    @ApiOperation("获取教室")
    @GetMapping(value = "/get")
    public HttpResponseResult<ExamRoom> getExamRoom(Long id){
        return succeed(examRoomService.get(id));
    }

    @ApiOperation("获取教室")
    @PostMapping(value = "/api/get")
    public ExamRoom getApiExamRoom(Long id){
        return examRoomService.get(id);
    }

    @ApiOperation("获取考场统计信息")
    @GetMapping(value = "/getExamRoomStatisticsInfo")
    public HttpResponseResult<ExamRoomStatisticsDto> getExamRoomStatisticsInfo(Long examId){
        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();
        }
        return succeed(examRoomService.getExamRoomStatisticsInfo(organId, examId));
    }

    @ApiOperation("获取报名学员可加入教室列表")
    @GetMapping(value = "/getStudentEnableJoinRoom")
    public HttpResponseResult<List<ExamRoom>> getStudentEnableJoinRoom(Long registId){
        return succeed(examRoomService.getStudentEnableJoinRoom(registId));
    }

    @ApiOperation("强制关闭考场")
    @PostMapping(value = "/forceCloseExamRoom")
    public HttpResponseResult forceCloseExamRoom(Long examRoomId){
        return examRoomService.forceCloseExamRoom(examRoomId);
    }
}
