package com.qmth.themis.exam.websocketTemplete;

import com.alibaba.fastjson.JSONObject;
import com.qmth.themis.business.cache.ExamRecordCacheUtil;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dto.WebsocketDto;
import com.qmth.themis.business.enums.ExamRecordFieldEnum;
import com.qmth.themis.business.enums.WebsocketTypeEnum;
import com.qmth.themis.business.service.TOeExamRecordService;
import com.qmth.themis.business.util.RedisUtil;
import com.qmth.themis.common.enums.ExceptionResultEnum;
import com.qmth.themis.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Objects;

/**
 * @Description: 考生端oe websocket模版
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/7/24
 */
@Component
public class WebSocketOeMessageTemplete {
    private final static Logger log = LoggerFactory.getLogger(WebSocketOeMessageTemplete.class);

    @Resource
    RedisUtil redisUtil;

    @Resource
    TOeExamRecordService tOeExamRecordService;

    /**
     * 客户端已下载试卷
     *
     * @param body
     * @return
     */
    public WebsocketDto clientPaperDownload(String body) {
        JSONObject jsonObject = JSONObject.parseObject(body);
        log.info("clientPaperDownload jsonObject:{}", jsonObject.toJSONString());
        if (Objects.isNull(jsonObject.get("recordId")) || Objects.equals(jsonObject.get("recordId"), "")) {
            throw new BusinessException(ExceptionResultEnum.RECORD_ID_IS_NULL);
        }
        Long recordId = Long.parseLong(String.valueOf(jsonObject.get("recordId")));
        Long examId = ExamRecordCacheUtil.getExamId(recordId);
        if (Objects.isNull(examId)) {
            throw new BusinessException(ExceptionResultEnum.RECORD_NO);
        }
        ExamRecordCacheUtil.setPaperDownload(recordId, 0, true);
        return new WebsocketDto(Collections.singletonMap("success", true));
    }

    /**
     * 状态同步
     *
     * @param body
     * @return
     */
    public WebsocketDto syncStatus(String body) {
        JSONObject jsonObject = JSONObject.parseObject(body);
        log.info("syncStatus jsonObject:{}", jsonObject.toJSONString());
        if (Objects.isNull(jsonObject.get("recordId")) || Objects.equals(jsonObject.get("recordId"), "")) {
            throw new BusinessException(ExceptionResultEnum.RECORD_ID_IS_NULL);
        }
        Long recordId = Long.parseLong(String.valueOf(jsonObject.get("recordId")));
        if (Objects.isNull(jsonObject.get("progress")) || Objects.equals(jsonObject.get("progress"), "")) {
            throw new BusinessException("答题进度不能为空");
        }
        Double progress = Double.parseDouble(String.valueOf(jsonObject.get("progress")));
        if (Objects.isNull(jsonObject.get("durationSeconds")) || Objects.equals(jsonObject.get("durationSeconds"), "")) {
            throw new BusinessException("考试累计用时秒数不能为空");
        }
        Integer durationSeconds = Integer.parseInt((String.valueOf(jsonObject.get("durationSeconds"))));
        Long examId = ExamRecordCacheUtil.getExamId(recordId);
        if (Objects.isNull(examId)) {
            throw new BusinessException(ExceptionResultEnum.RECORD_NO);
        }
        ExamRecordCacheUtil.setAnswerProgress(recordId, progress, false);
        ExamRecordCacheUtil.setDurationSeconds(recordId, durationSeconds, false);
        Long clientLastSyncTime = System.currentTimeMillis();
        ExamRecordCacheUtil.setClientLastSyncTime(recordId, clientLastSyncTime, false);
        String[] columns = new String[]{ExamRecordFieldEnum.answer_progress.name(), ExamRecordFieldEnum.duration_seconds.name(), ExamRecordFieldEnum.client_last_sync_time.name()};
        Object[] values = new Object[]{progress, durationSeconds, clientLastSyncTime};
        tOeExamRecordService.dataUpdatesMq(recordId, columns, values);
        return this.syncAck(body);
    }

    /**
     * 同步确认
     *
     * @param body
     * @return
     */
    public WebsocketDto syncAck(String body) {
        return new WebsocketDto(WebsocketTypeEnum.convertToName(Thread.currentThread().getStackTrace()[1].getMethodName()), Collections.singletonMap(SystemConstant.ACK_MESSAGE, System.currentTimeMillis()));
    }

    /**
     * 监考消息确认
     *
     * @param body
     * @return
     */
    public WebsocketDto invigilateNoticeAck(String body) {
        return new WebsocketDto(WebsocketTypeEnum.convertToName(Thread.currentThread().getStackTrace()[1].getMethodName()), Collections.singletonMap(SystemConstant.ACK_MESSAGE, System.currentTimeMillis()));
    }
}
