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;
using Jiguang.JPush;
using Jiguang.JPush.Model;
using System.Net;
using System.Linq;

namespace ASHermed.SingleSignOnFramework
{
    /// <summary>
	/// 极光推送(https://github.com/jpush/jpush-api-csharp-client)文档
	/// </summary>
	public class JpushService : IJpushService
    {
        ILogger<JpushService> _logger;
        //登录极光后台的生成的患者版Secret
        private readonly string portalMasterSecret = "2b8ae0501895fb5e6e3d8a05";
        //极光后台获取的患者版key
        private readonly string portalAppKey = "3dbfe676e1101b5de2d7b95e";
        //登录极光后台的生成的医生版Secret
        private readonly string medicalMasterSecret = "86ae7890bc176c503fc13bd2";
        //极光后台获取的医生版key
        private readonly string medicalAppKey = "a26f3bcc01aba416dade8d6d";
        private readonly int time = 86400;
        /// <summary>
        /// 极光推送
        /// </summary>
        public JpushService(ILogger<JpushService> logger)
        {
            _logger = logger;
        }

        public HttpResponse JiGuangPush(string[] arr, string msg_title, string msg_content, string msg_type, string businessId, string unread, string accptMd, int appType, string url = "")
        {
            HttpResponse result = Push(arr, msg_title, msg_content, msg_type, businessId, unread, accptMd, appType, url);
            return result;
        }

        public HttpResponse Push(string[] arr, string msg_title, string msg_content, string msg_type, string businessId, string unread, string accptMd, int appType, string url)
        {
            HttpResponse pushResult = null;
            string masterSecret = "";
            string appKey = "";
            //判断是向患者版发送还是医生版发送
            if (appType == 1)//患者
            {
                masterSecret = portalMasterSecret;
                appKey = portalAppKey;
            }
            else if (appType == 2)//医生
            {
                masterSecret = medicalMasterSecret;
                appKey = medicalAppKey;
            }
            JPushClient jpushClient = new JPushClient(appKey, masterSecret);
            PushPayload payload = null;
            if ("2" == accptMd)
            {
                payload = BuildPushObject_ios_alias_alert(arr, msg_title, msg_content, msg_type, businessId, unread, url);
            }
            else if ("3" == accptMd)
            {
                payload = BuildPushObject_android_alias_alert(arr, msg_title, msg_content, msg_type, businessId, unread, url);
            }

            try
            {
                pushResult = jpushClient.SendPush(payload);
            }
            catch (Exception ee)
            {
                _logger.LogError(ee.Message);
            }
            return pushResult;
        }

        public PushPayload BuildPushObject_android_alias_alert(string[] arr, string msg_title, string msg_content, string msg_type, string businessId, string unread, string url)
        {
            PushPayload load = new PushPayload();
            Audience audience = new Audience();
            audience.RegistrationId = arr.ToList();
            load.Platform = new List<string> { "android" };
            load.Audience = audience;
            Dictionary<string, object> ext = new Dictionary<string, object>();
            ext.Add("unread", unread);
            ext.Add("businessId", businessId);
            ext.Add("url", url);
            load.Message = new Message() { Content = msg_content, Title = msg_title, Extras = ext, ContentType = msg_type };
            load.Options = new Options() { IsApnsProduction = true, TimeToLive = time };
            return load;
        }
        public PushPayload BuildPushObject_ios_alias_alert(string[] arr, string msg_title, string msg_content, string msg_type, string businessId, string unread, string url)
        {
            PushPayload load = new PushPayload();
            Audience audience = new Audience();
            audience.RegistrationId = arr.ToList();
            load.Platform = new List<string> { "ios" };
            load.Audience = audience;
            Dictionary<string, object> ext = new Dictionary<string, object>();
            ext.Add("id", msg_type);
            ext.Add("businessId", businessId);
            ext.Add("unread", unread);
            ext.Add("contentType", msg_type);
            ext.Add("url", url);
            load.Notification = new Notification()
            {
                Alert = msg_title,
                IOS = new IOS
                {
                    Alert = msg_content,
                    Badge = unread,
                    Extras = ext,
                    Sound = "default"
                }
            };
            load.Options = new Options() { IsApnsProduction = true, TimeToLive = time };
            return load;
        }

        public bool TestJpush(string ios_id)
        {
            JPushClient client = new JPushClient(portalAppKey, portalMasterSecret);
            Audience audience = new Audience();
            audience.RegistrationId = new List<string>();
            audience.RegistrationId.Add("171976fa8a07b67334d");
            PushPayload pushPayload = new PushPayload()
            {
                Platform = new List<string> { "android", "ios" },
                Audience = audience,
                Notification = new Notification
                {
                    Alert = "hello jpush",
                    //Android = new Android
                    //{
                    //    Alert = "android alert",
                    //    Title = "title"
                    //},
                    IOS = new IOS
                    {
                        Alert = "ios alert",
                        Badge = "1",
                        Extras = new Dictionary<string, object>
                        {
                            ["id"] = "1",
                            ["businessId"] = "value1",
                            ["contentType"] = "1",
                            ["unread"] = "value2"
                        },
                        Sound = "default"
                    }
                },
                //Message = new Message
                //{
                //    Title = "message title",
                //    Content = "message content",
                //    Extras = new Dictionary<string, string>
                //    {
                //        ["businessId"] = "value1",
                //        ["unread"] = "value2"
                //    },
                //    ContentType="1"
                //},
                Options = new Options
                {
                    IsApnsProduction = false // 设置 iOS 推送生产环境。不设置默认为开发环境。
                }
            };
            var response = client.SendPush(pushPayload);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }
}
