using Furion.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Senparc.NeuChar.Entities;
using Senparc.Weixin.MP.Entities;
using System;
using System.Threading.Tasks;
using YBDevice.WXApplication.DeviceInfo;

namespace YBDevice.WX.MessageHandlers.CustomMessageHandler
{
    /// <summary>
    /// 自定义MessageHandler
    /// </summary>
    public partial class CustomMessageHandler
    {
        /// <summary>
        /// 预处理文字或事件类型请求。
        /// 这个请求是一个比较特殊的请求，通常用于统一处理来自文字或菜单按钮的同一个执行逻辑，
        /// 会在执行OnTextRequest或OnEventRequest之前触发，具有以下一些特征：
        /// 1、如果返回null，则继续执行OnTextRequest或OnEventRequest
        /// 2、如果返回不为null，则终止执行OnTextRequest或OnEventRequest，返回最终ResponseMessage
        /// 3、如果是事件，则会将RequestMessageEvent自动转为RequestMessageText类型，其中RequestMessageText.Content就是RequestMessageEvent.EventKey
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override Task<IResponseMessageBase> OnTextOrEventRequestAsync(RequestMessageText requestMessage)
        {
            return base.OnTextOrEventRequestAsync(requestMessage);
        }
        /// <summary>
        /// 二维码扫码事件
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override async Task<IResponseMessageBase> OnEvent_ScanRequestAsync(RequestMessageEvent_Scan requestMessage)
        {
            //检查是否为带参二维码
            if (!string.IsNullOrEmpty(requestMessage.EventKey))
            {
                var sceneId = requestMessage.EventKey.Replace("qrscene_", "");
                #region 针对自定义二维码的处理
                var paramarr = sceneId.Split('#');
                var deviceService = base.ServiceProvider.GetService<IDeviceService>();
                if (paramarr.Length == 4 && paramarr[0].ToLower() == "yb")
                {
                    #region 针对八电极绑定处理 参数格式:yb#1#设备id#粉丝唯一标识
                    if (paramarr[1] == "1")
                    {
                        var result = await deviceService.InsertOrUpdateBodyBindInfoAsync(paramarr[3], paramarr[2], requestMessage.ToUserName, requestMessage.FromUserName);
                        var responseMessage = CreateResponseMessage<ResponseMessageText>();
                        responseMessage.Content = result.message;
                        return responseMessage;
                    }
                    #endregion
                    else if (paramarr[1] == "2") //认证的服务号测量二维码
                    {
                        Guid wxfansid = Guid.Parse(paramarr[3]);
                        //更新关注状态
                        var resultid = await deviceService.InsertOrUpdateSubscribeAsync(requestMessage.ToUserName, requestMessage.FromUserName, wxfansid, 2, 2);
                        if (!string.IsNullOrEmpty(resultid))
                        {
                            var arts = deviceService.GetWXMessageNews(RequestMessage.ToUserName, requestMessage.FromUserName, paramarr[3], 1, resultid);
                            var requestMessagenews = base.CreateResponseMessage<ResponseMessageNews>();
                            requestMessagenews.Articles.AddRange(arts);
                            return requestMessagenews;
                        }
                    }
                    else if (paramarr[1] == "3") //固定贴纸关注二维码
                    {
                        //更新关注状态
                        Guid wxfansid = Guid.Parse(paramarr[3]);
                        var resultid = await deviceService.InsertOrUpdateSubscribeAsync(requestMessage.ToUserName, requestMessage.FromUserName, wxfansid, 2, 1);
                        if (!string.IsNullOrEmpty(resultid))
                        {
                            var rid = Guid.Parse(resultid);
                            var arts = deviceService.GetStickyWXMessageNews(RequestMessage.ToUserName, requestMessage.FromUserName, paramarr[3], 2, rid);
                            var requestMessagenews = base.CreateResponseMessage<ResponseMessageNews>();
                            requestMessagenews.Articles.AddRange(arts);
                            return requestMessagenews;
                        }
                    }
                }
                #endregion
            }
            return await base.OnEvent_ScanRequestAsync(requestMessage);
        }
        /// <summary>
        /// 关注事件
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override async Task<IResponseMessageBase> OnEvent_SubscribeRequestAsync(RequestMessageEvent_Subscribe requestMessage)
        {
            var deviceService = base.ServiceProvider.GetService<IDeviceService>();
            //检查是否为带参二维码
            if (!string.IsNullOrEmpty(requestMessage.EventKey))
            {
                var sceneId = requestMessage.EventKey.Replace("qrscene_", "");
                #region 针对自定义二维码的处理
                var paramarr = sceneId.Split('#');
                if (paramarr.Length == 4 && paramarr[0].ToLower() == "yb")
                {
                    #region 针对八电极绑定处理 参数格式:yb#1#设备id#粉丝唯一标识
                    if (paramarr[1] == "1")
                    {
                        var result = await deviceService.InsertOrUpdateBodyBindInfoAsync(paramarr[3], paramarr[2], requestMessage.ToUserName, requestMessage.FromUserName);
                        var responseMessage = CreateResponseMessage<ResponseMessageText>();
                        responseMessage.Content = result.message;
                        return responseMessage;
                    }
                    #endregion
                    else if (paramarr[1] == "2") //认证的服务号测量二维码
                    {
                        Guid wxfansid = Guid.Parse(paramarr[3]);
                        //更新关注状态
                       var resultid=await deviceService.InsertOrUpdateSubscribeAsync(requestMessage.ToUserName, requestMessage.FromUserName, wxfansid, 1,2);
                        if (!string.IsNullOrEmpty(resultid))
                        {
                            var arts = deviceService.GetWXMessageNews(RequestMessage.ToUserName, requestMessage.FromUserName, paramarr[3], 1, resultid);
                            var requestMessagenews = base.CreateResponseMessage<ResponseMessageNews>();
                            requestMessagenews.Articles.AddRange(arts);
                            return requestMessagenews;
                        }
                    }
                    else if (paramarr[1] == "3") //固定贴纸二维码
                    {
                        //更新关注状态
                        Guid wxfansid = Guid.Parse(paramarr[3]);
                        var resultid= await deviceService.InsertOrUpdateSubscribeAsync(requestMessage.ToUserName, requestMessage.FromUserName, wxfansid, 1,1);
                        if (!string.IsNullOrEmpty(resultid))
                        {
                            var rid = Guid.Parse(resultid);
                            var arts = deviceService.GetStickyWXMessageNews(RequestMessage.ToUserName, requestMessage.FromUserName, paramarr[3], 2, rid);
                            var requestMessagenews = base.CreateResponseMessage<ResponseMessageNews>();
                            requestMessagenews.Articles.AddRange(arts);
                            return requestMessagenews;
                        }
                    }
                }
                #endregion
            }
            await deviceService.InsertOrUpdateSubscribeAsync(requestMessage.ToUserName, requestMessage.FromUserName,null,3);

            //if(!await new OfficialApp().CheckIsRZFAsync(requestMessage.ToUserName))
            //{
            //    var arts = new DeviceApp().GetWXMessageNews(RequestMessage.ToUserName, requestMessage.FromUserName, "", 3);
            //    var requestMessagenews = base.CreateResponseMessage<ResponseMessageNews>();
            //    requestMessagenews.Articles.AddRange(arts);
            //    return requestMessagenews;
            //}
            return await base.OnEvent_SubscribeRequestAsync(requestMessage);
        }

        /// <summary>
        /// 取关
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override async Task<IResponseMessageBase> OnEvent_UnsubscribeRequestAsync(RequestMessageEvent_Unsubscribe requestMessage)
        {
            var deviceService = base.ServiceProvider.GetService<IDeviceService>();
            //更新取关信息
            await deviceService.InsertOrUpdateSubscribeAsync(requestMessage.ToUserName, requestMessage.FromUserName,null, 0);
            return await base.OnEvent_UnsubscribeRequestAsync(requestMessage);
        }
    }
}
