package com.qmth.themis.business.service.impl;

import com.qmth.themis.business.bean.exam.QrMobileMonitorParamBean;
import com.qmth.themis.business.bean.exam.QrResponseBean;
import com.qmth.themis.business.bean.exam.QrUploadParamBean;
import com.qmth.themis.business.cache.ExamRecordCacheUtil;
import com.qmth.themis.business.cache.MobileAuthCacheUtil;
import com.qmth.themis.business.cache.RedisKeyHelper;
import com.qmth.themis.business.cache.bean.ExamCacheBean;
import com.qmth.themis.business.config.SystemConfig;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.enums.MobileModeEnum;
import com.qmth.themis.business.enums.MonitorVideoSourceEnum;
import com.qmth.themis.business.enums.RoleEnum;
import com.qmth.themis.business.service.TEExamService;
import com.qmth.themis.business.service.TEQrcodeService;
import com.qmth.themis.business.util.RedisUtil;
import com.qmth.themis.common.enums.Source;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@Service
public class TEQrcodeServiceImpl implements TEQrcodeService {

    @Resource
    TEExamService examService;
    @Resource
    RedisUtil redisUtil;
    @Resource
    SystemConfig systemConfig;

    @Override
    public QrResponseBean mobileMonitor(QrMobileMonitorParamBean param) throws NoSuchAlgorithmException {
        String domain = systemConfig.getProperty("wxapp.upload.url");
        String code = uuid();
        MobileModeEnum mode = MobileModeEnum.MOBILE_MONITOR;
        Set<String> roleCodes = new HashSet<>();
        roleCodes.add(RoleEnum.STUDENT.name());
        MobileAuthCacheUtil.setUserType(mode, code, roleCodes.toString());
        MobileAuthCacheUtil.setSource(mode, code, param.getSource());
        MobileAuthCacheUtil.setMode(mode, code);
        MobileAuthCacheUtil.setCode(mode, code);
        MobileAuthCacheUtil.setRecordId(mode, code, param.getRecordId());
        MobileAuthCacheUtil.setMonitorKey(mode, code, ExamRecordCacheUtil.getMonitorKey(param.getRecordId()));
        MobileAuthCacheUtil.setMonitorVideoSource(mode, code, param.getSource());
        MobileAuthCacheUtil.setMonitorAudioEnable(mode, code, getMonitorAudioEnable(param));
        Long expireTime = DateUtils.addSeconds(new Date(), SystemConstant.QR_EXPIRE_TIME.intValue()).getTime();
        redisUtil.expire(RedisKeyHelper.mobileAuthCacheKey(mode, code), SystemConstant.QR_EXPIRE_TIME.intValue());
        QrResponseBean ret = new QrResponseBean();
        ret.setExpireTime(expireTime);
        ret.setContent(domain + "/" + mode.name().toLowerCase() + "/" + code);
        return ret;
    }

    private Boolean getMonitorAudioEnable(QrMobileMonitorParamBean param) {
        Long examId = ExamRecordCacheUtil.getExamId(param.getRecordId());
        ExamCacheBean exam = examService.getExamCacheBean(examId);
        String monitorVideoSource = exam.getMonitorVideoSource();
        if (!monitorVideoSource.toUpperCase().contains(MonitorVideoSourceEnum.CLIENT_SCREEN.name())
                && !monitorVideoSource.toUpperCase().contains(MonitorVideoSourceEnum.CLIENT_CAMERA.name())
                && Source.MOBILE_FIRST.equals(param.getSource())) {
            return true;
        } else {
            return false;
        }
    }

    private String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    @Override
    public QrResponseBean upload(QrUploadParamBean param, MobileModeEnum mode) {
        String domain = systemConfig.getProperty("wxapp.upload.url");
        String code = uuid();
        MobileAuthCacheUtil.setMode(mode, code);
        MobileAuthCacheUtil.setCode(mode, code);
        MobileAuthCacheUtil.setRecordId(mode, code, param.getRecordId());
        MobileAuthCacheUtil.setMainNumber(mode, code, param.getMainNumber());
        MobileAuthCacheUtil.setSubNumber(mode, code, param.getSubNumber());
        if (param.getSubIndex() != null) {
            MobileAuthCacheUtil.setSubIndex(mode, code, param.getSubIndex());
        }
        Long expireTime = DateUtils.addSeconds(new Date(), SystemConstant.QR_EXPIRE_TIME.intValue()).getTime();
        redisUtil.expire(RedisKeyHelper.mobileAuthCacheKey(mode, code), SystemConstant.QR_EXPIRE_TIME.intValue());
        QrResponseBean ret = new QrResponseBean();
        ret.setExpireTime(expireTime);
        ret.setContent(domain + "/" + mode.name().toLowerCase() + "/" + code);
        return ret;
    }

}
