using System.Diagnostics;
using System.Text;
using BLW_Log.Models;
using Microsoft.AspNetCore.Mvc;
using NLog;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace BLW_Log.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        //private static Logger _logger = LogManager.GetCurrentClassLogger();
        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }



        [HttpGet("stream")]
        public async Task StreamSSEvents()
        {
            HttpContext.Response.ContentType = "text/event-stream";
            HttpContext.Response.Headers.Add("Cache-Control", "no-cache");
            HttpContext.Response.Headers.Add("Connection", "keep-alive");

            for (int i = 0; i < 10; i++)
            {
                var data = new
                {
                    id = Guid.NewGuid().ToString(),
                    eventmm = "111",
                    data = "22222"
                };

                var responseData = $"id: {data.id} ";
                responseData += $"event: {data.eventmm} ";
                responseData += $"data: 111111111111111 ";

                await HttpContext.Response.WriteAsync(responseData, Encoding.UTF8);
                await Task.Delay(2000); // 等待2秒再发送下一个事件
            }

        }
    }
}
