using CommunityToolkit.Maui;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GreadyPoang.Services;

namespace GreadyPoang.ViewModelLayer;

public partial class InfoPopupViewModel : ObservableObject, IQueryAttributable
{
    private readonly IPopupService _popupService;
    private readonly IPopupEventHub _popupEvent;
    public string PopupId { get; } = Guid.NewGuid().ToString();

    public event EventHandler ClosePopupRequested;

    [ObservableProperty]
    private string title;

    [ObservableProperty]
    private string message;

    [ObservableProperty]
    private string name;
    public InfoPopupViewModel(IPopupService popupService, IPopupEventHub popupEvent)
    {
        _popupService = popupService;
        _popupEvent = popupEvent;
        LatestPopup.valueGuid = PopupId;
    }

    [RelayCommand]
    private async Task Cancel()
    {
        _popupEvent.RaiseInfoPopupClose(PopupId);
    }

    [RelayCommand(CanExecute = nameof(CanSave))]
    void OnSave()
    {
    }

    bool CanSave() => string.IsNullOrWhiteSpace(Name) is false;

    public void ApplyQueryAttributes(IDictionary<string, object> query)
    {
        Title = (string)query[nameof(InfoPopupViewModel.Title)];
        Message = (string)query[nameof(InfoPopupViewModel.Message)];
        Name = (string)query[nameof(InfoPopupViewModel.Name)];
    }
}
