package com.qmth.themis.business.util;

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.entity.TEExamStudentOnlineLog;
import com.qmth.themis.business.enums.WebsocketStatusEnum;
import com.qmth.themis.business.service.TEExamStudentOnlineLogService;
import com.qmth.themis.business.service.TEExamStudentService;
import com.qmth.themis.business.service.TOeExamRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: websocket util
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/7/27
 */
public class WebsocketUtil {

    private final static Logger log = LoggerFactory.getLogger(WebsocketUtil.class);

    public static Object getFieldInstance(Object obj, String fieldPath) {
        String fields[] = fieldPath.split("#");
        for (String field : fields) {
            obj = getField(obj, obj.getClass(), field);
            if (obj == null) {
                return null;
            }
        }

        return obj;
    }

    public static Object getField(Object obj, Class<?> clazz, String fieldName) {
        for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
            try {
                Field field;
                field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                return field.get(obj);
            } catch (Exception e) {
            }
        }
        return null;
    }

    /**
     * 更新websocket状态
     *
     * @param recordId
     * @param websocketSessionId
     * @param websocketStatusEnum
     */
    public static void updateExamRecordWebsocketStatus(Long recordId, String websocketSessionId,
                                                       WebsocketStatusEnum websocketStatusEnum) {
        Long timestamp = System.currentTimeMillis();
        ExamRecordCacheUtil.setClientWebsocketStatus(recordId, websocketStatusEnum, timestamp);
        ExamRecordCacheUtil.setClientWebsocketId(recordId, websocketSessionId);
        Long clientLastSyncTime = System.currentTimeMillis();
        ExamRecordCacheUtil.setClientLastSyncTime(recordId, clientLastSyncTime);
        TOeExamRecordService tOeExamRecordService = SpringContextHolder.getBean(TOeExamRecordService.class);
        tOeExamRecordService.sendExamRecordDataSaveMq(recordId, timestamp);

        Long examStudentId = ExamRecordCacheUtil.getExamStudentId(recordId);
        if (Objects.nonNull(examStudentId)) {
            TEExamStudentService teExamStudentService = SpringContextHolder.getBean(TEExamStudentService.class);
            ExamStudentCacheBean examStudentCacheBean = teExamStudentService.getExamStudentCacheBean(examStudentId);

            if (Objects.nonNull(examStudentCacheBean)) {
                TEExamStudentOnlineLogService teExamStudentOnlineLogService = SpringContextHolder.getBean(TEExamStudentOnlineLogService.class);
                teExamStudentOnlineLogService.sendExamStudentOnlineLogSaveMq(new TEExamStudentOnlineLog(examStudentCacheBean.getStudentId(), examStudentId, recordId, websocketStatusEnum, timestamp));
            }
        }
    }

    /**
     * 更新mobile first websocket状态
     *
     * @param recordId
     * @param websocketSessionId
     * @param websocketStatusEnum
     */
    public static void updateExamRecordMobileFirstWebsocketStatus(Long recordId, String websocketSessionId,
                                                                  WebsocketStatusEnum websocketStatusEnum) {
        Long timestamp = System.currentTimeMillis();
        ExamRecordCacheUtil.setMobileFirstWebsocketStatus(recordId, websocketStatusEnum, timestamp);
        ExamRecordCacheUtil.setMobileFirstWebsocketId(recordId, websocketSessionId);
        TOeExamRecordService tOeExamRecordService = SpringContextHolder.getBean(TOeExamRecordService.class);
        tOeExamRecordService.sendExamRecordDataSaveMq(recordId, timestamp);
    }

    /**
     * 更新mobile second websocket状态
     *
     * @param recordId
     * @param websocketSessionId
     * @param websocketStatusEnum
     */
    public static void updateExamRecordMobileSecondWebsocketStatus(Long recordId, String websocketSessionId,
                                                                   WebsocketStatusEnum websocketStatusEnum) {
        Long timestamp = System.currentTimeMillis();
        ExamRecordCacheUtil.setMobileSecondWebsocketStatus(recordId, websocketStatusEnum, timestamp);
        ExamRecordCacheUtil.setMobileSecondWebsocketId(recordId, websocketSessionId);
        TOeExamRecordService tOeExamRecordService = SpringContextHolder.getBean(TOeExamRecordService.class);
        tOeExamRecordService.sendExamRecordDataSaveMq(recordId, timestamp);
    }

    /**
     * 初始化websocket
     *
     * @param recordId
     * @param deviceId
     * @param updateTime
     * @return
     */
    public static Map<String, Object> initWebsocket(Long recordId, String deviceId, Long updateTime) {
        Map<String, Object> tranMap = new HashMap<>();
        if (Objects.nonNull(recordId)) {
            tranMap.put(SystemConstant.RECORD_ID, recordId);
        }
        tranMap.put("deviceId", deviceId);
        tranMap.put(SystemConstant.UPDATE_TIME, updateTime);
        return tranMap;
    }
}