using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http.WebSockets;
using static System.Net.Mime.MediaTypeNames;
using WCS.Model.WebSocketModel;

namespace WCS.BLL.Manager
{
    public class WebSocketServicePlugin : PluginBase, IWebSocketReceivedPlugin
    {
        private readonly ILog m_logger;

        public WebSocketServicePlugin(ILog logger)
        {
            this.m_logger = logger;
        }
        public async Task OnWebSocketReceived(IWebSocket client, WSDataFrameEventArgs e)
        {
            switch (e.DataFrame.Opcode)
            {
                case WSDataType.Cont:
                    m_logger.Info($"收到中间数据，长度为：{e.DataFrame.PayloadLength}");

                    return;

                case WSDataType.Text:
                    m_logger.Info(e.DataFrame.ToText());

                    if (!client.Client.IsClient)
                    {
                        try
                        {
                            var message = e.DataFrame.ToText();
                            var warning = JsonConvert.DeserializeObject<WebSocketMessageModel>(message);
                            if (warning.IsWarning == false)
                            {
                                WarningManager.RemoveMessage(warning);
                                return;
                            }
                            if (warning != null)
                            {
                                //收到这个表示客户端已成功收到消息并提示给前端 避免一直发
                                var warningInManager = WarningManager.Warnings.Where(t => t.Guid == warning.Guid).FirstOrDefault();
                                if (warningInManager != null)
                                {
                                    warningInManager.ClientIsReceived = true;
                                }
                            }
                        }
                        catch
                        {
                        }

                    }
                    return;

                case WSDataType.Binary:
                    if (e.DataFrame.FIN)
                    {
                        m_logger.Info($"收到二进制数据，长度为：{e.DataFrame.PayloadLength}");
                    }
                    else
                    {
                        m_logger.Info($"收到未结束的二进制数据，长度为：{e.DataFrame.PayloadLength}");
                    }
                    return;

                case WSDataType.Close:
                    {
                        m_logger.Info("远程请求断开");
                        client.Close("断开");
                    }
                    return;

                case WSDataType.Ping:
                    client.Pong();
                    break;

                case WSDataType.Pong:
                    break;

                default:
                    break;
            }

            await e.InvokeNext();
        }
    }
}
