package com.tzld.piaoquan.growth.common.utils;


import com.alibaba.fastjson.JSON;
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.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

public class SendShortMessageUtil {

    public final static String ACCESS_KEY_ID = "LTAI5t714Be8LxczJ1Z2z3hz";
    public final static String ACCESS_KEY_SECRET = "2NdJ69NXOsTyCOBr0nf9jNS7a5r9MI";
    private final static String REGION = "cn-hangzhou";
    private final static String END_POINT = "dysmsapi.aliyuncs.com";
    private final static String SIGN_NAME = "票圈视频";


    public static void sendShortMessage(String phoneNumbers, String templateCode, Map<String, String> params) {

        AsyncClient client = AsyncClient.builder()
                .region(REGION)
                .credentialsProvider(StaticCredentialProvider.create(Credential.builder()
                        .accessKeyId(ACCESS_KEY_ID)
                        .accessKeySecret(ACCESS_KEY_SECRET)
                        .build()))
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(END_POINT)
                )
                .build();

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(phoneNumbers)
                .signName(SIGN_NAME)
                .templateCode(templateCode)
                .templateParam(JSON.toJSONString(params))
                .build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = null;
        try {
            resp = response.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
//        System.out.println(new Gson().toJson(resp));
        client.close();
    }

    public static void sendVerifyCode(String phoneNumber, String verifyCode) {
        //String templateCode = "SMS_483325081";
        //Map<String, String> params = new HashMap<>();
        //params.put("code", verifyCode);
        //sendShortMessage(phoneNumber, templateCode, params);
    }


    public static void main(String[] args) {
        // 初始化账号密码
        //String phoneNumbers = "18742010048";
        //String signName = "票圈视频";
        //String templateCode = "SMS_463221247";
        //Map<String, String> params = new HashMap<>();
        //params.put("username", "admin021");
        //params.put("password", "admin021");
        //sendShortMessage(phoneNumbers, templateCode, params);
    }

}
