using System.Windows.Controls;
using ClaudeDo.Installer.Core;
using ClaudeDo.Installer.Localization;
using CommunityToolkit.Mvvm.ComponentModel;

namespace ClaudeDo.Installer.Pages.UiSettingsPage;

public partial class UiSettingsPageViewModel : ObservableObject, IInstallerPage
{
    private readonly InstallContext _context;
    private UiSettingsPageView? _view;

    public string Title => TrExtension.Localizer?["installer.uiSettings.title"] ?? "UI Settings";
    public string Icon => "\uE771";
    public int Order => 4;
    public bool ShowInWizard => true;
    public bool ShowInSettings => true;
    public UserControl View => _view ??= new UiSettingsPageView { DataContext = this };

    [ObservableProperty] private string _signalRUrl = "http://127.0.0.1:47821/hub";
    [ObservableProperty] private string _uiDbPath = "~/.claudeDo/todo.db";
    [ObservableProperty] private bool _isSynced = true;
    [ObservableProperty] private string? _validationError;

    public UiSettingsPageViewModel(InstallContext context) => _context = context;

    partial void OnIsSyncedChanged(bool value)
    {
        if (value) SyncFromContext();
    }

    private void SyncFromContext()
    {
        SignalRUrl = $"http://127.0.0.1:{_context.SignalRPort}/hub";
        UiDbPath = _context.DbPath;
    }

    public Task LoadAsync()
    {
        if (IsSynced)
        {
            SyncFromContext();
        }
        else
        {
            var cfg = InstallerAppSettings.Load();
            SignalRUrl = cfg.SignalRUrl;
            UiDbPath = cfg.DbPath;
        }
        return Task.CompletedTask;
    }

    public Task ApplyAsync()
    {
        if (IsSynced) SyncFromContext();
        _context.SignalRUrl = SignalRUrl;
        _context.UiDbPath = UiDbPath;
        return Task.CompletedTask;
    }

    public bool Validate()
    {
        if (string.IsNullOrWhiteSpace(SignalRUrl))
        {
            ValidationError = "SignalR URL is required.";
            return false;
        }

        if (string.IsNullOrWhiteSpace(UiDbPath))
        {
            ValidationError = "Database path is required.";
            return false;
        }

        if (!Uri.TryCreate(SignalRUrl, UriKind.Absolute, out _))
        {
            ValidationError = "SignalR URL must be a valid URL.";
            return false;
        }

        ValidationError = null;
        return true;
    }
}
