using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Nexus.Contracts.Apps;

namespace NexusShell.ViewModels;

public sealed class NexusAppPlaceholderViewModel : INotifyPropertyChanged
{
    private string _statusMessage;

    public NexusAppPlaceholderViewModel(AppEntry app)
    {
        AppId = app.Id;
        Name = string.IsNullOrWhiteSpace(app.Name) ? "Nexus app" : app.Name;
        Category = string.IsNullOrWhiteSpace(app.Category) ? "Nexus" : app.Category;
        Description = string.IsNullOrWhiteSpace(app.Description)
            ? "Denne app er registreret i Nexus, men indholdet er endnu ikke aktiveret i POC-builden."
            : app.Description;
        Status = string.IsNullOrWhiteSpace(app.Status) ? "Under udvikling" : app.Status;
        IconPath = string.IsNullOrWhiteSpace(app.IconPath) ? "⬡" : app.IconPath;
        _statusMessage = $"{Name} er registreret i app-kataloget og vises som Nexus-native placeholder.";

        CloseCommand = new RelayCommand(_ => CloseRequested?.Invoke(this, EventArgs.Empty));
        OpenDiagnosticsCommand = new RelayCommand(_ => OpenDiagnosticsRequested?.Invoke(this, EventArgs.Empty));
        OpenAppHubCommand = new RelayCommand(_ => OpenAppHubRequested?.Invoke(this, EventArgs.Empty));
        ReloadCommand = new RelayCommand(_ =>
        {
            StatusMessage = "App-kataloget er kontrolleret. Der er endnu ikke fuldt indhold til denne app.";
        });
    }

    public event PropertyChangedEventHandler? PropertyChanged;
    public event EventHandler? CloseRequested;
    public event EventHandler? OpenDiagnosticsRequested;
    public event EventHandler? OpenAppHubRequested;

    public string AppId { get; }
    public string Name { get; }
    public string Category { get; }
    public string Description { get; }
    public string Status { get; }
    public string IconPath { get; }
    public string StatusSubtitle => $"{Status} · Nexus placeholder";

    public string StatusMessage
    {
        get => _statusMessage;
        private set
        {
            _statusMessage = value;
            OnPropertyChanged();
        }
    }

    public ICommand CloseCommand { get; }
    public ICommand OpenDiagnosticsCommand { get; }
    public ICommand OpenAppHubCommand { get; }
    public ICommand ReloadCommand { get; }

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