
using Api.SignToken;
using MadRunFabric.Common;
using Microsoft.Extensions.Configuration;
using Senparc.Weixin.Context;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Entities.Request;
using Senparc.Weixin.MP.MessageHandlers;
using System;
using System.IO;

namespace WechatApi.MessageHandlers
{
    public partial class CustomMessageHandler : MessageHandler<MessageContext<IRequestMessageBase, IResponseMessageBase>>
    {
        public readonly ISignTokenService _signTokenService;
        public readonly IConfiguration _configuration;
        public CustomMessageHandler(Stream inputStream, PostModel postModel, IConfiguration configuration, ISignTokenService signTokenService) : base(inputStream, postModel)
        {
            _signTokenService = signTokenService;
            _configuration = configuration;
        }

        /// <summary>
        /// 订阅事件
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
        {
            if (!string.IsNullOrEmpty(requestMessage.EventKey))
            {
                string eventkey = requestMessage.EventKey;
                string usercode = eventkey.Replace("qrscene_", "");
                string title = "绑定账号成功！";
                string result = _signTokenService.Wechat_BindAsync(usercode, requestMessage.FromUserName).Result;
                if (result.ToJObject()["state"].ToString() != "success")
                {
                    title = "绑定账号失败！";
                }
                var responseMessage = base.CreateResponseMessage<ResponseMessageNews>();
                var art = new Article();
                art.Title = title;
                art.Description = "账号：" + usercode + "\n时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                responseMessage.Articles.Add(art);
                return responseMessage;
            }
            else
            {
                //var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
                //responseMessage.Content = "你好，欢迎关注NuodaMaintenance！";
                //return responseMessage;

                var responseMessage = base.CreateResponseMessage<ResponseMessageNews>();
                var json = _configuration["WechatStatic:SubscribeJson"];
                responseMessage.Articles = json.ToList<Article>();
                return responseMessage;
            }
        }

        /// <summary>
        /// 扫描事件
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_ScanRequest(RequestMessageEvent_Scan requestMessage)
        {
            if (!string.IsNullOrEmpty(requestMessage.EventKey))
            {
                string usercode = requestMessage.EventKey;
                string title = "绑定账号成功！";
                string result = _signTokenService.Wechat_BindAsync(usercode, requestMessage.FromUserName).Result;
                if (result.ToJObject()["state"].ToString() != "success")
                {
                    title = "绑定账号失败！";
                }

                var responseMessage = base.CreateResponseMessage<ResponseMessageNews>();
                var art = new Article();
                art.Title = title;
                art.Description = "账号：" + usercode + "\n时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                responseMessage.Articles.Add(art);
                return responseMessage;
            }
            else
            {
                //var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
                //responseMessage.Content = "你好，欢迎扫描关注NuodaMaintenance！";
                //return responseMessage;

                var responseMessage = base.CreateResponseMessage<ResponseMessageNews>();
                var json = _configuration["WechatStatic:SubscribeJson"];
                responseMessage.Articles = json.ToList<Article>();
                return responseMessage;
            }
        }

        /// <summary>
        /// 默认回复
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "欢迎你！";
            return responseMessage;
        }
    }

}