Files @ 8ee5f618b07c
Branch filter:

Location: ATITD-Tools/Desert-Paint-Codex/ViewModels/ReactionTestViewModel.cs

Malkyne
Merge
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using DesertPaintCodex.Models;
using DesertPaintCodex.Services;
using DesertPaintCodex.Util;
using ReactiveUI;

namespace DesertPaintCodex.ViewModels
{
    public class ReactionTestViewModel : ViewModelBase
    {
        private ReactionTest _reactionTest = Constants.StubReactionTest;
        public ReactionTest ReactionTest
        {
            get => _reactionTest;
            set => this.RaiseAndSetIfChanged(ref _reactionTest, value);
        }

        private readonly List<Reagent> _allPigmentList = new();
        private readonly List<Reagent> _allReagentList = new();
        public ObservableCollection<Reagent> FirstBufferList { get; } = new();
        public ObservableCollection<Reagent> LastBufferList { get; } = new();

        public ReactionTestViewModel()
        {
            List<string> reagentNames = ReagentService.Names;
            
            foreach (var reagent in reagentNames.Select(ReagentService.GetReagent))
            {
                if (!reagent.IsCatalyst)
                { 
                    _allPigmentList.Add(reagent);
                }
                _allReagentList.Add(reagent);
            }

            this.WhenAnyValue(x => x.ReactionTest)
                .Subscribe(_ => UpdateDerivedState());
            
            ShowScreenSettingsDialog = new Interaction<ScreenSettingsViewModel, Unit>();
            SaveReaction = ReactiveCommand.Create(() => ReactionTest.SaveReaction());
            ClearReaction = ReactiveCommand.Create(() => ReactionTest.ClearReaction());
            FinalizeTestResults = ReactiveCommand.Create(Test);
        }

        private void UpdateDerivedState()
        {
            // There are more "reactive" ways to pull this off, but I don't have time to figure out all those recipes
            // right now.
            SetupTest();
        }
            
        
        private void SetupTest()
        {
            if (ReactionTest.Requires3Way)
            {
                FilterBufferPigments();
            }
        }

        private void FilterBufferPigments()
        {
            // Avalonia doesn't really have proper filtering for listy controls yet.
            PlayerProfile? profile = ProfileManager.CurrentProfile;
            if (profile == null) return;
            ReactionSet reactions = profile.Reactions;
            List<Reagent> bufferList = ReactionTest.IsAllCatalysts ? _allPigmentList : _allReagentList;
            foreach (Reagent buffer in bufferList)
            {
                if (buffer == ReactionTest.Reagent1) continue;
                if (buffer == ReactionTest.Reagent2) continue;


                if ((reactions.Find(buffer, ReactionTest.Reagent1) != null)
                    && (reactions.Find(buffer, ReactionTest.Reagent2) != null))
                {
                    FirstBufferList.Add(buffer);
                }

                if ((reactions.Find(ReactionTest.Reagent1, buffer) != null)
                    && (reactions.Find(ReactionTest.Reagent2, buffer) != null))
                {
                    LastBufferList.Add(buffer);
                }
            }

            // BufferPigmentList.AddRange(_allPigmentList.Where(pigment =>
            //         pigment != ReactionTest.Reagent1 && pigment != ReactionTest.Reagent2));
        }

        public async void Analyze()
        {
            Debug.WriteLine("Analyze");
            try
            {
                await ReactionTest.StartScan();
                await FinalizeTestResults.Execute();
            }
            catch (OperationCanceledException)
            {
                Debug.WriteLine("Scan canceled.");
            }
        }

        public void MarkInert()
        {
            ReactionTest.MarkInert();
        }

        public void CancelScan()
        {
            ReactionTest.CancelScan();
        }

        public async Task OpenScreenSettings()
        {
            await ShowScreenSettingsDialog.Handle(new ScreenSettingsViewModel());
        }

        public async Task ShowClipInfo()
        {
            await ShowInfoBox("Clipped Reactions", "The Pigment Lab is only capable of displaying color channel values in the 0-255 range. However, sometimes, your reactions will push one or more channels outside of that range. When that happens, the value will be clamped either to 0 or 255. In this case, we say that the reaction was \"Clipped.\"\n\nThis is nothing to panic about, though. We solve this issue by using a third (buffer) reagent to offset the extreme value, so that it falls within measurable range, similar to how we test Catalyst+Catalyst reactions. Desert Paint Codex will automatically do the math for this, but it does move clipped reactions towards the end of your test list, because you'll want any reactions between the buffer reagent and your two test reagents already known, prior to running your buffered test.");
        }
        
        public void UseFirstBuffer()
        {
            ReactionTest.UseFirstBuffer = true;
        }

        public void UseLastBuffer()
        {
            ReactionTest.UseFirstBuffer = false;
        }

        private void Test()
        {
            Debug.WriteLine("Test complete");
        }
        
        public Interaction<ScreenSettingsViewModel, Unit> ShowScreenSettingsDialog { get; }

        public ReactiveCommand<Unit, Unit> ClearReaction { get; }
        public ReactiveCommand<Unit, Unit> SaveReaction { get; }
        public ReactiveCommand<Unit, Unit> FinalizeTestResults { get; }
    }
}