using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace NexusShell.Models.Browser;

public sealed class BrowserTab : INotifyPropertyChanged
{
    private string _title = "Ny fane";
    private string _addressText = string.Empty;
    private string _currentUrl = NexusBrowserConstants.HomeUrl;
    private BrowserSecurityState _securityState = BrowserSecurityState.Local;
    private bool _isLoading;
    private bool _isActive;
    private bool _canGoBack;
    private bool _canGoForward;
    private string? _blockedMessage;

    public BrowserTab(Guid id)
    {
        Id = id;
    }

    public Guid Id { get; }

    public string Title
    {
        get => _title;
        set { _title = value; OnPropertyChanged(); }
    }

    public string AddressText
    {
        get => _addressText;
        set { _addressText = value; OnPropertyChanged(); }
    }

    public string CurrentUrl
    {
        get => _currentUrl;
        set { _currentUrl = value; OnPropertyChanged(); }
    }

    public BrowserSecurityState SecurityState
    {
        get => _securityState;
        set { _securityState = value; OnPropertyChanged(); OnPropertyChanged(nameof(SecurityLabel)); }
    }

    public bool IsLoading
    {
        get => _isLoading;
        set
        {
            _isLoading = value;
            OnPropertyChanged();
            OnPropertyChanged(nameof(SecurityLabel));
        }
    }

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

    public bool CanGoBack
    {
        get => _canGoBack;
        set { _canGoBack = value; OnPropertyChanged(); }
    }

    public bool CanGoForward
    {
        get => _canGoForward;
        set { _canGoForward = value; OnPropertyChanged(); }
    }

    public string? BlockedMessage
    {
        get => _blockedMessage;
        set { _blockedMessage = value; OnPropertyChanged(); OnPropertyChanged(nameof(HasBlockedMessage)); }
    }

    public bool HasBlockedMessage => !string.IsNullOrWhiteSpace(BlockedMessage);

    public string SecurityLabel => IsLoading
        ? "Indlæser"
        : SecurityState switch
        {
            BrowserSecurityState.Secure => "Sikker",
            BrowserSecurityState.NotSecure => "Ikke sikker",
            BrowserSecurityState.Local => "Lokal",
            BrowserSecurityState.Loading => "Indlæser",
            _ => "Ukendt",
        };

    public event PropertyChangedEventHandler? PropertyChanged;

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

public enum BrowserSecurityState
{
    Secure,
    NotSecure,
    Local,
    Loading,
}

public static class NexusBrowserConstants
{
    public const string HomeUrl = "nexus:home";
    public const string BlockedMessage = "Denne adresse er blokeret af Nexus.";
    public const string DownloadsPlaceholder = "Downloads kommer i næste version.";
}
