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.MobileAuthCacheUtil;
import com.qmth.themis.business.cache.RedisKeyHelper;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.entity.SysConfig;
import com.qmth.themis.business.enums.MobileModeEnum;
import com.qmth.themis.business.enums.RoleEnum;
import com.qmth.themis.business.service.ThemisCacheService;
import com.qmth.themis.business.service.TEQrcodeService;
import com.qmth.themis.business.util.RedisUtil;
import com.qmth.themis.common.exception.BusinessException;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@Service
public class TEQrcodeServiceImpl implements TEQrcodeService {

    @Resource
    RedisUtil redisUtil;

    @Resource
    ThemisCacheService themisCacheService;

    @Override
    public QrResponseBean mobileMonitor(QrMobileMonitorParamBean param) throws NoSuchAlgorithmException {
        SysConfig sysConfig = themisCacheService.addSysConfigCache(SystemConstant.WXAPP_UPLOAD_URL);
        Optional.ofNullable(sysConfig).orElseThrow(() -> new BusinessException("未配置微信上传url"));

        String domain = sysConfig.getConfigValue();
        String code = SystemConstant.getNanoId();
        MobileModeEnum mode = MobileModeEnum.MOBILE_MONITOR;
        Set<String> roleCodes = new HashSet<>();
        roleCodes.add(RoleEnum.STUDENT.name());
        MobileAuthCacheUtil.setUserType(mode, code, roleCodes.toString());
        MobileAuthCacheUtil.setMode(mode, code);
        MobileAuthCacheUtil.setCode(mode, code);
        MobileAuthCacheUtil.setRecordId(mode, code, param.getRecordId());
        MobileAuthCacheUtil.setMonitorVideoSource(mode, code, param.getSource());

        SysConfig sysConfigQrExpireTime = themisCacheService.addSysConfigCache(SystemConstant.QR_EXPIRE_TIME);
        Optional.ofNullable(sysConfigQrExpireTime).orElseThrow(() -> new BusinessException("未设置二维码失效时间"));

        Integer qrExpireTime = Integer.parseInt(sysConfigQrExpireTime.getConfigValue());
        Long expireTime = -1L;
        if (qrExpireTime.intValue() > 0) {
            expireTime = DateUtils.addSeconds(new Date(), qrExpireTime.intValue()).getTime();
            redisUtil.expire(RedisKeyHelper.mobileAuthCacheKey(mode, code), qrExpireTime.intValue());
        }
        QrResponseBean ret = new QrResponseBean();
        ret.setExpireTime(expireTime);
        ret.setContent(domain + "/" + mode.name().toLowerCase() + "/" + code);
        return ret;
    }

    @Override
    public QrResponseBean upload(QrUploadParamBean param, MobileModeEnum mode) {
        SysConfig sysConfig = themisCacheService.addSysConfigCache(SystemConstant.WXAPP_UPLOAD_URL);
        Optional.ofNullable(sysConfig).orElseThrow(() -> new BusinessException("未配置微信上传url"));

        String domain = sysConfig.getConfigValue();
        String code = SystemConstant.getNanoId();
        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());
        MobileAuthCacheUtil.setQuestionNumber(mode, code, Objects.nonNull(param.getSubIndex()) ? "第" + param.getMainNumber() + "大题第" + param.getSubNumber() + "题第" + param.getQuestionNumber() + "小题" : "第" + param.getMainNumber() + "大题第" + param.getQuestionNumber() + "小题");
        if (param.getSubIndex() != null) {
            MobileAuthCacheUtil.setSubIndex(mode, code, param.getSubIndex());
        }
        SysConfig sysConfigQrExpireTime = themisCacheService.addSysConfigCache(SystemConstant.QR_EXPIRE_TIME);
        Optional.ofNullable(sysConfigQrExpireTime).orElseThrow(() -> new BusinessException("未设置二维码失效时间"));

        Integer qrExpireTime = Integer.parseInt(sysConfigQrExpireTime.getConfigValue());
        Long expireTime = -1L;
        if (qrExpireTime.intValue() > 0) {
            expireTime = DateUtils.addSeconds(new Date(), qrExpireTime.intValue()).getTime();
            redisUtil.expire(RedisKeyHelper.mobileAuthCacheKey(mode, code), qrExpireTime.intValue());
        }
        QrResponseBean ret = new QrResponseBean();
        ret.setExpireTime(expireTime);
        ret.setContent(domain + "/" + mode.name().toLowerCase() + "/" + code);
        return ret;
    }

}
