package com.qmth.teachcloud.common.util;

import com.alibaba.fastjson.JSONObject;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.boot.core.sms.model.SmsSendRequest;
import com.qmth.boot.core.sms.model.SmsSendResponse;
import com.qmth.boot.core.sms.service.SmsService;
import com.qmth.teachcloud.common.bean.result.SmsResponseResult;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysConfig;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.MessageEnum;
import com.qmth.teachcloud.common.service.CommonCacheService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 短信发送工具类
 */
@Component
public class SmsSendUtil {

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

    public static final String OK = "OK";
    private static final String ERROR = "ERROR";

    @Resource
    private SmsService smsService;

    @Resource
    CommonCacheService commonCacheService;


    /**
     * 短信发送接口
     *
     * @param phoneNumber   发送手机号
     * @param configKey     参数key
     * @param templateParam 模版变量
     * @return bizId 本次操作业务标识
     */
    public SmsResponseResult sendSms(String phoneNumber, String configKey, Map<String, Object> templateParam) {
        validPhoneNumber(phoneNumber);
        SmsSendRequest smsSendRequest = new SmsSendRequest();
        // 必填，发送手机号
        smsSendRequest.setPhoneNumber(phoneNumber);
        // 必填，短信签名
        String signName = getConfigValue(SystemConstant.SMS_SIGN_NAME);
        smsSendRequest.setSignName(signName);
        // 必填，短信模版编号
        String templateCode = getConfigValue(configKey);
        smsSendRequest.setTemplateCode(templateCode);
        //模版变量，可选
        if (templateParam != null && !templateParam.isEmpty()) {
            smsSendRequest.setTemplateParam(templateParam);
        }
        SmsResponseResult smsResponseResult = new SmsResponseResult();
        try {
            SmsSendResponse smsSendResponse = smsService.sendSms(smsSendRequest);
            if (smsSendResponse == null) {
                throw ExceptionResultEnum.ERROR.exception("短信发送失败");
            }
            smsResponseResult.setCode(OK);
            smsResponseResult.setMessage(smsSendResponse.getBizId());
        } catch (ApiException e) {
            smsResponseResult.setCode(ERROR);
            smsResponseResult.setMessage(e.getMessage());
        }
        return smsResponseResult;
    }

    /**
     * 校验手机号
     *
     * @param phoneNumber 手机号
     */
    private void validPhoneNumber(String phoneNumber) {
        if (StringUtils.isBlank(phoneNumber)) {
            throw ExceptionResultEnum.ERROR.exception("手机号不能为空");
        }
        if (phoneNumber.length() != 11) {
            throw ExceptionResultEnum.ERROR.exception("手机号长度必须为11位");
        }
    }

    /**
     * 校验文件存储配置参数
     *
     * @param configKey 参数key
     */
    private String getConfigValue(String configKey) {
        if (StringUtils.isBlank(configKey)) {
            throw ExceptionResultEnum.ERROR.exception("参数key值不能为空");
        }
        SysConfig sysConfig = commonCacheService.addSysConfigCache(configKey);
        if (sysConfig == null) {
            throw ExceptionResultEnum.ERROR.exception("参数key[" + configKey + "]数据不存在");
        }
        if (StringUtils.isBlank(sysConfig.getConfigValue())) {
            throw ExceptionResultEnum.ERROR.exception("参数key[" + configKey + "]对应值不能为空");
        }
        return sysConfig.getConfigValue();
    }

    public Map<String, String> getCodeAndContentByEnum(MessageEnum messageEnum) {
        Map<String, String> result = new HashMap<>();
        String templateContent = null;
        String templateCode = null;
        switch (messageEnum) {
            case NOTICE_OF_AUDIT_PASS:
                templateCode = SystemConstant.SMS_AUDIT_PASS_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_AUDIT_NOT_PASS:
                templateCode = SystemConstant.SMS_AUDIT_NOT_PASS_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_EXAM_TASK_CREATED:
                templateCode = SystemConstant.SMS_EXAM_TASK_CREATE_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_EXAM_TASK_WILL_EXPIRE:
                templateCode = SystemConstant.SMS_EXAM_TASK_WILL_EXPIRE_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_EXAM_TASK_OVERDUE:
                templateCode = SystemConstant.SMS_EXAM_TASK_OVERDUE_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_ALLOCATION_WILL_EXPIRE:
                templateCode = SystemConstant.SMS_ALLOCATION_WILL_EXPIRE_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_ALLOCATION_OVERDUE:
                templateCode = SystemConstant.SMS_ALLOCATION_OVERDUE_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_AUDIT_CREATED:
                templateCode = SystemConstant.SMS_AUDIT_CREATE_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_AUDIT_REVIEW:
                templateCode = SystemConstant.SMS_AUDIT_REVIEW_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_AUDIT_WILL_EXPIRE:
                templateCode = SystemConstant.SMS_AUDIT_WILL_EXPIRE_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_AUDIT_OVERDUE:
                templateCode = SystemConstant.SMS_AUDIT_OVERDUE_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_AUDIT_REJECT:
                templateCode = SystemConstant.SMS_AUDIT_REJECT_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_UPLOAD_STRUCTURE:
                templateCode = SystemConstant.SMS_UPLOAD_STRUCTURE_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            case NOTICE_OF_AUDIT_COPY_USER:
                templateCode = SystemConstant.SMS_AUDIT_COPY_USER_CODE;
                templateContent = messageEnum.getTemplate();
                break;
            default:
                break;
        }
        result.put("templateContent", templateContent);
        result.put("templateCode", templateCode);
        return result;
    }

}
