aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_fomod_plus/installer/ui/FomodViewModel.cpp
blob: b11674c4f487e06a180b22f79aa0168f5a668730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#include "FomodViewModel.h"
#include "xml/ModuleConfiguration.h"
#include "lib/Logger.h"

using GroupCallback  = std::function<void(GroupRef)>;
using PluginCallback = std::function<void(GroupRef, PluginRef)>;


/*
--------------------------------------------------------------------------------
                               Helpers
--------------------------------------------------------------------------------
*/
#pragma region Helpers

bool isRadioLike(GroupRef group)
{
    return group->getType() == SelectExactlyOne
        || (group->getType() == SelectAtMostOne && group->getPlugins().size() > 1);
}

bool moreThanOneSelected(GroupRef group)
{
    auto selectedPlugins = group->getPlugins() | std::views::filter([](const auto& plugin) {
        return plugin->isSelected();
    });
    return std::ranges::distance(selectedPlugins) > 1;
}

bool anySelected(GroupRef group)
{
    return std::ranges::any_of(group->getPlugins(), [](const auto& plugin) { return plugin->isSelected(); });
}

std::string pluginTypeEnumToString(const PluginTypeEnum type)
{
    switch (type) {
    case PluginTypeEnum::Recommended:
        return "Recommended";
    case PluginTypeEnum::Required:
        return "Required";
    case PluginTypeEnum::Optional:
        return "Optional";
    case PluginTypeEnum::NotUsable:
        return "NotUsable";
    case PluginTypeEnum::CouldBeUsable:
        return "CouldBeUsable";
    default:
        return "Unknown";
    }
}

#pragma endregion

/*
--------------------------------------------------------------------------------
                               Lifecycle
--------------------------------------------------------------------------------
*/
#pragma region ViewModel Lifecycle

/**
 * @brief FomodViewModel constructor
 *
 * @note DO NOT USE DIRECTLY. We should only use FomodViewModel::create() to create a new instance.
 * @see FomodViewModel::create
 *
 * @param organizer The organizer instance passed from the IInstaller
 * @param fomodFile The ModuleConfiguration instance created from the raw ModuleConfiguration.xml file
 * @param infoFile  The FomodInfoFile instance created from the raw info.xml file
 *
 * @return A FomodViewModel instance
 */
FomodViewModel::FomodViewModel(MOBase::IOrganizer* organizer,
    std::unique_ptr<ModuleConfiguration> fomodFile,
    std::unique_ptr<FomodInfoFile> infoFile)
    : mOrganizer(organizer), mFomodFile(std::move(fomodFile)), mInfoFile(std::move(infoFile)),
      mConditionTester(organizer),
      mInfoViewModel(std::make_shared<InfoViewModel>(mInfoFile))
{
    mFlags = std::make_shared<FlagMap>();
}

/**
 *
 * @param organizer The organizer instance passed from the IInstaller
 * @param fomodFile The ModuleConfiguration instance created from the raw ModuleConfiguration.xml file
 * @param infoFile  The FomodInfoFile instance created from the raw info.xml file
 * @return A shared pointer to the FomodViewModel instance
 */
std::shared_ptr<FomodViewModel> FomodViewModel::create(MOBase::IOrganizer* organizer,
    std::unique_ptr<ModuleConfiguration> fomodFile,
    std::unique_ptr<FomodInfoFile> infoFile)
{
    auto viewModel = std::make_shared<FomodViewModel>(organizer, std::move(fomodFile), std::move(infoFile));
    if (viewModel->mFlags == nullptr) {
        viewModel->mFlags = std::make_shared<FlagMap>();
    }
    viewModel->createStepViewModels();
    
    // Handle FOMODs with no steps
    if (viewModel->mSteps.empty()) {
        viewModel->mInitialized = true;
        viewModel->logMessage(INFO, "FOMOD with no steps - initialization complete");
        return viewModel;
    }
    
    viewModel->processPluginConditions(-1); // please dont judge me. ill fix this someday.
    viewModel->enforceGroupConstraints();
    viewModel->updateVisibleSteps();
    viewModel->mInitialized      = true;
    viewModel->mCurrentStepIndex = viewModel->mVisibleStepIndices.front();
    viewModel->mActiveStep       = viewModel->mSteps.at(viewModel->mVisibleStepIndices.front());
    viewModel->mActivePlugin     = viewModel->getFirstPluginForActiveStep();
    viewModel->getActiveStep()->setVisited(true);
    viewModel->logMessage(DEBUG, "VIEWMODEL INITIALIZED");
    viewModel->logMessage(DEBUG, viewModel->toString());
    return viewModel;
}
#pragma endregion

/*
--------------------------------------------------------------------------------
                               Traversal Functions
--------------------------------------------------------------------------------
*/
#pragma region Traversal Functions

void FomodViewModel::forEachGroup(const GroupCallback& callback) const
{
    for (const auto& stepViewModel : mSteps) {
        for (const auto& groupViewModel : stepViewModel->getGroups()) {
            callback(groupViewModel);
        }
    }
}

void FomodViewModel::forEachPlugin(const PluginCallback& callback) const
{
    for (const auto& stepViewModel : mSteps) {
        for (const auto& groupViewModel : stepViewModel->getGroups()) {
            for (const auto& pluginViewModel : groupViewModel->getPlugins()) {
                callback(groupViewModel, pluginViewModel);
            }
        }
    }
}

void FomodViewModel::forEachFuturePlugin(const int fromStepIndex, const PluginCallback& callback) const
{
    for (int i = fromStepIndex + 1; i < mSteps.size(); ++i) {
        for (const auto& groupViewModel : mSteps[i]->getGroups()) {
            for (const auto& pluginViewModel : groupViewModel->getPlugins()) {
                callback(groupViewModel, pluginViewModel);
            }
        }
    }
}
#pragma endregion

/*
--------------------------------------------------------------------------------
                               Initialization
--------------------------------------------------------------------------------
*/
#pragma region Initializers
void FomodViewModel::createStepViewModels()
{
    shared_ptr_list<StepViewModel> stepViewModels;

    // Handle legacy FOMODs with no install steps
    if (mFomodFile->installSteps.installSteps.empty()) {
        logMessage(INFO, "No install steps found - creating default step for legacy FOMOD");
        return;
    }

    for (int stepIndex = 0; stepIndex < mFomodFile->installSteps.installSteps.size(); ++stepIndex) {
        const auto& installStep = mFomodFile->installSteps.installSteps[stepIndex];
        shared_ptr_list<GroupViewModel> groupViewModels;

        for (int groupIndex = 0; groupIndex < installStep.optionalFileGroups.groups.size(); ++groupIndex) {
            const auto& group = installStep.optionalFileGroups.groups[groupIndex];
            shared_ptr_list<PluginViewModel> pluginViewModels;

            for (int pluginIndex = 0; pluginIndex < group.plugins.plugins.size(); ++pluginIndex) {
                const auto& plugin   = group.plugins.plugins[pluginIndex];
                auto pluginViewModel = std::make_shared<PluginViewModel>(std::make_shared<Plugin>(plugin), false, true,
                    pluginIndex);

                pluginViewModel->setStepIndex(stepIndex);
                pluginViewModel->setGroupIndex(groupIndex);
                pluginViewModels.emplace_back(pluginViewModel); // Assuming default values for selected and enabled
            }
            auto groupViewModel = std::make_shared<GroupViewModel>(std::make_shared<Group>(group), pluginViewModels,
                groupIndex, stepIndex);
            if (groupViewModel->getType() == SelectAtMostOne && groupViewModel->getPlugins().size() > 1) {
                createNonePluginForGroup(groupViewModel);
            }
            groupViewModels.emplace_back(groupViewModel);
        }
        auto stepViewModel = std::make_shared<StepViewModel>(std::make_shared<InstallStep>(installStep),
            std::move(groupViewModels), stepIndex);
        stepViewModels.emplace_back(stepViewModel);

    }
    mSteps = std::move(stepViewModels);
}

void FomodViewModel::createNonePluginForGroup(GroupRef group)
{
    const auto nonePlugin           = std::make_shared<Plugin>();
    nonePlugin->name                = "None";
    nonePlugin->typeDescriptor.type = PluginTypeEnum::Optional;
    const int newIndex              = static_cast<int>(group->getPlugins().size());
    const auto nonePluginViewModel  = std::make_shared<PluginViewModel>(nonePlugin, true, true, newIndex);
    group->addPlugin(nonePluginViewModel);
}
#pragma endregion

/*
--------------------------------------------------------------------------------
                               Group Constraints
--------------------------------------------------------------------------------
*/
#pragma region Group Constraints

void FomodViewModel::enforceRadioGroupConstraints(GroupRef group) const
{
    if (!isRadioLike(group)) {
        return;
    }

    logMessage(INFO, "Enforcing group constraints for group " + group->getName());

    if (group->getType() == SelectExactlyOne && group->getPlugins().size() == 1) {
        logMessage(INFO,
            "Disabling " + group->getPlugins().at(0)->getName() + " because it's the only plugin.");
        group->getPlugins().at(0)->setEnabled(false);
    }

    if (moreThanOneSelected(group)) {
        logMessage(ERR, "More than one plugin is selected in a SelectExactlyOne group. Deselecting all.");
        for (const auto& plugin : group->getPlugins()) {
            plugin->setSelected(false); // don't call toggle here, that'll do the radio stuff.
        }
    }

    if (anySelected(group)) {
        logMessage(INFO, "At least one plugin is selected. Nothing to enforce.");
        return;
    }

    // First, try to select the first Recommended plugin
    for (const auto& plugin : group->getPlugins()) {
        if (mConditionTester.getPluginTypeDescriptorState(plugin->getPlugin(), mFlags) == PluginTypeEnum::Recommended) {
            logMessage(INFO, "Selecting " + plugin->getName() + " because it's the first recommended plugin.");
            togglePlugin(group, plugin, true);
            return;
        }
    }

    // If no Recommended plugin is found, select the first one that isn't NotUsable
    for (const auto& plugin : group->getPlugins()) {
        if (mConditionTester.getPluginTypeDescriptorState(plugin->getPlugin(), mFlags) != PluginTypeEnum::NotUsable) {
            logMessage(INFO, "Selecting " + plugin->getName() + " because it's the first usable plugin.");
            togglePlugin(group, plugin, true);
            return;
        }
    }
}

void FomodViewModel::enforceSelectAllConstraint(GroupRef groupViewModel) const
{
    if (groupViewModel->getType() != SelectAll) {
        return;
    }

    for (const auto& pluginViewModel : groupViewModel->getPlugins()) {
        togglePlugin(groupViewModel, pluginViewModel, true);
        pluginViewModel->setEnabled(false);
    }

}

void FomodViewModel::enforceSelectAtLeastOneConstraint(GroupRef group) const
{
    if (group->getType() != SelectAtLeastOne || group->getPlugins().size() != 1) {
        return;
    }

    const auto plugin = group->getPlugins().front();
    if (mConditionTester.getPluginTypeDescriptorState(plugin->getPlugin(), mFlags) != PluginTypeEnum::NotUsable) {
        logMessage(DEBUG, "Selecting " + plugin->getName() + " because it's the only plugin in a SelectAtLeastOne.");
        togglePlugin(group, plugin, true);
        plugin->setEnabled(false);
    }
}

void FomodViewModel::enforceGroupConstraints() const
{
    forEachGroup([this](const auto& groupViewModel) {
        enforceRadioGroupConstraints(groupViewModel);
        enforceSelectAllConstraint(groupViewModel);
        enforceSelectAtLeastOneConstraint(groupViewModel);
    });
}

#pragma endregion

/*
--------------------------------------------------------------------------------
                               Plugin Constraints
--------------------------------------------------------------------------------
*/
#pragma region Plugin Constraints

void FomodViewModel::processPlugin(GroupRef group, PluginRef plugin) const
{
    if (group->getType() == SelectAll) {
        return;
    }
    const auto typeDescriptor = mConditionTester.getPluginTypeDescriptorState(plugin->plugin, mFlags);

    if (typeDescriptor == plugin->getCurrentPluginType()) {
        return;
    }

    logMessage(DEBUG,
        "Plugin " + plugin->getName() + " in group " + std::to_string(group->getOwnIndex()) + " has changed type from "
        + pluginTypeEnumToString(plugin->getCurrentPluginType()) + " to " + pluginTypeEnumToString(typeDescriptor));

    plugin->setCurrentPluginType(typeDescriptor);

    const bool isOnlyPlugin = group->getPlugins().size() == 1
        && (group->getType() == SelectExactlyOne || group->getType() == SelectAtLeastOne);

    // check if step hasVisited, if it hasn't been, set it to unchecked if it's optional.
    const auto stepNotVisitedYet = !mSteps[group->getStepIndex()]->getHasVisited();

    switch (typeDescriptor) {
    case PluginTypeEnum::Recommended:
        plugin->setEnabled(true);
        if (!plugin->isSelected()) {
            togglePlugin(group, plugin, true);
        }
        break;
    case PluginTypeEnum::Required:
        plugin->setEnabled(false);
        if (!plugin->isSelected()) {
            togglePlugin(group, plugin, true);
        }
        break;
    case PluginTypeEnum::Optional:
        if (!isOnlyPlugin) {
            plugin->setEnabled(true);
        }
    // In the case where we're changing flags to make something optional from Recommended, set it back to unchecked.
        if (plugin->isSelected() & stepNotVisitedYet && group->getType() == SelectAny) {
            togglePlugin(group, plugin, false);
        }
        break;
    case PluginTypeEnum::NotUsable:
        plugin->setEnabled(false);
        if (plugin->isSelected()) {
            togglePlugin(group, plugin, false);
        }
        break;
    case PluginTypeEnum::CouldBeUsable:
        plugin->setEnabled(true);
        break;
    default: ;
    }
}

void FomodViewModel::processPluginConditions(const int fromStepIndex) const
{
    // We only want to update plugins that haven't been seen yet. Otherwise, we could undo manual selections by the user.
    if (fromStepIndex >= 0) {
        logMessage(DEBUG, "Processing plugins from step " + std::to_string(fromStepIndex));
        forEachFuturePlugin(fromStepIndex, [this](const auto& groupViewModel, const auto& pluginViewModel) {
            processPlugin(groupViewModel, pluginViewModel);
        });
    } else {
        forEachPlugin([this](const auto& groupViewModel, const auto& pluginViewModel) {
            processPlugin(groupViewModel, pluginViewModel);
        });
    }
}

void FomodViewModel::setFlagForPluginState(PluginRef plugin) const
{
    if (plugin->isSelected()) {
        mFlags->setFlagsForPlugin(plugin);
    } else {
        mFlags->unsetFlagsForPlugin(plugin);
    }
}

/*
 * In an exclusive group, this gets called for the deselected plugin and then the selected plugin.
 * So if we're unselecting modB to select modA, we will get calls like
 *  togglePlugin(group, modB, false)
 *  togglePlugin(group, modA, true)
 */
bool FomodViewModel::togglePlugin(GroupRef group, PluginRef plugin, const bool selected) const
{
    if (plugin->isSelected() == selected) {
        logMessage(DEBUG, "Plugin " + plugin->getName() + " is already " + (selected ? "selected" : "deselected"));
        return false;
    }

    // Disable other radio options first.
    if (selected && isRadioLike(group)) {
        for (const auto& otherPlugin : group->getPlugins()) {
            if (otherPlugin != plugin && plugin->isSelected()) {
                logMessage(DEBUG,
                    "Deselecting " + otherPlugin->getName() + " because " + plugin->getName() + " was selected.");
                otherPlugin->setSelected(false);
                setFlagForPluginState(otherPlugin);
            }
        }
    }

    const auto stepIndex = group->getStepIndex();

    logMessage(INFO, "Toggling " + plugin->getName() + " to " + (selected ? "true" : "false"));
    plugin->setSelected(selected);
    setFlagForPluginState(plugin);

    if (mInitialized) {
        mActivePlugin = plugin;
    }
    processPluginConditions(stepIndex);
    updateVisibleSteps();
    return true;
}

void FomodViewModel::markManuallySet(PluginRef plugin)
{
    plugin->manuallySet = true;
}

#pragma endregion

/*
--------------------------------------------------------------------------------
                               Step Constraints
--------------------------------------------------------------------------------
*/
#pragma region Step Constraints

void FomodViewModel::updateVisibleSteps() const
{
    mVisibleStepIndices.clear();
    mFlags->clearAll();

    for (int i = 0; i < mSteps.size(); ++i) {
        if (i == 0) {
            rebuildConditionFlagsForStep(i);
        }

        // This also depends on previous flags that may have set this particular flag.
        if (mConditionTester.isStepVisible(mFlags, mSteps[i]->getVisibilityConditions(), i, mSteps)) {
            mVisibleStepIndices.push_back(i);
            rebuildConditionFlagsForStep(i);
        }
    }
    if (mFlags->getFlagCount() > 0) {
        logMessage(DEBUG, mFlags->toString());
    }
}

void FomodViewModel::rebuildConditionFlagsForStep(const int stepIndex) const
{
    for (const auto& group : mSteps[stepIndex]->getGroups()) {
        for (const auto& plugin : group->getPlugins()) {
            setFlagForPluginState(plugin);
        }
    }
}
#pragma endregion

/*
--------------------------------------------------------------------------------
                               Navigation/UI
--------------------------------------------------------------------------------
*/
#pragma region Navigation/UI

void FomodViewModel::stepBack()
{
    if (mSteps.empty()) {
        return;  // No steps to move back to
    }
    
    logMessage(DEBUG, "Stepping back from step " + std::to_string(mCurrentStepIndex));
    const auto it = std::ranges::find(mVisibleStepIndices, mCurrentStepIndex);
    if (it != mVisibleStepIndices.end() && it != mVisibleStepIndices.begin()) {
        mCurrentStepIndex = *std::prev(it);
        mActiveStep       = mSteps[mCurrentStepIndex];
        mActivePlugin     = getFirstPluginForActiveStep();
    }
    logMessage(DEBUG, "Stepped back to step " + std::to_string(mCurrentStepIndex));
}

void FomodViewModel::stepForward()
{
    if (mSteps.empty()) {
        return;  // No steps to move forward to
    }
    
    logMessage(DEBUG, "Stepping forward from step " + std::to_string(mCurrentStepIndex));
    const auto it = std::ranges::find(mVisibleStepIndices, mCurrentStepIndex);
    if (it != mVisibleStepIndices.end() && std::next(it) != mVisibleStepIndices.end()) {
        mCurrentStepIndex = *std::next(it);
        mActiveStep       = mSteps[mCurrentStepIndex];
        mActivePlugin     = getFirstPluginForActiveStep();
    }
    mActiveStep->setVisited(true);
    logMessage(DEBUG, "Stepped forward to step " + std::to_string(mCurrentStepIndex));
}

bool FomodViewModel::isLastVisibleStep() const
{
    if (mSteps.empty()) {
        return true;  // Legacy FOMODs are always "last step"
    }
    return !mVisibleStepIndices.empty() && mCurrentStepIndex == mVisibleStepIndices.back();
}

bool FomodViewModel::isFirstVisibleStep() const
{
    if (mSteps.empty()) {
        return true;  // Legacy FOMODs are always "first step"
    }
    return !mVisibleStepIndices.empty() && mCurrentStepIndex == mVisibleStepIndices.front();
}

void FomodViewModel::preinstall(const std::shared_ptr<MOBase::IFileTree>& tree, const QString& fomodPath)
{
    mFileInstaller = std::make_shared<
        FileInstaller>(mOrganizer, fomodPath, tree, std::move(mFomodFile), mFlags, mSteps);
}


std::string FomodViewModel::getDisplayImage() const
{
    // if the active plugin has an image, return it
    if (mActivePlugin && !mActivePlugin->getImagePath().empty()) {
        return mActivePlugin->getImagePath();
    }
    return mCurrentStepIndex == 0 ? mFomodFile->moduleImage.path : "";
}

std::shared_ptr<PluginViewModel> FomodViewModel::getFirstPluginForActiveStep() const
{
    if (!mActiveStep) {
        return nullptr;
    }

    const auto& groups = mActiveStep->getGroups();
    if (groups.empty()) {
        return nullptr;
    }

    const auto& plugins = groups.front()->getPlugins();
    if (plugins.empty()) {
        return nullptr;
    }

    return plugins.front();
}
#pragma endregion

/*
--------------------------------------------------------------------------------
                               Utility
--------------------------------------------------------------------------------
*/
#pragma region Utility
std::string FomodViewModel::toString() const
{
    std::string viewModel = "\n";
    for (const auto& step : mSteps) {

        const auto isVisible = std::ranges::find(mVisibleStepIndices, step->getOwnIndex()) != mVisibleStepIndices.end();
        viewModel += "Step " + std::to_string(step->getOwnIndex()) + ": " + step->getName() + "[Visible: " +
            std::to_string(isVisible) + "]\n";

        for (const auto& group : step->getGroups()) {

            viewModel += "\tGroup " + std::to_string(group->getOwnIndex()) + ": " + group->getName() + "\n";

            for (const auto& plugin : group->getPlugins()) {
                viewModel += "\t\tPlugin: " + plugin->getName() + "[Selected: " + (plugin->isSelected()
                    ? "TRUE"
                    : "FALSE") + "]\n";
            }
        }
    }
    viewModel += "\n";
    std::ostringstream oss;
    std::ranges::transform(mVisibleStepIndices,
        std::ostream_iterator<std::string>(oss, ", "),
        [](const int i) { return std::to_string(i); });

    std::string stepList = oss.str();
    stepList.erase(stepList.length() - 2);

    viewModel += "Visible Steps: [" + stepList + "]\n";
    viewModel += mFlags->toString();
    return viewModel;
}


void FomodViewModel::resetToDefaults()
{
    logMessage(INFO, "Resetting all choices to author defaults");

    // Clear all flags first
    mFlags->clearAll();

    // Reset all plugins to deselected and clear visited states
    for (const auto& step : mSteps) {
        step->setVisited(false);
        for (const auto& group : step->getGroups()) {
            for (const auto& plugin : group->getPlugins()) {
                plugin->setSelected(false);
                plugin->setEnabled(true);
                plugin->manuallySet = false;
                plugin->setCurrentPluginType(PluginTypeEnum::UNKNOWN);
            }
        }
    }

    // Re-run the initial constraint enforcement to restore author defaults
    processPluginConditions(-1);
    enforceGroupConstraints();
    updateVisibleSteps();

    // Reset to first step
    mCurrentStepIndex = mVisibleStepIndices.empty() ? 0 : mVisibleStepIndices.front();
    mActiveStep       = mSteps.empty() ? nullptr : mSteps.at(mCurrentStepIndex);
    mActivePlugin     = getFirstPluginForActiveStep();
    if (mActiveStep) {
        mActiveStep->setVisited(true);
    }

    logMessage(DEBUG, "Reset complete. Current state:\n" + toString());
}

void FomodViewModel::selectFromJson(nlohmann::json json) const
{
    const auto jsonSteps = json["steps"];
    const auto stepCount = jsonSteps.size();

    for (int stepIndex = 0; stepIndex < stepCount; ++stepIndex) {

        if (stepIndex > mSteps.size() - 1) {
            logMessage(ERR, "Step index " + std::to_string(stepIndex) + " is out of bounds.");
            continue;
        }

        const auto currentStep = mSteps[stepIndex];
        const auto step        = jsonSteps[stepIndex];
        const auto groupCount  = step["groups"].size();

        logMessage(DEBUG, "Selecting plugins for step " + std::to_string(stepIndex));
        logMessage(DEBUG, "There are " + std::to_string(groupCount) + " groups.");

        for (int groupIndex = 0; groupIndex < groupCount; ++groupIndex) {
            if (groupIndex > currentStep->getGroups().size() - 1) {
                logMessage(ERR, "Group index " + std::to_string(groupIndex) + " is out of bounds.");
                continue;
            }

            const auto group        = step["groups"][groupIndex];
            const auto currentGroup = currentStep->getGroups()[groupIndex];

            for (const auto jsonPlugin : group["plugins"]) {

                const auto& allPlugins = currentGroup->getPlugins();
                const auto searchName = jsonPlugin.get<std::string>();

                logMessage(DEBUG, "Looking for plugin " + searchName);

                const auto currentPlugin = std::ranges::find_if(allPlugins,
                    [searchName](PluginRef p) {
                        return p->getName() == searchName;
                    });

                if (currentPlugin == allPlugins.end()) {
                    logMessage(DEBUG, "Plugin " + searchName + " not found in group " + currentGroup->getName());
                    continue;
                }

                if ((*currentPlugin)->isSelected()) {
                    logMessage(DEBUG, "Plugin " + searchName + " is already selected.");
                    continue;
                }
                logMessage(DEBUG, "Toggle plugin " + searchName + " to selected.");
                if (!(*currentPlugin)->isEnabled()) {
                    logMessage(DEBUG, "Plugin " + searchName + " is not enabled.");
                    continue;
                }
                togglePlugin(currentGroup, *currentPlugin, true);
            }

            if (!group.contains("deselected")) {
                continue;
            }

            // Do the opposite of the above. For unchecked plugins, disable them.
            for (const auto jsonPlugin : group["deselected"]) {

                const auto& allPlugins = currentGroup->getPlugins();
                const auto searchName = jsonPlugin.get<std::string>();

                logMessage(DEBUG, "Looking for plugin to disable: " + searchName);

                const auto currentPlugin = std::ranges::find_if(allPlugins,
                    [searchName](PluginRef p) {
                        return p->getName() == searchName;
                    });

                if (currentPlugin == allPlugins.end()) {
                    logMessage(DEBUG, "Plugin " + searchName + " not found in group " + currentGroup->getName());
                    continue;
                }

                if (!(*currentPlugin)->isSelected()) {
                    logMessage(DEBUG, "Plugin " + searchName + " is already deselected.");
                    continue;
                }
                logMessage(DEBUG, "Toggle plugin " + searchName + " to deselected.");
                if (!(*currentPlugin)->isEnabled()) {
                    logMessage(DEBUG, "Plugin " + searchName + " is not enabled.");
                    continue;
                }
                togglePlugin(currentGroup, *currentPlugin, false);
                (*currentPlugin)->manuallySet = true; // To preserve this state when serializing JSON.
            }
        }
    }
}
#pragma endregion