using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using static PKHeX.Core.MessageStrings;
namespace PKHeX.Core
{
///
/// Carries out a batch edit and contains information summarizing the results.
///
public sealed class BatchEditor
{
private int Modified { get; set; }
private int Iterated { get; set; }
private int Errored { get; set; }
///
/// Tries to modify the .
///
/// Object to modify.
/// Filters which must be satisfied prior to any modifications being made.
/// Modifications to perform on the .
/// Result of the attempted modification.
public bool ProcessPKM(PKM pkm, IEnumerable filters, IEnumerable modifications)
{
if (pkm.Species <= 0)
return false;
if (!pkm.Valid || pkm.Locked)
{
Iterated++;
var reason = pkm.Locked ? "Locked." : "Not Valid.";
Debug.WriteLine($"{MsgBEModifyFailBlocked} {reason}");
return false;
}
var result = BatchEditing.TryModifyPKM(pkm, filters, modifications);
if (result != ModifyResult.Invalid)
Iterated++;
if (result == ModifyResult.Error)
Errored++;
if (result != ModifyResult.Modified)
return false;
pkm.RefreshChecksum();
Modified++;
return true;
}
///
/// Gets a message indicating the overall result of all modifications performed across multiple Batch Edit jobs.
///
/// Collection of modifications.
/// Friendly (multi-line) string indicating the result of the batch edits.
public string GetEditorResults(ICollection sets)
{
if (sets.Count == 0)
return MsgBEInstructionNone;
int ctr = Modified / sets.Count;
int len = Iterated / sets.Count;
string maybe = sets.Count == 1 ? string.Empty : "~";
string result = string.Format(MsgBEModifySuccess, maybe, ctr, len);
if (Errored > 0)
result += Environment.NewLine + maybe + string.Format(MsgBEModifyFailError, Errored);
return result;
}
public static BatchEditor Execute(IList lines, IEnumerable data)
{
var editor = new BatchEditor();
var sets = StringInstructionSet.GetBatchSets(lines).ToArray();
foreach (var pk in data)
{
foreach (var set in sets)
editor.ProcessPKM(pk, set.Filters, set.Instructions);
}
return editor;
}
}
}