package com.ho.common.tools.util;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendBatchSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendBatchSmsResponse;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;

/**
 * 阿里发送短信工具类
 */
@Slf4j
public class AliSendSmsUtils {

    private static final String accessKeyId = "LTAI5tCQyHiB3ww1dKhVhedx";

    private static final String accessKeySecret = "tvovJTgBVhn93rWcWTcoFekLrqHSCN";

    /**
     * 发送短信
     * @param param JSON对象(),key分别为 name（用户名） siteName（电站名称） content （告警信息）
     * @param phoneNumbers
     * @throws Exception
     */
    public static void sendMessage(String param,String phoneNumbers) throws Exception{
        sendMessage("弘正储能","SMS_463325018",param,phoneNumbers);
    }

    /**
     * 发送短信
     * @param param JSON对象(),key分别为 name（用户名），4G卡号: num siteName（电站名称） content （告警信息）
     * @param phoneNumbers
     * @throws Exception
     */
    public static void sendFlowMessage(String param,String phoneNumbers) throws Exception{
        sendMessage("弘正储能","SMS_463625526",param,phoneNumbers);
    }

    /**
     * 发送短信
     * @param signName 签名
     * @param templateCode 模板
     * @param param 参数
     * @param phoneNumbers 被发送的电话号码
     * @throws Exception
     */
    public static void sendMessage(String signName,String templateCode,String param,String phoneNumbers) throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName(signName)
                .templateCode(templateCode)
                .phoneNumbers(phoneNumbers)
                .build();
        if(param != null){
            sendSmsRequest = SendSmsRequest.builder()
                    .signName(signName)
                    .templateCode(templateCode)
                    .templateParam(param)
                    .phoneNumbers(phoneNumbers)
                    .build();
        }
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }

    /**
     * 批量发送短信
     * @param signName 签名
     * @param templateCode 模板
     * @param param 参数
     * @param phoneNumbers 被发送的电话号码
     * @throws Exception
     */
    public static void sendBatchMessage(String signName,String templateCode,String param,String phoneNumbers) throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        SendBatchSmsRequest sendBatchSmsRequest = SendBatchSmsRequest.builder()
                .signNameJson(signName)
                .templateCode(templateCode)
                .phoneNumberJson(phoneNumbers)
                .build();

        CompletableFuture<SendBatchSmsResponse> response = client.sendBatchSms(sendBatchSmsRequest);
        SendBatchSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }

    public static void main(String[] args) throws Exception {
//        JSONObject param = new JSONObject();
//        param.put(CommonConstant.SMS.SITE_NAME,"test");
//        param.put(CommonConstant.SMS.CONTENT,CommonConstant.Heartbeat.STATION_FAIL);
//        param.put(CommonConstant.SMS.NAME,"json");
//        sendMessage(param.toJSONString(),"");
    }


}
