using CloudGaming.Code.Contract;

using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Tea;

using static System.Runtime.InteropServices.JavaScript.JSType;

namespace CloudGaming.Code.Aliyun
{
    /// <summary>
    /// 阿里云身份证号验证  AlibabaExtend
    /// </summary>
    public class AlibabaIdCardVerify(AliyunConfig aliyunConfig) : IIdCardVerify
    {

        /// <term><b>Description:</b></term>
        /// <description>
        /// <para>使用AK&amp;SK初始化账号Client</para>
        /// </description>
        /// 
        /// <returns>
        /// Client
        /// </returns>
        /// 
        /// <term><b>Exception:</b></term>
        /// Exception
        public AlibabaCloud.SDK.Cloudauth20190307.Client CreateClient()
        {
            // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
            // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378671.html。
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                AccessKeyId = "LTAI5tEMoHbcDC5d9CQWovJk",//aliyunOssConfig.AccessKeyId, //Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                AccessKeySecret = "DgnYOJr0l9hTnl82vI4BxwVgtE1RdL"// aliyunOssConfig.AccessKeySecret,
            };
            // Endpoint 请参考 https://api.aliyun.com/product/Cloudauth
            config.Endpoint = "cloudauth.aliyuncs.com";
            return new AlibabaCloud.SDK.Cloudauth20190307.Client(config);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="idCard"></param>
        /// <returns></returns>
        public async Task<(bool isVerify, string msg)> IdCardVerify(string userName, string idCard)
        {
            AlibabaCloud.SDK.Cloudauth20190307.Client client = CreateClient();
            AlibabaCloud.SDK.Cloudauth20190307.Models.Id2MetaVerifyRequest id2MetaVerifyRequest = new AlibabaCloud.SDK.Cloudauth20190307.Models.Id2MetaVerifyRequest
            {
                ParamType = "normal",
                IdentifyNum = idCard,
                UserName = userName,
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            try
            {
                // 复制代码运行请自行打印 API 的返回值
                var obj = await client.Id2MetaVerifyWithOptionsAsync(id2MetaVerifyRequest, runtime);
                if (obj.Body.ResultObject.BizCode == "1")
                {
                    return (true, obj.Body.ResultObject.BizCode);
                }
                else
                {
                    return (false, obj.Body.ResultObject.BizCode == "2" ? "核验不一致" : $"{obj.Body.ResultObject.BizCode}_查无记录");
                }
            }
            catch (TeaException error)
            {
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                return (false, error.Message);
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                return (false, error.Message);
            }
        }
    }
}
