namespace HistoryView.ViewModels;
internal partial class StatusViewModel : ObservableObject
{
    public StatusViewModel(IEventAggregator eventAggregator,
        IDialogService dialogService,
        IRegionManager regionManager,
        HubSender hubSender,
        LanguageLoader language,
        MessageBoxHelper messageBox,
        Alarms alarms,
        Hardwares hardwares,
        UserInformation userInfo)
    {
        this._sender = hubSender;
        this._messageBox = messageBox;
        this._regionManager = regionManager;
        this._dialogService = dialogService;
        this._eventAggregator = eventAggregator;

        this.Alarms = alarms;
        this.Hardwares = hardwares;
        this.User = userInfo;
        this.Language = language;
    }

    private readonly IEventAggregator _eventAggregator;
    private readonly IDialogService _dialogService;
    private readonly IRegionManager _regionManager;
    private readonly MessageBoxHelper _messageBox;
    private readonly HubSender _sender;

    [ObservableProperty]
    private Hardwares _hardwares;

    [ObservableProperty]
    private Alarms _Alarms;

    [ObservableProperty]
    private LanguageLoader _language;

    [ObservableProperty]
    private UserInformation _User;

    [ObservableProperty]
    private bool _popUser;

    [ObservableProperty]
    private bool _popLanguage;

    [ObservableProperty]
    private bool _popWarning;


    [RelayCommand]
    private void Exit()
    {
        if (this._messageBox.ShowAsync((string)App.Current.FindResource("ExitConfirm"), MessageBoxButton.YesNo, MessageBoxImage.Question).Result.Result == ButtonResult.Yes)
        {
            this._sender.RequestShutdown();

            foreach (Process p in Process.GetProcessesByName("MinicsConsole"))
                p.Kill();

            App.Current.Shutdown();
        }
    }

    [RelayCommand]
    public void Switch(string name) => _regionManager.Regions["ModuleContent"].RequestNavigate(name);

    [RelayCommand]
    private void Setting()
    {
        _dialogService.ShowDialog("Setting", new DialogParameters());
    }

    [RelayCommand]
    private void SwitchUser()
    {
        _eventAggregator.GetEvent<WindowSwitch>().Page = Pages.Login;
        _eventAggregator.GetEvent<WindowSwitch>().Publish();
    }

    [RelayCommand]
    private void UserOperation()
    {
        this.PopUser = true;
    }

    [RelayCommand]
    private void LanguageOperation(object para)
    {
        if (para is null)
        {
            this.PopLanguage = true;
            return;
        }

        this.Language.SelectedLanguage = para;
        this.PopLanguage = false;
    }
}