using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using TouchSocket.Core;
using TouchSocket.Sockets;

namespace JiShe.CollectBus.Plugins
{
    public partial class ServerMonitor(ILogger<ServerMonitor> logger) : PluginBase, IServerStartedPlugin, IServerStoppedPlugin
    {
        public Task OnServerStarted(IServiceBase sender, ServiceStateEventArgs e)
        {
            switch (sender)
            {
                case ITcpService service:
                {
                    foreach (var item in service.Monitors)
                    {
                        logger.LogInformation($"TCP {item.Option.IpHost}");
                    }

                    break;
                }
                case UdpSession session:
                    //logger.LogInformation($"UDP {session.Monitor.IPHost}");
                    break;
            }

            logger.LogInformation(e.ServerState == ServerState.Running
                ? $"服务器成功启动"
                : $"服务器启动失败，状态：{e.ServerState}，异常：{e.Exception}");
            return e.InvokeNext();
        }

        public Task OnServerStopped(IServiceBase sender,ServiceStateEventArgs e)
        {
            logger.LogInformation("服务已停止");
            return e.InvokeNext();
        }
    }
}
