using RobotNet.RobotShares.Dtos;

namespace RobotNet.RobotManager.Services.Traffic;

public class TrafficMap
{
    public Guid MapId { get; set; }
    public Dictionary<string, Agent> Agents { get; set; } = [];
    public Dictionary<string, List<TrafficNodeDto>> Locked { get; set; } = [];
    public List<TrafficConflict> Conflicts { get; set; } = [];
    public List<TrafficGiveway> GivewayResolution { get; set; } = [];

    public void AddLocker(string robotId, TrafficNodeDto[] releaseNodes)
    {
        //Console.WriteLine($"{robotId} Add Locker: {string.Join(", ", releaseNodes.Select(n => n.Name))}");
        if (Locked.TryGetValue(robotId, out List<TrafficNodeDto>? lockedNodes) && lockedNodes is not null) Locked[robotId].AddRange(releaseNodes.Where(n => !lockedNodes.Any(ln => ln.Id == n.Id)));
        else Locked.Add(robotId, [.. releaseNodes]);
    }

    public void UpdateLocker(string robotId, TrafficNodeDto[] releaseNodes)
    {
        //Console.WriteLine($"{robotId} Update Locker: {string.Join(", ", releaseNodes.Select(n => n.Name))}");
        if (Locked.TryGetValue(robotId, out _)) Locked[robotId] = [.. releaseNodes];
        else Locked.Add(robotId, [.. releaseNodes]);
    }
}
