using System;
using System.Collections.Generic;
namespace PKHeX.Core;
///
/// Clears contents of boxes by deleting all but the first duplicate detected.
///
/// Base type of the "is duplicate" hash for the duplicate detection.
public sealed class BoxManipClearDuplicate : BoxManipBase
{
private readonly HashSet HashSet = new();
private readonly Func Criteria;
public BoxManipClearDuplicate(BoxManipType type, Func criteria) : this(type, criteria, _ => true) { }
public BoxManipClearDuplicate(BoxManipType type, Func criteria, Func usable) : base(type, usable)
{
Criteria = pk =>
{
var result = criteria(pk);
if (HashSet.Contains(result))
return true;
HashSet.Add(result);
return false;
};
}
public override string GetPrompt(bool all) => all ? MessageStrings.MsgSaveBoxClearAll : MessageStrings.MsgSaveBoxClearCurrent;
public override string GetFail(bool all) => all ? MessageStrings.MsgSaveBoxClearAllFailBattle : MessageStrings.MsgSaveBoxClearCurrentFailBattle;
public override string GetSuccess(bool all) => all ? MessageStrings.MsgSaveBoxClearAllSuccess : MessageStrings.MsgSaveBoxClearCurrentSuccess;
public override int Execute(SaveFile sav, BoxManipParam param)
{
HashSet.Clear();
bool Method(PKM p) => param.Reverse ^ Criteria(p);
return sav.ClearBoxes(param.Start, param.Stop, Method);
}
}