using BBWY.Client.Helpers;
using GalaSoft.MvvmLight.Messaging;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WebSocketSharp;

namespace BBWY.Client.ViewModels
{
    public partial class OrderListViewModel
    {
        private ConcurrentDictionary<string, string> dongdongBuyerDictionary;
        private WebSocket webSocketClient;


        public void InitDongDong()
        {
            dongdongBuyerDictionary = new ConcurrentDictionary<string, string>();
        }

        private void CloseWebSocket()
        {
            if (webSocketClient != null)
            {
                if (webSocketClient.IsAlive)
                    webSocketClient.Close();
                webSocketClient.OnMessage -= WebSocketClient_OnMessage;
                webSocketClient = null;
            }
        }

        private void WebSocketClient_OnMessage(object sender, WebSocketSharp.MessageEventArgs e)
        {
            if (!e.IsText)
                return;
            var msg = e.Data;

            CloseWebSocket();
            Console.WriteLine($"OnDongDongSendMessage 接收到消息 {msg}");
            if (string.IsNullOrEmpty(msg))
                return;
            try
            {
                var j = JObject.Parse(msg);
                var buyerAccount = j.Value<string>("user");
                var orderId = j.Value<string>("orderId");
                if (string.IsNullOrEmpty(buyerAccount) && string.IsNullOrEmpty(orderId))
                    return;

                var phone = j.Value<string>("phone");
                if (!string.IsNullOrEmpty(buyerAccount) && !string.IsNullOrEmpty(phone))
                {
                    if (!dongdongBuyerDictionary.TryAdd(msg, phone))
                        return;
                }

                var order = OrderList.FirstOrDefault(o => o.Id == orderId);
                if (order == null)
                    order = OrderList.FirstOrDefault(o => !string.IsNullOrEmpty(o.BuyerAccount)
                                                          && o.BuyerAccount == buyerAccount
                                                          && o.Consignee != null
                                                          && !o.Consignee.IsDecode);

                if (order == null || order.Consignee == null || order.Consignee.IsDecode)
                    return;

                var response = orderService.DecodeConsignee(order.Id, phone);
                if (!response.Success)
                {
                    App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "解密失败"));
                    return;
                }
                order.Consignee.ContactName = response.Data.ContactName;
                order.Consignee.Address = response.Data.Address;
                order.Consignee.Mobile = response.Data.Mobile;
                order.Consignee.IsDecode = true;

                App.Current.Dispatcher.BeginInvoke((Action)delegate
                {
                    Messenger.Default.Send(string.Empty, "ActiveMainWindow");
                });
            }
            catch (Exception ex)
            {

            }
        }
    }
}