package com.backendsys.modules.sdk.tencentcloud.sms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.backendsys.exception.CustException;
import com.backendsys.modules.sdk.tencentcloud.sms.entity.TencentSmsCallbackStatis;
import com.backendsys.modules.sdk.tencentcloud.sms.entity.TencentSmsStatis;
import com.backendsys.modules.sdk.tencentcloud.sms.service.TencentSmsService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class TencentSmsServiceImpl implements TencentSmsService {

    @Value("${tencent.sms.secret-id}")
    private String SECRET_ID;
    @Value("${tencent.sms.secret-key}")
    private String SECRET_KEY;
    @Value("${tencent.sms.sdk-app-id}")
    private String SDK_APP_ID;
    @Value("${tencent.sms.sign-name}")
    private String SIGN_NAME;

    // [腾讯云SMS] 生成 SmsClient
    private SmsClient getSmsClient() {
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        ClientProfile clientProfile = new ClientProfile();
        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
        return client;
    }

    /**
     * [腾讯云SMS] 发送短信 { 模板ID, 模板参数, 下发手机号码 }
     * https://cloud.tencent.com/document/product/382/55981
     */
    @Override
    public SendSmsResponse send(String template_id, String[] templateParamSet, String[] phoneNumberSet) throws TencentCloudSDKException {

        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppId(SDK_APP_ID);
        req.setSignName(SIGN_NAME);
        req.setTemplateId(template_id);
        req.setTemplateParamSet(templateParamSet);
        req.setPhoneNumberSet(phoneNumberSet);

        // [SDK] 发送短信
        // 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
        // 返回的 res 是一个 SendSmsResponse 类的实例，与请求对象对应
        SmsClient client = getSmsClient();
        SendSmsResponse response = client.SendSms(req);
        return response;
    }

    /**
     * [腾讯云SMS] 获得统计短信发送数据
     * https://cloud.tencent.com/document/product/382/55965
     */
    @Override
    public TencentSmsStatis getSMSStatistics(String beginTime, String endTime) throws TencentCloudSDKException {

        if (StrUtil.isEmpty(beginTime)) throw new CustException("beginTime 不能为空");
        if (StrUtil.isEmpty(endTime)) throw new CustException("endTime 不能为空");

        SendStatusStatisticsRequest req = new SendStatusStatisticsRequest();
        req.setSmsSdkAppId(SDK_APP_ID);
        req.setLimit(0L);   // 最大上限 (注：目前固定设置为0)
        req.setOffset(0L);  // 偏移量 (注：目前固定设置为0
        req.setBeginTime(beginTime);
        req.setEndTime(endTime);

        // [SDK] 统计短信发送数据
        // 通过 client 对象调用 SendStatusStatistics 方法发起请求。注意请求方法名与请求对象是对应的
        // 返回的 res 是一个 SendStatusStatisticsResponse 类的实例，与请求对象对应
        SmsClient client = getSmsClient();
        SendStatusStatisticsResponse response = client.SendStatusStatistics(req);
        SendStatusStatistics statis = response.getSendStatusStatistics();

        // String resStr = SendStatusStatisticsResponse.toJsonString(response);

        TencentSmsStatis tencentSmsStatis = new TencentSmsStatis();
        tencentSmsStatis.setFee_count(statis.getFeeCount());
        tencentSmsStatis.setRequest_count(statis.getRequestCount());
        tencentSmsStatis.setRequest_success_count(statis.getRequestSuccessCount());
        return tencentSmsStatis;
    }

    /**
     * [腾讯云SMS] 获得回执数据统计
     * https://cloud.tencent.com/document/product/382/55966
     */
    @Override
    public TencentSmsCallbackStatis getSMSCallbackStatistics(String beginTime, String endTime) throws TencentCloudSDKException {

        if (StrUtil.isEmpty(beginTime)) throw new CustException("beginTime 不能为空");
        if (StrUtil.isEmpty(endTime)) throw new CustException("endTime 不能为空");

        CallbackStatusStatisticsRequest req = new CallbackStatusStatisticsRequest();
        req.setSmsSdkAppId(SDK_APP_ID);
        req.setLimit(0L);
        req.setOffset(0L);
        req.setBeginTime(beginTime);
        req.setEndTime(endTime);

        SmsClient client = getSmsClient();
        CallbackStatusStatisticsResponse response = client.CallbackStatusStatistics(req);
        CallbackStatusStatistics statis = response.getCallbackStatusStatistics();

        // String resStr = SendStatusStatisticsResponse.toJsonString(response);

        TencentSmsCallbackStatis tencentSmsCallbackStatis = new TencentSmsCallbackStatis();
        tencentSmsCallbackStatis.setCallback_count(statis.getCallbackCount());
        tencentSmsCallbackStatis.setRequest_success_count(statis.getRequestSuccessCount());
        tencentSmsCallbackStatis.setCallback_fail_count(statis.getCallbackFailCount());
        tencentSmsCallbackStatis.setCallback_success_count(statis.getCallbackSuccessCount());
        tencentSmsCallbackStatis.setInternal_error_count(statis.getInternalErrorCount());
        tencentSmsCallbackStatis.setInvalid_number_count(statis.getInvalidNumberCount());
        tencentSmsCallbackStatis.setShutdown_error_count(statis.getShutdownErrorCount());
        tencentSmsCallbackStatis.setBlack_list_count(statis.getBlackListCount());
        tencentSmsCallbackStatis.setFrequency_limit_count(statis.getFrequencyLimitCount());
        return tencentSmsCallbackStatis;
    }




}
