using Senparc.Weixin.MP.MessageHandlers;
using Senparc.Weixin.MP.Entities;
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Senparc.Weixin.MP.MessageContexts;
using Senparc.NeuChar.Entities;
using Senparc.Weixin.MP.Entities.Request;
using Hncore.Pass.BaseInfo.Service;
using Hncore.Pass.Sells.Service;
using Hncore.Wx.Open;
using Hncore.Infrastructure.Common;

namespace Home.Models
{
    public class MyMessageHandler : MessageHandler<DefaultMpMessageContext>
    {
        WxAppUserService m_WxAppUserService;
        CouponService m_CouponService;

        public MyMessageHandler(Stream inputStream
            , WxAppUserService _WxAppUserService
            , CouponService _CouponService
            , PostModel postModel = null
            , int maxRecordCount = 0) : base(inputStream, postModel)
        {
            m_WxAppUserService = _WxAppUserService;
            m_CouponService = _CouponService;
        }


        public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
        {
            string openid = requestMessage.FromUserName;         

            if (requestMessage.Content == "Hi")
            {
                var responseMessage = this.CreateResponseMessage<ResponseMessageText>();
                responseMessage.Content = "hello";
                return responseMessage;
            }
            return base.OnTextRequest(requestMessage);
        }

        /// <summary>
        /// 菜单按钮
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage)
        {
            switch (requestMessage.EventKey)
            {
                case "haibao": return null;               
            }
            return base.OnEvent_ClickRequest(requestMessage);
        }

        /// <summary>
        /// 关注
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
        {
            LogHelper.Info("OnEvent_SubscribeRequest", requestMessage.FromUserName);
            string appId = requestMessage.ToUserName;
            string openid = requestMessage.FromUserName;
            var appWxinfo = m_WxAppUserService.GetByOpenId(openid);
            if (appWxinfo == null)
            {
                var mpUserInfo = WxOpenApi.GetUserinfoByOpenId(appId, openid).Result;
                var wx = new Hncore.Pass.BaseInfo.Models.WxAppUserEntity()
                {
                    Appid = requestMessage.ToUserName,
                    UserId = 0,
                    HeadImgUrl = mpUserInfo.headimgurl,
                    NickName = mpUserInfo.nickname,
                    City = mpUserInfo.city,
                    Country = mpUserInfo.country,
                    Openid = openid,
                    UserName = mpUserInfo.nickname,
                    IsSubscribe=1,
                };
                m_WxAppUserService.Add(wx).Wait();
            }
            else if (appWxinfo.UserId>0 &&appWxinfo.IsSubscribe == 0)
            {
                appWxinfo.IsSubscribe = 1;
                m_WxAppUserService.Update(appWxinfo).Wait();
                m_CouponService.Give(5, "", appWxinfo.UserId, 1, Hncore.Pass.Sells.Domain.Enums.CouponOriginType.MP, "关注公众号赠送").Wait();
            }

           return base.OnEvent_SubscribeRequest(requestMessage);
        }


        /// <summary>
        /// 默认消息
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
        {
            // var responseMessage = this.CreateResponseMessage<ResponseMessageText>();
            //  responseMessage.Content = "Hi";
            //return responseMessage;
            return null;

        }
    }
}