package com.qmth.themis.exam.config;

import com.qmth.themis.business.cache.ExamRecordCacheUtil;
import com.qmth.themis.business.cache.bean.ExamStudentCacheBean;
import com.qmth.themis.business.constant.SpringContextHolder;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dto.MqDto;
import com.qmth.themis.business.entity.TOeExamRecord;
import com.qmth.themis.business.enums.MonitorStatusSourceEnum;
import com.qmth.themis.business.enums.MonitorVideoSourceEnum;
import com.qmth.themis.business.enums.MqTagEnum;
import com.qmth.themis.business.service.MqDtoService;
import com.qmth.themis.business.service.TEExamStudentService;
import com.qmth.themis.business.service.TOeExamRecordService;
import com.qmth.themis.business.util.MqUtil;
import com.qmth.themis.common.enums.ExceptionResultEnum;
import com.qmth.themis.common.exception.BusinessException;

import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @Description: 考试系统常量
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/9/21
 */
public class ExamConstant {

    /**
     * 发送考试结束msg
     *
     * @param recordId
     * @param clientStop
     */
    public static void sendExamStopMsg(Long recordId, boolean clientStop, boolean mobileStop) throws NoSuchAlgorithmException {
        MqUtil mqUtil = SpringContextHolder.getBean(MqUtil.class);
        MqDtoService mqDtoService = SpringContextHolder.getBean(MqDtoService.class);
//        TEExamStudentService teExamStudentService = SpringContextHolder.getBean(TEExamStudentService.class);
//        TOeExamRecordService tOeExamRecordService = SpringContextHolder.getBean(TOeExamRecordService.class);
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put(SystemConstant.RECORD_ID, recordId);
//        mapParameter.put(SystemConstant.REMOVE_WEBSOCKET, true);
        //客户端考试结束
        if (clientStop) {
            MqDto mqDto = new MqDto(mqUtil.getTopic(), MqTagEnum.OE_WEBSOCKET_EXAM_STOP.name(), recordId, MqTagEnum.OE_WEBSOCKET_EXAM_STOP, String.valueOf(recordId), mapParameter, String.valueOf(recordId));
            mqDtoService.assembleSendAsyncOrderMsg(mqDto);
        }
//        else {//结束推流状态
////            SystemConstant.clientMonitorStatusStop(recordId);
//            TOeExamRecordService toeExamRecordService = SpringContextHolder.getBean(TOeExamRecordService.class);
//            toeExamRecordService.sendExamRecordDataSaveMq(recordId, System.currentTimeMillis());
//        }
        //移动端考试结束
        if (mobileStop) {
//            Long examStudentId = ExamRecordCacheUtil.getExamStudentId(recordId);
//            if (Objects.isNull(examStudentId)) {
//                TOeExamRecord tOeExamRecord = tOeExamRecordService.getById(recordId);
//                Optional.ofNullable(tOeExamRecord).orElseThrow(() -> new BusinessException(ExceptionResultEnum.NOT_FOUND_EXAM_RECORD));
//                examStudentId = tOeExamRecord.getExamStudentId();
//            }
//            ExamStudentCacheBean examStudentCacheBean = teExamStudentService.getExamStudentCacheBean(examStudentId);
//            Optional.ofNullable(examStudentCacheBean).orElseThrow(() -> new BusinessException("考生数据为空"));
            MqDto mqDto = new MqDto(mqUtil.getTopic(), MqTagEnum.EXAM_STOP.name(), recordId, MqTagEnum.EXAM_STOP, String.valueOf(recordId), mapParameter, String.valueOf(recordId));
            mqDtoService.assembleSendAsyncOrderMsg(mqDto);
//            SystemConstant.mobileMonitorStatusStop(examStudentCacheBean.getStudentId(), recordId, false);
        }
    }

    /**
     * 发送考试开始msg
     *
     * @param recordId
     */
    public static void sendExamStartMsg(Long recordId) {
        MqUtil mqUtil = SpringContextHolder.getBean(MqUtil.class);
        MqDtoService mqDtoService = SpringContextHolder.getBean(MqDtoService.class);
        Map mapParameter = new HashMap<>();
        mapParameter.put(SystemConstant.RECORD_ID, recordId);
        MqDto mqDto = new MqDto(mqUtil.getTopic(), MqTagEnum.EXAM_START.name(), recordId, MqTagEnum.EXAM_START, String.valueOf(recordId), mapParameter, String.valueOf(recordId));
        mqDtoService.assembleSendAsyncOrderMsg(mqDto);
    }
}
