using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;
using Elfisa.Avalonia.ViewModels;
using Elfisa.Core;

namespace Elfisa.Avalonia.Views;

public partial class SettingsWindow : Window
{
    public SettingsWindow() => AvaloniaXamlLoader.Load(this);

    private void OnSave(object? sender, RoutedEventArgs e)
    {
        if (DataContext is SettingsViewModel vm)
        {
            SettingsStore.Current.ApiBaseUrl = string.IsNullOrWhiteSpace(vm.ApiBaseUrl) ? null : vm.ApiBaseUrl.Trim().TrimEnd('/');
            SettingsStore.Current.Theme = vm.ThemeIndex == 1 ? "Dark" : "Light";
            SettingsStore.Current.DefaultLocationId = vm.SelectedLocation?.BuyerLocationId;
            SettingsStore.Save();

            if (Application.Current is { } app)
                app.RequestedThemeVariant = vm.ThemeIndex == 1 ? ThemeVariant.Dark : ThemeVariant.Light;
        }
        Close();
    }

    private void OnCancel(object? sender, RoutedEventArgs e) => Close();
}
