using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SinoInterface.ContractModel;
using ASHermed.SinoInterface.Tools;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Ashermed.BaseFramework;
using ASHermed.SinoInterface.IFramework;
using ASHermed.SinoInterface.Api.ContractModel.App;
using Microsoft.Extensions.Logging;

namespace ASHermed.SingleSignOnFramework
{
    /// <summary>
	/// 助通短信服务(https://doc.zthysms.com/web/#/1?page_id=14)文档
	/// </summary>
	public class ZhuTongSMSService : IZhuTongSMSService
    {
        IHttpClientHelp httpClient;
        AppConfigurtaion appConfigurtaion;
        ILogger<ZhuTongSMSService> logger;
        /// <summary>
        /// 助通服务
        /// </summary>
        /// <param name="_httpClient"></param>
        public ZhuTongSMSService(IHttpClientHelp _httpClient, AppConfigurtaion _appConfigurtaion, ILogger<ZhuTongSMSService> _logger)
        {
            httpClient = _httpClient;
            appConfigurtaion = _appConfigurtaion;
            logger = _logger;
        }



        /// <summary>
        /// 发短信
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="content">内容中必须包含签名</param>
        /// <returns></returns>
        public ZhuTongResb SendSMS(string phone, string content)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
            long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数
            string tkey = timeStamp.ToString();
            string pwd = Common.GetMD5((Common.GetMD5(appConfigurtaion.GetZhuTongPassword) + tkey)).ToLower();
            //content += signature;

            var dic = new Dictionary<string, string>();
            dic.Add("username", appConfigurtaion.GetZhuTongUserName);
            dic.Add("password", pwd);
            dic.Add("tKey", tkey);
            dic.Add("mobile", phone);
            dic.Add("content", content);
            //var body = new
            //{
            //    username = appConfigurtaion.GetZhuTongUserName,
            //    password = pwd,
            //    tKey = tkey,
            //    mobile = phone,
            //    content = content,
            //    //time = "2020-01-01 09:00:00",//定时
            //    //ext = "9999"
            //};
            var listParm = new List<(string Name, string FileName, object Value)>();
            listParm.Add(("username", "", appConfigurtaion.GetZhuTongUserName));
            listParm.Add(("password", "", pwd));
            listParm.Add(("tKey", "", tkey));
            listParm.Add(("mobile", "", phone));//"15857396623"
            listParm.Add(("content", "", content));
            try
            {
                var result = httpClient.HttpPost<ZhuTongResb>(appConfigurtaion.GetZhuTongSMSUrl, "", listParm);
                return result.Data;
            }
            catch (Exception ee)
            {
                logger.LogError(ee, "SendSMS:" + ee.Message);
                return new ZhuTongResb();
            }


        }

        /// <summary>
        /// 发短信
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="content">内容中必须包含签名</param>
        /// <param name="isChinese">是否是中文</param>
        /// <returns></returns>
        public ZhuTongResb SendSMS(string phone, string content, bool isChinese = true)
        {
            string sign = string.Empty;
            if (isChinese)
            {
                sign = "【" + appConfigurtaion.GetZhuTongSMSCNSgin + "】";
            }
            else
            {
                sign = "【" + appConfigurtaion.GetZhuTongSMSENSgin + "】";
            }
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
            long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数
            string tkey = timeStamp.ToString();
            string pwd = Common.GetMD5((Common.GetMD5(appConfigurtaion.GetZhuTongPassword) + tkey)).ToLower();
            var listParm = new List<(string Name, string FileName, object Value)>();
            listParm.Add(("username", "", appConfigurtaion.GetZhuTongUserName));
            listParm.Add(("password", "", pwd));
            listParm.Add(("tKey", "", tkey));
            listParm.Add(("mobile", "", phone));
            listParm.Add(("content", "", content + sign));
            try
            {
                var result = httpClient.HttpPost<ZhuTongResb>(appConfigurtaion.GetZhuTongSMSUrl, "", listParm);
                return result.Data;
            }
            catch (Exception ee)
            {
                logger.LogError(ee, "SendSMS:" + ee.Message);
                return new ZhuTongResb();
            }

        }



    }
}
