package com.hys.app.controller.erp.use.msg;


import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.AddSmsTemplateRequest;
import com.aliyuncs.dysmsapi.model.v20170525.AddSmsTemplateResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.hys.app.framework.exception.ServiceException;

import java.util.HashMap;
import java.util.Map;

public class AliSmsUtil {
    private static String signName = "上海瑞赢旷视科技";
    public static String access_id = "LTAI5t9RbXSuirMa8AkvHxQf";
    public static String access_secret = "VCmMZvMKrcN2tBHXdenyEKJIoKbwQf";


    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化账号Client</p>
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId(access_id)
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret(access_secret);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 获取用于查询短信状态的客户端
     *
     * @return
     * @throws Exception
     */
    public static IAcsClient getIAcsClient() throws Exception {
        // 创建DefaultProfile实例，用于初始化客户端配置
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", access_id, access_secret);
        // 创建IAcsClient实例
        return new DefaultAcsClient(profile);
    }

    public static Boolean sendSms(String rePhone, String templateCode, String map) {

        try {
            com.aliyun.dysmsapi20170525.Client client = AliSmsUtil.createClient();
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setPhoneNumbers(rePhone)
                    .setTemplateCode(templateCode)
                    .setTemplateParam(map)
                    .setSignName(signName);
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String createSmsTemplate(Integer templateType, String templateName, String templateContent, String remark) {
        try {
            // 设置鉴权参数，初始化客户端
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", access_id, access_secret);
            System.out.println("Gson version: ---------------------------------" + com.google.gson.Gson.class.getPackage().getImplementationVersion());
            IAcsClient client = new DefaultAcsClient(profile);

            // 创建API请求并设置参数
            AddSmsTemplateRequest request = new AddSmsTemplateRequest();
            request.setTemplateType(templateType); // 短信类型，0为验证码，1为短信通知，2为推广短信
            request.setTemplateName(templateName);
            request.setTemplateContent(templateContent);
            request.setRemark(remark); // 模板申请的备注信息，如用途、使用场景等
            // 发起请求并处理响应
            AddSmsTemplateResponse response = client.getAcsResponse(request);
            if (!"OK".equals(response.getCode())) {
                throw new ServiceException("申请提交失败, 错误信息: " + response.getMessage());
            }
            return response.getTemplateCode();
        } catch (Exception e) {
            throw new ServiceException("短信模板创建失败");
        }
    }
}