using BookReader.Models;
using BookReader.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace BookReader.ViewModels;

public partial class SettingsViewModel : BaseViewModel
{
    private readonly ISettingsService _settingsService;
    private readonly ICalibreWebService _calibreWebService;
    private readonly INavigationService _navigationService;

    [ObservableProperty]
    private string _calibreUrl = string.Empty;

    [ObservableProperty]
    private string _calibreUsername = string.Empty;

    [ObservableProperty]
    private string _calibrePassword = string.Empty;

    [ObservableProperty]
    private int _defaultFontSize = Constants.Reader.DefaultFontSize;

    [ObservableProperty]
    private string _defaultFontFamily = "serif";

    [ObservableProperty]
    private string _connectionStatus = string.Empty;

    [ObservableProperty]
    private bool _isConnectionTesting;

    public List<string> AvailableFonts { get; } = new()
    {
        "serif", "sans-serif", "monospace", "Georgia", "Palatino",
        "Times New Roman", "Arial", "Verdana", "Courier New"
    };

    public List<int> AvailableFontSizes { get; } = new()
    {
        12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 36, 40
    };

    public SettingsViewModel(
        ISettingsService settingsService,
        ICalibreWebService calibreWebService,
        INavigationService navigationService)
    {
        _settingsService = settingsService;
        _calibreWebService = calibreWebService;
        _navigationService = navigationService;
        Title = "Settings";
    }

    [RelayCommand]
    public async Task LoadSettingsAsync()
    {
        CalibreUrl = await _settingsService.GetAsync(SettingsKeys.CalibreUrl);
        CalibreUsername = await _settingsService.GetAsync(SettingsKeys.CalibreUsername);
        CalibrePassword = await _settingsService.GetSecurePasswordAsync();
        DefaultFontSize = await _settingsService.GetIntAsync(SettingsKeys.DefaultFontSize, Constants.Reader.DefaultFontSize);
        DefaultFontFamily = await _settingsService.GetAsync(SettingsKeys.DefaultFontFamily, "serif");
    }

    [RelayCommand]
    public async Task SaveSettingsAsync()
    {
        await _settingsService.SetAsync(SettingsKeys.CalibreUrl, CalibreUrl);
        await _settingsService.SetAsync(SettingsKeys.CalibreUsername, CalibreUsername);
        await _settingsService.SetSecurePasswordAsync(CalibrePassword);
        await _settingsService.SetIntAsync(SettingsKeys.DefaultFontSize, DefaultFontSize);
        await _settingsService.SetAsync(SettingsKeys.DefaultFontFamily, DefaultFontFamily);

        if (!string.IsNullOrEmpty(CalibreUrl))
        {
            _calibreWebService.Configure(CalibreUrl, CalibreUsername, CalibrePassword);
        }

        await _navigationService.DisplayAlertAsync("Settings", "Settings saved successfully.", "OK");
    }

    [RelayCommand]
    public async Task TestConnectionAsync()
    {
        if (string.IsNullOrWhiteSpace(CalibreUrl))
        {
            ConnectionStatus = "Please enter a URL";
            return;
        }

        IsConnectionTesting = true;
        ConnectionStatus = "Testing connection...";

        try
        {
            var success = await _calibreWebService.TestConnectionAsync(CalibreUrl, CalibreUsername, CalibrePassword);
            ConnectionStatus = success ? "✅ Connection successful!" : "❌ Connection failed";
        }
        catch (Exception ex)
        {
            ConnectionStatus = $"❌ Error: {ex.Message}";
        }
        finally
        {
            IsConnectionTesting = false;
        }
    }
}