using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

namespace NexusShell.ViewModels;

public sealed class WorkspaceAppItem : INotifyPropertyChanged
{
    private bool _isActive;
    private bool _isExternal;
    private bool _isMinimized;

    public WorkspaceAppItem(string appId, string displayName, string iconKey, bool isExternal)
    {
        AppId = appId;
        DisplayName = displayName;
        IconKey = iconKey;
        IsExternal = isExternal;
        ActivateCommand = new RelayCommand(_ => ActivateRequested?.Invoke(this, appId));
        MinimizeCommand = new RelayCommand(_ => MinimizeRequested?.Invoke(this, appId));
        CloseCommand = new RelayCommand(_ => CloseRequested?.Invoke(this, appId));
    }

    public string AppId { get; }
    public string DisplayName { get; }
    public string IconKey { get; }
    public string StatusHint => IsExternal ? "Eksternt (POC)" : IsMinimized ? "Minimeret" : IsActive ? "I fokus" : "Kører";

    public bool IsActive
    {
        get => _isActive;
        set { _isActive = value; OnPropertyChanged(); OnPropertyChanged(nameof(StatusHint)); }
    }

    public bool IsExternal
    {
        get => _isExternal;
        set { _isExternal = value; OnPropertyChanged(); OnPropertyChanged(nameof(StatusHint)); }
    }

    public bool IsMinimized
    {
        get => _isMinimized;
        set { _isMinimized = value; OnPropertyChanged(); OnPropertyChanged(nameof(StatusHint)); }
    }

    public ICommand ActivateCommand { get; }
    public ICommand MinimizeCommand { get; }
    public ICommand CloseCommand { get; }

    public event EventHandler<string>? ActivateRequested;
    public event EventHandler<string>? MinimizeRequested;
    public event EventHandler<string>? CloseRequested;
    public event PropertyChangedEventHandler? PropertyChanged;

    private void OnPropertyChanged([CallerMemberName] string? name = null) =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
