using System.Reactive;
using CommunityToolkit.Mvvm.DependencyInjection;
using FastBill.Models;
using PSCHelpdesk.Shared.Service;
using PSCHelpdesk.Shared.Setting;
using PSCHelpdesk.Shared.ViewModels;
using ReactiveUI;

namespace FastBill.ViewModels;

public class SettingsViewModel: ViewModelBase, IViewModelBase
{
    private string _email;
    private string _apiKey;
    private Settings _settings;

    public ReactiveCommand<Unit, Unit> SaveSettings { get; }
    
    public string EMail
    {
        get => _email;
        set => SetAndRaisePropertyChanged(ref _email, value);
    }
    public string ApiKey
    {
        get => _apiKey;
        set => SetAndRaisePropertyChanged(ref _apiKey, value);
    }
    
    public Settings Settings
    {
        get => _settings;
        set => SetAndRaisePropertyChanged(ref _settings, value);
    }
    
    public SettingsViewModel()
    {
        Settings = new Settings();
        var settingsManager = (SettingsManager)Ioc.Default.GetService<ISettingsManager>();
        var _set = new Settings();
        settingsManager.LoadPluginSettings("FastbillSettings", _set);
        
        Settings.EMail = _set.EMail;
        Settings.ApiKey = _set.ApiKey;

        SaveSettings = ReactiveCommand.Create(saveSettings);
    }

    void saveSettings()
    {
        var settingsManager = (SettingsManager)Ioc.Default.GetService<ISettingsManager>();
        settingsManager.SavePluginSettings("FastbillSettings", Settings);
        
    }
    
}