package com.jeequan.jeepay.core.sms;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.smsconfig.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class JuhedxSmsHandler implements ISmsHandler {
    @Override
    public void sendVercode(SmsBizVercodeModel smsBizVercodeModel, AbstractSmsConfig smsConfig) {
        JuhedxSmsConfig juhedxSmsConfig = (JuhedxSmsConfig)smsConfig;

        String smsTemplateId = "";
        if (CS.SMS_TYPE_API_ENUM.TYPE_AUTH.equals(smsBizVercodeModel.getSmsBizType())) {
            smsTemplateId = juhedxSmsConfig.getLoginMchTemplateId();
        }else if (CS.SMS_TYPE_API_ENUM.TYPE_REGISTER.equals(smsBizVercodeModel.getSmsBizType())) {
            smsTemplateId = juhedxSmsConfig.getRegisterMchTemplateId();
        }else if (CS.SMS_TYPE_API_ENUM.TYPE_RETRIEVE.equals(smsBizVercodeModel.getSmsBizType())) {
            smsTemplateId = juhedxSmsConfig.getForgetPwdTemplateId();
        }else if (CS.SMS_TYPE_API_ENUM.TYPE_MBR_TEL_BIND.equals(smsBizVercodeModel.getSmsBizType())) {
            smsTemplateId = juhedxSmsConfig.getMbrTelBindTemplateId();
        }

        // 生成短信验证码
        String verifyCode = smsBizVercodeModel.getSmsVercode();
        JSONObject codeObj = new JSONObject();
        codeObj.put("mobile", smsBizVercodeModel.getPhoneNo());
        codeObj.put("tpl_id", smsTemplateId);
        codeObj.put("tpl_value", "#code#=" + verifyCode);
        codeObj.put("key", juhedxSmsConfig.getAccessKeyId());

        JuhedxSmsProcessKit.callInterface(juhedxSmsConfig, smsTemplateId, codeObj);
    }

    @Override
    public void sendDiyContent(SmsBizDiyContentModel smsBizDiyContentModel, AbstractSmsConfig smsConfig) {
        JuhedxSmsConfig juhedxSmsConfig = (JuhedxSmsConfig) smsConfig;
        String templateId = juhedxSmsConfig.getForgetPwdTemplateId();
        JSONObject codeObj = new JSONObject();
        codeObj.put("mobile", smsBizDiyContentModel.getPhoneNo());
        if(CS.SMS_TYPE_API_ENUM.TYPE_ACCOUNT_OPEN.equals(smsBizDiyContentModel.getSmsBizType())){
            codeObj.put("loginName", smsBizDiyContentModel.getString(SmsBizDiyContentModel.FIELD_LOGIN_NAME));
            codeObj.put("loginPwd", smsBizDiyContentModel.getString(SmsBizDiyContentModel.FIELD_USER_PWD));
        }
        if(CS.SMS_TYPE_API_ENUM.TYPE_ACCOUNT_BALANCE.equals(smsBizDiyContentModel.getSmsBizType())){
            codeObj.put("tpl_value", "#name#=" + smsBizDiyContentModel.getString("name") + "&#amount#=" + smsBizDiyContentModel.getString("amount"));
            templateId = juhedxSmsConfig.getAccountBalanceTemplateId();
        }
        codeObj.put("key", juhedxSmsConfig.getAccessKeyId());
        codeObj.put("tpl_id", templateId);
        JuhedxSmsProcessKit.callInterface(juhedxSmsConfig, templateId, codeObj);
    }

    @Override
    public String querySmsInfo(AbstractSmsConfig smsConfig, String bizQueryType) {
        throw new BizException("聚合数据不支持该操作");
    }
}
