using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.IServices.OP;
using APT.Infrastructure.Api;
using APT.Infrastructure.Core;
using Newtonsoft.Json;
using System;
using System.Net;

namespace APT.BaseData.Services.Services.OP
{
    public class OPSmsService : CommonService, IOPSmsService
    {
        public OPSmsService(IRepository repository) : base(repository)
        {
        }

        public bool SendNote(T_FM_NOTICE note)
        {
            try
            {
                //Random r = new Random();
                //JSMSClient _smsClient = new JSMSClient("1cad1e6d2e226662f6388989", "692e9b7d38266a4f7220252d");
                //if (string.IsNullOrEmpty(note.MESSAGE_TEMPLATE_ID))
                //{
                //    JSMSResult result = _smsClient.SendTemplateMessage(new TemplateMessage
                //    {
                //        Mobile = note.TEL,
                //        TemplateId = 148580,//模板id是在极光平台上申请的
                //        TemplateParameters = new Dictionary<string, string>
                //        {
                //            { "date", note.CREATE_TIME.GetValueOrDefault().ToString("MM-dd HH:mm") },//报警时间
                //            { "msg",note.MESSAGE},
                //            { "msgid",r.Next(1,100).ToString()},//描述
                //            { "server","optiEnergy"}//报警端
                //        }
                //    });
                //    return result.Succeeded;
                //}
                //else
                //{
                //    JSMSResult result = _smsClient.SendTemplateMessage(new TemplateMessage
                //    {
                //        Mobile = note.TEL,
                //        TemplateId = Convert.ToInt32(note.MESSAGE_TEMPLATE_ID),//模板id是在极光平台上申请的
                //        TemplateParameters = new Dictionary<string, string>
                //        {
                //            { "code",note.MESSAGE}
                //        }
                //    });
                //    return result.Succeeded;
                //}

            }
            catch (Exception ex)
            {
                this.ThrowError("010001", $"{ex.Message};{ex.Source};{ex.StackTrace};{ex.InnerException}");
            }
            return true;

        }
        public bool SendMessage(string phoneNumber, string code)
        {
            //try
            //{
            //    T_FM_NOTICE notice = new T_FM_NOTICE()
            //    {
            //        ORG_ID = new Guid(LibUtils.ToString(ConfigurationManager.WexinSettings["OrgId"]).ToString()),
            //        MESSAGE = $"您的手机验证码：{code}，有效期1分钟，请勿泄露。如非本人操作，请忽略此短信。谢谢！",
            //        TEL = phoneNumber,
            //        NOTICE_TYPE = 0,
            //        //MESSAGE_TEMPLATE_ID = "148580"
            //    };
            //    this.AddEntity(notice);
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
            return true;
        }
        /// <summary>
        /// 极光短信返回实体类。
        /// </summary>
        private class JSMSResult
        {
            //{\"msg_id\":605999989525}
            //{"error":{"code":50013,"message":"invalid temp_id"}}
            /// <summary>
            /// 发送成功消息Id。
            /// </summary>
            public long Msg_Id { get; set; }

            /// <summary>
            /// 错误信息。
            /// </summary>
            public ErrorResult Error { get; set; }

            /// <summary>
            /// 是否发送成功。
            /// </summary>
            public bool Succeeded { get; private set; }

            //public static implicit operator JSMSResult(HttpResponse response)
            //{
            //    var result = JsonConvert.DeserializeObject<JSMSResult>(response.Content);
            //    result.Succeeded = response.StatusCode == HttpStatusCode.OK;
            //    return result;
            //}
        }

        /// <summary>
        /// 极光短信错误实体类
        /// </summary>
        private class ErrorResult
        {
            /// <summary>
            /// 错误编码。
            /// </summary>
            public int Code { get; set; }

            /// <summary>
            /// 错误消息
            /// </summary>
            public string Message { get; set; }
        }
    }
}
