using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MVMoniter
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //private UdpLoggerReceiver logReceiver;
        private UdpReceiver udpReceiver;
        public MainWindow()
        {
            InitializeComponent();

            //logReceiver = new UdpLoggerReceiver(5000);
            //logReceiver.LogReceived += OnLogReceived;

            udpReceiver = new UdpReceiver("127.0.0.1", 5000); // 示例 IP 和端口
            udpReceiver.OnMessageReceived += UdpReceiver_OnMessageReceived;
            Task.Run(() => udpReceiver.StartReceiving()); // 启动接收 UDP 消息
        }

        //private void OnLogReceived(string message)
        //{
        //    Dispatcher.Invoke(() => AppendLog(message));
        //}

        //private void AppendLog(string message)
        //{
        //    LogBox.Document.Blocks.Add(new Paragraph(new Run(message)));
        //    LogBox.ScrollToEnd();
        //}

        // 接收消息的事件处理函数
        private void UdpReceiver_OnMessageReceived(object sender, string message)
        {
            // 将接收到的日志信息添加到 TextBlock 控件中
            Dispatcher.BeginInvoke(new Action(() =>
            {
                //LogTextBlock.Text += message + Environment.NewLine;
                LogTextBlock.Text += message;
            }));
        }


        protected override void OnClosed(EventArgs e)
        {
            //base.OnClosed(e);
            //logReceiver?.Close();
            udpReceiver.StopReceiving();
            base.OnClosed(e);
        }
        //-----------------------------------------------
    }
}
