
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.Service;
using ASHermed.SinoInterface.ContractModel;
using Jiguang.JPush.Model;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.IFramework
{
    /// <summary>
    /// 多对多 -- 极光推送服务
    /// </summary>
    public class JGPushService : BaseService, IJGPushService
    {
        IJpushService _jpushService;
        ILogger<JGPushService> _logger;
        IPubPatInfoRepository _pubPatInfoRepository;
        public JGPushService(IJpushService jpushService, ILogger<JGPushService> logger, IPubPatInfoRepository pubPatInfoRepository)
        {
            _jpushService = jpushService;
            _logger = logger;
            _pubPatInfoRepository = pubPatInfoRepository;
        }

        /// <summary>
        /// 多对多-发多用户极光  
        /// </summary>
        /// <param name="patId"></param>
        /// <param name="jgPushType">1咨询, 2患教, 3音视频, 4满意度调查, 5预约提交, 6预约取消, 7互动, 8APP更新, 9报告SignOff, 20复诊开药APP消息列表最新，21视频问诊APP消息列表最新, 25我的账单红点,30统一带跳转的,31诊前15分钟弹框, 32待支付订单提醒 35七陌回复提醒 38疫苗预约提醒</param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public bool PushMessageToUsers(int patId, string jgPushType, string title, string content, string url = "")
        {
            var users = _pubPatInfoRepository.GetUsersByPatId(patId);
            if (users?.Count > 0)
            {
                var andList = new List<string>();
                var iosList = new List<string>();
                foreach (var user in users)
                {
                    if (user.Key_Type == 1)
                    {
                        andList.Add(user.Android_ID);
                    }
                    else if(user.Key_Type == 2)
                    {
                        iosList.Add(user.Android_ID);
                    }
                    
                }
                if (andList.Count > 0)
                {
                    var res = _jpushService.JiGuangPush(andList.ToArray(), title, content, jgPushType, patId.ToString(), "0", "3", 1, url);
                }
                if (iosList.Count > 0)
                {
                    var res = _jpushService.JiGuangPush(iosList.ToArray(), title, content, jgPushType, patId.ToString(), "0", "2", 1, url);
                }
                return true;
            }
            
            return false;
        }
    }
}
