using Furion.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Senparc.Weixin.Open;
using Senparc.Weixin.Open.Entities.Request;
using Senparc.Weixin.Open.MessageHandlers;
using System.IO;
using YBDevice.Entity;
using YBDevice.WXApplication.WXInfo;

namespace YBDevice.WX.MessageHandlers
{
    /// <summary>
    /// 开放平台推送消息
    /// </summary>
    public class CustomThirdPartyMessageHandler : ThirdPartyMessageHandler
    {
        public CustomThirdPartyMessageHandler(Stream inputStream, PostModel encryptPostModel)
         : base(inputStream, encryptPostModel)
        { }

        /// <summary>
        ///  推送component_verify_ticket协议
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override string OnComponentVerifyTicketRequest(RequestMessageComponentVerifyTicket requestMessage)
        {
            //保存openticket
            Scoped.Create(async (_, scope) =>
            {
                var services = scope.ServiceProvider;
                var wxService = services.GetService<IWXService>();
                await wxService.InsertOrUpdateAsync(new YB_OpenWXConfig
                {
                    ComponentAppId = requestMessage.AppId,
                    OpenTicket = requestMessage.ComponentVerifyTicket
                });
            });
            return base.OnComponentVerifyTicketRequest(requestMessage);
        }

        /// <summary>
        /// 取消授权
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override string OnUnauthorizedRequest(RequestMessageUnauthorized requestMessage)
        {
            //取消授权
            Scoped.Create(async (_, scope) =>
            {
                var services = scope.ServiceProvider;
                var wxService = services.GetService<IWXService>();
                await wxService.NoticeUpdateAsync(new YB_OfficlaAccount
                {
                    componentappid = requestMessage.AppId,
                    authorizer_appid = requestMessage.AuthorizerAppid,
                    isauthorize = 0
                });
            });
            return base.OnUnauthorizedRequest(requestMessage);
        }

        /// <summary>
        /// 授权成功通知
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override string OnAuthorizedRequest(RequestMessageAuthorized requestMessage)
        {
            Scoped.Create(async (_, scope) =>
            {
                var services = scope.ServiceProvider;
                var wxService = services.GetService<IWXService>();
                await wxService.NoticeUpdateAsync(new YB_OfficlaAccount
                {
                    componentappid = requestMessage.AppId,
                    authorizer_appid = requestMessage.AuthorizerAppid,
                    authorizeationcode = requestMessage.AuthorizationCode,
                    authorizationcodeexpiredtime = requestMessage.AuthorizationCodeExpiredTime.DateTime,
                    isauthorize = 1
                });
            });
            return base.OnAuthorizedRequest(requestMessage);
        }

        /// <summary>
        /// 授权更新通知
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override string OnUpdateAuthorizedRequest(RequestMessageUpdateAuthorized requestMessage)
        {
            Scoped.Create(async (_, scope) =>
            {
                var services = scope.ServiceProvider;
                var wxService = services.GetService<IWXService>();
                await wxService.NoticeUpdateAsync(new YB_OfficlaAccount
                {
                    componentappid = requestMessage.AppId,
                    authorizer_appid = requestMessage.AuthorizerAppid,
                    authorizeationcode = requestMessage.AuthorizationCode,
                    authorizationcodeexpiredtime = requestMessage.AuthorizationCodeExpiredTime.DateTime,
                    isauthorize = 1
                });
            });
            return base.OnUpdateAuthorizedRequest(requestMessage);
        }
    }
}
