namespace HistoryView.ViewModels.Regions.SettingSubs;

internal partial class OtherSettingsViewModel : ObservableObject
{
    public OtherSettingsViewModel(LanguageLoader languageLoader, DatabaseCleaner databaseCleaner, MonitorHelper monitor, UserInformation user, IEventAggregator eventAggregator, HubSender sender, MessageBoxHelper messageBoxHelper)
    {
        this.Language = languageLoader;
        this.Monitor = monitor;
        this.eventAggregator = eventAggregator;
        this.UserInfo = user;
        this._sender = sender;
        this.DatabaseCleaner = databaseCleaner;
        this._messageBoxHelper = messageBoxHelper;
    }
    private readonly IEventAggregator eventAggregator;
    private readonly MessageBoxHelper _messageBoxHelper;
    private readonly HubSender _sender;


    [ObservableProperty]
    private DatabaseCleaner _DatabaseCleaner;

    [ObservableProperty]
    private MonitorHelper _Monitor;

    [ObservableProperty]
    private LanguageLoader _Language;

    [ObservableProperty]
    private UserInformation? _UserInfo;

    [RelayCommand]
    private void DisplaySelect(int para)
    {
        if (para == this.Monitor.SelectedDisplay)
            return;

        this.Monitor.SelectedDisplay = para;
        MainWindow.DefaultRegion = "Setting";

        if (BaseConfigFileLoader.Load(out BasicInfo? basicInfo) && basicInfo is not null)
        {
            basicInfo.SelectedDisplay = para;
            BaseConfigFileLoader.Save(basicInfo);
        }

        eventAggregator.GetEvent<WindowSwitch>().Page = Pages.MainWindow;
        eventAggregator.GetEvent<WindowSwitch>().Publish();
    }

    [RelayCommand]
    private async Task SwitchDataBase(string para)
    {
        if (this._sender is null)
            return;

        switch (para)
        {
            case "Reset":
                if (!_sender.FullyReset())
                {
                    await _messageBoxHelper.ShowAsync("Failed");
                    return;
                }
                await _messageBoxHelper.ShowAsync("Success");
                break;

            case "SwitchDataBase":
                if (!_sender.SwitchDataBase(out string? dbName) || string.IsNullOrEmpty(dbName))
                {
                    await _messageBoxHelper.ShowAsync("Failed");
                    return;
                }

                await _messageBoxHelper.ShowAsync($"Switch to new database - {dbName}");
                break;

            case "Delete":
                this._sender.ClearDataBase().Wait();

                break;
            case "Range":
                this._sender.UpdateDataBaseRange((int)DatabaseCleaner.KeepRange).Wait();
                break;

            default:
                break;
        }

    }
}
