JD2022-TU1/main/tools/modules/JD.Guinness.WPF.Module/ViewModel/ChallengeViewModel.cs

106 lines
5.1 KiB
C#

using System;
using System.Collections;
using System.Linq;
using System.Windows;
using JD.Guinness.WPF.Module.Model;
using JD.Guinness.WPF.Module.Model.Ps4;
using JD.Guinness.WPF.Module.Model.Ps5;
using JD.Guinness.WPF.Module.Model.Stadia;
using JD.Guinness.WPF.Module.Model.XBoxOne;
using JD.Guinness.WPF.Module.Model.XBoxSeriesX;
namespace JD.Guinness.WPF.Module.ViewModel
{
public sealed class ChallengeViewModel
{
public DelegateCommand AddChallengeCommand { get; }
public DelegateCommand<IList> DuplicateChallengeCommand { get; }
public DelegateCommand<IList> RemoveChallengeCommand { get; }
public DelegateCommand<Challenge> TogglePs4TropyCommand { get; }
public DelegateCommand<Challenge> TogglePs5TropyCommand { get; }
public DelegateCommand<Challenge> ToggleXBoxOneAchievementCommand { get; }
public DelegateCommand<Challenge> ToggleXBoxSeriesXAchievementCommand { get; }
public DelegateCommand<Challenge> ToggleStadiaAchievementCommand { get; }
public DelegateCommand GenerateCommand { get; }
public GameViewModel Game { get; }
public ChallengeViewModel(GameViewModel gameViewModel)
{
Game = gameViewModel ?? throw new ArgumentNullException(nameof(gameViewModel));
AddChallengeCommand = new DelegateCommand(OnAddChallenge, () => Game.IsAvailable);
DuplicateChallengeCommand = new DelegateCommand<IList>(OnDuplicateChallenge, c => Game.IsAvailable && c.Count > 0);
RemoveChallengeCommand = new DelegateCommand<IList>(OnRemoveChallenge, c => Game.IsAvailable && c.Count > 0);
TogglePs4TropyCommand = new DelegateCommand<Challenge>(OnTogglePlatformChallenge<Ps4Trophy>, _ => Game.IsAvailable);
TogglePs5TropyCommand = new DelegateCommand<Challenge>(OnTogglePlatformChallenge<Ps5Trophy>, _ => Game.IsAvailable);
ToggleXBoxOneAchievementCommand = new DelegateCommand<Challenge>(OnTogglePlatformChallenge<XBoxOneAchievement>, _ => Game.IsAvailable);
ToggleXBoxSeriesXAchievementCommand = new DelegateCommand<Challenge>(OnTogglePlatformChallenge<XBoxSeriesXAchievement>, _ => Game.IsAvailable);
ToggleStadiaAchievementCommand = new DelegateCommand<Challenge>(OnTogglePlatformChallenge<StadiaAchievement>, _ => Game.IsAvailable);
GenerateCommand = new DelegateCommand(OnGenerate, () => Game.IsAvailable);
}
private void OnAddChallenge() => Game.Challenges.Add(new Challenge());
private void OnDuplicateChallenge(IList challenges)
{
foreach (Challenge challenge in challenges.OfType<Challenge>())
{
Challenge newChallenge = new Challenge
{
Name = challenge.Name,
Objective = challenge.Objective,
};
newChallenge.CustomData.Add(challenge.CustomData.Values);
challenge.GetPlatformChallenge<Ps4Trophy>()?.DuplicateInto(newChallenge);
challenge.GetPlatformChallenge<Ps5Trophy>()?.DuplicateInto(newChallenge);
challenge.GetPlatformChallenge<XBoxOneAchievement>()?.DuplicateInto(newChallenge);
challenge.GetPlatformChallenge<XBoxSeriesXAchievement>()?.DuplicateInto(newChallenge);
challenge.GetPlatformChallenge<StadiaAchievement>()?.DuplicateInto(newChallenge);
Game.Challenges.Add(newChallenge);
}
}
private void OnRemoveChallenge(IList challenges)
{
if (MessageBox.Show("Are you sure you want to remove those challenges?", "Remove challenge", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
foreach (Challenge challenge in challenges.OfType<Challenge>().ToList())
{
Game.Challenges.Remove(challenge);
}
}
}
private void OnTogglePlatformChallenge<T>(Challenge challenge)
where T : APlatformChallenge
{
if (challenge.GetPlatformChallenge<T>() is null)
{
challenge.GetOrAddPlatformChallenge<T>();
}
else if (MessageBox.Show("Are you sure you want to remove this platform data?", "Remove platform data", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
challenge.GetPlatformChallenge<T>().Remove();
}
else
{
challenge.NotifyPlatformChallenges();
}
}
private async void OnGenerate()
{
await Game.GeneratePlatforms(new[]
{
Game.Ps4Configuration.Generate ? typeof(Ps4Trophy) : null,
Game.Ps5Configuration.Generate ? typeof(Ps5Trophy) : null,
Game.XBoxOneConfiguration.Generate ? typeof(XBoxOneAchievement) : null,
Game.XBoxSeriesXConfiguration.Generate ? typeof(XBoxSeriesXAchievement) : null,
Game.StadiaConfiguration.Generate ? typeof(StadiaAchievement) : null,
}).ConfigureAwait(true);
}
}
}