namespace Netina.AdminPanel.PWA.Models;

public class BaseViewModel
{
    public bool IsProcessing { get; set; } = false;

    public virtual void Initialize()
    {

    }
    public virtual Task InitializeAsync()
    {
        return Task.CompletedTask;
    }
}

public class BaseViewModel<TPageDto>
{
    public bool IsProcessing { get; set; } = false;
    public TPageDto PageDto { get; set; }
    private string[] _permissions = new string[]{};
    protected readonly IUserUtility _userUtility;
    public bool IsPermitted { get; set; } = false;
    public BaseViewModel(IUserUtility userUtility,params string[] permissions)
    {
        _userUtility = userUtility;
        _permissions = permissions;
        PageDto = Activator.CreateInstance<TPageDto>();
    }
    public virtual void Initialize()
    {

    }
    public virtual async Task InitializeAsync()
    {
        await CheckPermissions();
    }

    public async Task<bool> CheckPermissions()
    {
        if (_permissions.Length == 0)
        {
            IsPermitted = true;
            return true;
        }
        var permissions = await _userUtility.GetPermissionsAsync();
        if (permissions == null)
            return false;
        foreach (var permission in _permissions)
        {
            if (permissions.Contains(permission))
            {
                IsPermitted = true;
                return true;
            }
        }
        return false;
    }
}