using RobotNet.RobotShares.VDA5050.State;

namespace RobotNet.WebApp.Robots.Components.Monitoring.Element;

#nullable disable

public class RobotVisualizationModel : IDisposable
{
    public string RobotId { get; set; }
    public string RobotName { get; set; }
    public double X { get; set; }
    public double Y { get; set; }
    public double Theta { get; set; }
    public Load[] Loads { get; set; } = [];
    public event Func<Task> PositionChanged;
    public event Func<Guid, Task> LoadsChanged;
    public void Update(double x, double y, double theta, Load[] loads)
    {
        if (X != x || Y != y || Theta != theta)
        {
            X = x;
            Y = y;
            Theta = theta;
            PositionChanged?.Invoke();
        }

        if (loads.Length > 0 && (Loads.Length == 0 || Loads[0].LoadId != loads[0].LoadId))
        {
            if (Guid.TryParse(loads[0].LoadId, out Guid elementModelId)) LoadsChanged?.Invoke(elementModelId);
            else LoadsChanged?.Invoke(Guid.Empty);
            Loads = loads;
        }
        else if (Loads.Length != 0 && loads.Length == 0)
        {
            LoadsChanged?.Invoke(Guid.Empty);
            Loads = loads;
        }
    }
    public void Dispose()
    {
        GC.SuppressFinalize(this);
    }
}
