using System;
using System.Collections.Generic;
using AlibabaCloud.SDK.Dysmsapi20170525;
using AlibabaCloud.SDK.Dysmsapi20170525.Models;
using Newtonsoft.Json;
using Tea;
using Wicture.DbRESTFul;

namespace JiaZhiQuan.Common.AliMessage
{
    public class AliMessageHelper
    {
        /// <summary>
        /// key
        /// </summary>
        public readonly static string AccessKey = "LTAI5tGReikR9NjZT17dNQb4";
        /// <summary>
        /// secret
        /// </summary>
        public readonly static string AccessSecret = "xBIM4MAoFLghCy7D7AIipiLiq7BXn8";
        /// <summary>
        /// 短信签名
        /// </summary>
        public readonly static string SignName = "欧拉鼠";

        /// <summary>
        /// 初始化aliyun客户端
        /// </summary>
        /// <param name="accessKey">key</param>
        /// <param name="accessKeySecret">secret</param>
        /// <returns></returns>
        public static Client CreateClient(string accessKey, string accessKeySecret)
        {
            // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
            // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378671.html。
            AlibabaCloud.OpenApiClient.Models.Config config = new()
            {
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                AccessKeyId = accessKey, //;Environment.GetEnvironmentVariable(accessKey),
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                AccessKeySecret = accessKeySecret //Environment.GetEnvironmentVariable(accessKeySecret),
            };
            // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
            config.Endpoint = "dysmsapi.aliyuncs.com";
            return new Client(config);
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phoneNumber">手机号</param>
        /// <param name="template">模板</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static string SendMessage(string phoneNumber, AliMessageTemplateEnum template, string data)
        {
            Client client = CreateClient(AccessKey, AccessSecret);//填入授权码
            SendSmsRequest sendSmsRequest = new()
            {
                SignName = SignName,//短信签名
                TemplateCode = template.GetDescription(),//短信模板
                PhoneNumbers = phoneNumber,//手机号
                TemplateParam = data, //模板参数
            }; 
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new();
            var resMessage = string.Empty;
            try
            {
                // 复制代码运行请自行打印 API 的返回值
                var res = client.SendSmsWithOptions(sendSmsRequest, runtime);
                resMessage = res.Body.Message;
            }
            catch (TeaException error)
            {
                // 如有需要，请打印 error
                resMessage = AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                LoggerManager.Logger.Error($"调用阿里云发送验证码短信API失败：" + resMessage);
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // 如有需要，请打印 error
                resMessage = AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                LoggerManager.Logger.Error($"调用阿里云发送验证码短信API失败：" + resMessage);
            }
            return resMessage;
        }
    }
}
