//using System;
//using Nancy;
//using System.Linq;
//using CommonUsage.Protocols.VDA5050.Messages;
//using Nancy.Extensions;
//using Newtonsoft.Json;
//using SimpleCore;
//using SimpleCore.Library;
//using System.Collections.Concurrent;
//using System.Timers;

//namespace StandardScene.CarTypes
//{
//    public class VDA5050WebApi : NancyModule
//    {
//        private static ConcurrentDictionary<string, DateTime> agvLastRequestTime =
//            new ConcurrentDictionary<string, DateTime>();

//        private static TimeSpan offlineThreshold = TimeSpan.FromSeconds(4);
//        private static Timer agvStatusCheckTimer;

//        public VDA5050WebApi()
//        {

//            Post["uagv/v2/manufacturer/SN/connection"] = param =>
//            {
//                var bodyStr = Request.Body.AsString();
//                var msg = JsonConvert.DeserializeObject<connectionMessage>(bodyStr);
//                var car = (VDA5050Car)SimpleLib.GetAllCars()
//                    .FirstOrDefault(cc => cc is VDA5050Car vdaCar && vdaCar.SerialNumber == msg.serialNumber);
//                if (car == null)
//                {
//                    Diagnosis.Post($"no car of serialNumber {msg.serialNumber}");
//                    return "";
//                }
//                Console.WriteLine($"Connection status---------------------{msg.connectionState}");
//                car.ConnectionStatus = msg.connectionState;
//                return "";
//            };

//            Post["/registration"] = param =>
//            {
//                var bodyStr = Request.Body.AsString();
//                var msg = JsonConvert.DeserializeObject<factsheetMessage>(bodyStr);

//                var car = (VDA5050Car)SimpleLib.GetAllCars()
//                    .FirstOrDefault(cc => cc is VDA5050Car vdaCar && vdaCar.SerialNumber == msg.serialNumber);
//                if (car == null)
//                {
//                    Diagnosis.Post($"no car of serialNumber {msg.serialNumber}");
//                    return "";
//                }

//                return "";
//            };

//            Post["/vda5050/state"] = param =>
//            {

//                var bodyStr = Request.Body.AsString();
//                var msg = JsonConvert.DeserializeObject<stateMessage>(bodyStr);

//                var car = (VDA5050Car)SimpleLib.GetAllCars()
//                    .FirstOrDefault(cc => cc is VDA5050Car vdaCar && vdaCar.SerialNumber == msg.serialNumber);
//                if (car == null)
//                {
//                    Diagnosis.Post($"no car of serialNumber {msg.serialNumber}");
//                    return "";
//                }

//                Console.WriteLine($"Is the car Paused: {msg.paused} ------------------");
//                if (msg.orderUpdatedId == car.OrderUpdateId) car.UpdateState(msg);
//                else Diagnosis.Post($"stateMessage orderUpdateId does not match!");
//                car.UpdateState(msg); // todo: what if orderUpdateId does not arrive in order?

//                return "";
//            };

//            Post["/vda5050/visualization"] = param =>
//            {

//                var bodyStr = Request.Body.AsString();
//                var msg = JsonConvert.DeserializeObject<stateMessage>(bodyStr);

//                var car = (VDA5050Car)SimpleLib.GetAllCars()
//                    .FirstOrDefault(cc => cc is VDA5050Car vdaCar && vdaCar.SerialNumber == msg.serialNumber);
//                if (car == null)
//                {
//                    Diagnosis.Post($"no car of serialNumber {msg.serialNumber}");
//                    return "";
//                }

//                car.UpdatePosition(msg);

//                return "";
//            };
//        }

//        private static void CheckAgvStatus(object sender, ElapsedEventArgs e)
//        {
//            var cars = SimpleLib.GetAllCars()
//                .OfType<VDA5050Car>(); // Assuming SimpleLib provides a way to get all cars

//            foreach (var car in cars)
//            {
//                if (agvLastRequestTime.TryGetValue(car.SerialNumber, out DateTime lastRequestTime))
//                {
//                    // Check if the time since the last request exceeds the offline threshold
//                    if ((DateTime.Now - lastRequestTime) > offlineThreshold)
//                    {
//                        if (car.ConnectionStatus == "Online")
//                        {
//                            car.ConnectionStatus = "Connection Broken";
//                            Diagnosis.Post($"AGV {car.SerialNumber} is now offline (no requests received).");
//                        }
//                    }
//                }
//                else
//                {
//                    // If there’s no record of the last request, the AGV is considered offline
//                    if (car.ConnectionStatus == "Online")
//                    {
//                        car.Online = false;
//                        Diagnosis.Post($"AGV {car.SerialNumber} is disconnected unexpectedly.");
//                    }
//                }
//            }
//        }
//    }
//}
