using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using QYMessageCenter.Client.Models;
using QYMessageCenter.Client.Models.Msg;
using QYMessageCenter.Common.Extensions;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace QYMessageCenter.Client
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class GlobalContext
    {
        public GlobalContext()
        {
            messageHandleDic = new Dictionary<string, Action<Message>>()
            {
                { "PJZS", OnReceiveMessageFromPJZS }
            };

            popupManager = new PopupManager();
        }

        public User User { get; set; }

        public string UserToken { get; set; }

        public string GetUserString()
        {
            return JsonConvert.SerializeObject(User);
        }


        private IDictionary<string, Action<Message>> messageHandleDic;

        private PopupManager popupManager;

        public void OnError(string msg)
        {
            App.Current.Dispatcher.Invoke(() =>
            {
                MessageBox.Show(msg, "齐越消息中心");
            });
        }

        public void OnReceiveMessage(string channel, string message)
        {
            try
            {
                var msg = JsonConvert.DeserializeObject<Message>(message);

                if (!string.IsNullOrEmpty(msg.RecevierId) && !msg.RecevierId.Contains(User.Id.ToString()))
                    return; //忽略不是自己的消息
                if (messageHandleDic.ContainsKey(msg.AppCode))
                    messageHandleDic[msg.AppCode](msg);
                else
                    OnReceiveMessageFromUnknow(msg);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "齐越消息中心");
            }
        }

        private void OnReceiveMessageFromPJZS(Message msg)
        {
            if (msg.CustomTypeCode == "SHANGJIASHIBAI")
            {
                var smsg = msg.Map<Message_PJZS_SHANGJIASHIBAI>();
                var j = JObject.Parse(msg.Content);

                smsg.ActivityName = j.Value<string>("ActivityName");
                smsg.SpuLogo = j.Value<string>("SpuLogo");
                smsg.MainProductSpu = j.Value<string>("MainProductSpu");
                smsg.ErrorMsg = j.Value<string>("ErrorMsg");

                App.Current.Dispatcher.BeginInvoke(() => popupManager.Show(smsg));
                return;
            }

            App.Current.Dispatcher.BeginInvoke(() => popupManager.Show(msg));
        }

        private void OnReceiveMessageFromUnknow(Message msg)
        {
            //ReceiveMessageList.Insert(0, msg);
            App.Current.Dispatcher.BeginInvoke(() => popupManager.Show(msg));
        }

        public void Test()
        {
            App.Current.Dispatcher.BeginInvoke((Action)delegate
            {
                popupManager.Show(new Message_PJZS_SHANGJIASHIBAI()
                {
                    AppCode = "PJZS",
                    ShopId = "12899501",
                    CustomTypeCode = "SHANGJIASHIBAI",
                    Title = "信奉玩具专营店",
                    Content = JsonConvert.SerializeObject(new
                    {

                    }),
                    ActivityName = "小熊头枕腰靠",
                    MainProductSpu = "10025305065298",
                    SpuLogo = "https://img13.360buyimg.com/n9/s100x100_jfs/t1/235072/18/12198/94918/65b261a6F6de6ba33/23b803a34546427f.jpg",
                    ErrorMsg = "上架sku失败-com,jd.bk,saf.exception.SafJosException:销售属性排序冲冲突,同一顺序下不能有两个相同别名,别名:[柿柿如意发财果串-乔迁吊坠]顺序:[3]#8d306b629bae458faa58e9f5f0c883fb(Solution reference: https://jos.jd.com/commondoc?listld=171)"
                });
            });

            App.Current.Dispatcher.BeginInvoke((Action)delegate
            {
                popupManager.Show(new Message()
                {
                    AppCode = "PJZS",
                    ShopId = "12899501",
                    Title = "信奉玩具专营店",
                    CustomTypeCode = "NOTASK",
                    Content = "上架sku失败-com,jd.bk,saf.exception.SafJosException:这是一个普通模板消息",
                });
            });
        }
    }
}