using adas_core.Domain.Enums;
using adas_core.Domain.Utils;

namespace adas_core.Domain.Models;

public class PatientLocationAction
{
    public ActionsEnum.ResourceAction? Action { get; set; }

    public PatientLocation? Location { get; set; }


    public static List<PatientLocation>? Apply(List<PatientLocationAction>? actions)
    {
        return Apply(null, actions);
    }

    public static List<PatientLocation>? Apply(List<PatientLocation>? source, List<PatientLocationAction>? actions)
    {
        source ??= [];
        (actions ?? []).ForEach(s =>
        {
            if (s.Location != null)
                switch (s.Action)
                {
                    case ActionsEnum.ResourceAction.Add:
                        if (!source.Contains(s.Location)) source.Add(s.Location);
                        break;
                    case ActionsEnum.ResourceAction.Delete:
                        if (source.Contains(s.Location)) source.Remove(s.Location);
                        break;

                    case ActionsEnum.ResourceAction.Update:
                        if (source.Contains(s.Location)) source[source.IndexOf(s.Location)] = s.Location;
                        else source.Add(s.Location);
                        break;
                }
        });

        return CollectionsUtils.NullIfEmpty(source);
    }
}