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

namespace adas_core.Domain.Models;

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


    public PersonResource? Resource { get; set; }

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

    public static List<PersonResource>? Apply(List<PersonResource>? source, List<PersonResourceAction>? actions)
    {
        source ??= [];
        (actions ?? []).ForEach(s =>
        {
            if (s.Resource != null)
                switch (s.Action)
                {
                    case ActionsEnum.ResourceAction.Add:
                        if (!source.Contains(s.Resource)) source.Add(s.Resource);
                        break;
                    case ActionsEnum.ResourceAction.Delete:
                        if (source.Contains(s.Resource)) source.Remove(s.Resource);
                        break;
                    case ActionsEnum.ResourceAction.Update:
                        if (source.Contains(s.Resource)) source[source.IndexOf(s.Resource)] = s.Resource;
                        else source.Add(s.Resource);
                        break;
                }
        });
        return CollectionsUtils.NullIfEmpty(source);
    }
}