ファイル

105 行
3.2 KiB
C#

using YukkuriMovieMaker.Commons;
namespace UndoTree;
internal enum UndoTreePlanKind
{
Change,
Same,
Conflict,
Merged,
Missing,
}
internal sealed record UndoTreePlanChoice(ConflictPolicy Policy, string Text);
internal sealed class UndoTreePlanItem : Bindable
{
readonly string setText;
readonly string? offsetText;
public UndoTreePlanItem(UndoTreePlanKind kind, UndoTreeChangeKey key, string targetLabel, string member, int count, string currentText, string setText, string? offsetText, ConflictPolicy choice)
{
Kind = kind;
Key = key;
TargetLabel = targetLabel;
Member = member;
Count = count;
CurrentText = currentText;
this.setText = setText;
this.offsetText = offsetText;
Choice = choice;
}
public UndoTreePlanKind Kind { get; }
public UndoTreeChangeKey Key { get; }
public string TargetLabel { get; }
public string Member { get; }
public int Count { get; }
public string CurrentText { get; }
public bool IsConflict => Kind == UndoTreePlanKind.Conflict;
public bool CanOffset => offsetText is not null;
public string Text => Count > 1 ? string.Format(Texts.FoldedRun, UndoTreeText.Join(TargetLabel, Member), Count) : UndoTreeText.Join(TargetLabel, Member);
public ConflictPolicy Choice { get; set => Set(ref field, value, nameof(Choice), nameof(ResultText)); }
public IReadOnlyList<UndoTreePlanChoice> Choices => [
new(ConflictPolicy.Skip, Texts.ConflictSkip),
new(ConflictPolicy.Apply, Texts.ConflictApply),
.. CanOffset ? [new UndoTreePlanChoice(ConflictPolicy.Offset, Texts.ConflictOffset)] : Array.Empty<UndoTreePlanChoice>(),
];
public string ResultText => Kind switch
{
UndoTreePlanKind.Conflict => Choice switch
{
ConflictPolicy.Apply => setText,
ConflictPolicy.Offset => offsetText ?? CurrentText,
_ => CurrentText,
},
UndoTreePlanKind.Same => CurrentText,
UndoTreePlanKind.Missing => string.Empty,
_ => setText,
};
public string StatusText => Kind switch
{
UndoTreePlanKind.Change => Texts.ReviewChange,
UndoTreePlanKind.Same => Texts.ReviewSame,
UndoTreePlanKind.Merged => Texts.ReviewMerged,
UndoTreePlanKind.Missing => Texts.ReviewMissing,
_ => string.Empty,
};
public void ChooseAtMost(ConflictPolicy choice) => Choice = choice == ConflictPolicy.Offset && !CanOffset ? ConflictPolicy.Skip : choice;
}
internal sealed class UndoTreePlan(IReadOnlyList<UndoTreePlanItem> items)
{
public IReadOnlyList<UndoTreePlanItem> Items { get; } = items;
public int ConflictCount => Items.Count(item => item.IsConflict);
public int MissingCount => Items.Count(item => item.Kind == UndoTreePlanKind.Missing);
public bool NeedsReview => ConflictCount > 0 || MissingCount > 0;
public IReadOnlyDictionary<UndoTreeChangeKey, ConflictPolicy> Decisions => Items
.Where(item => item.IsConflict)
.ToDictionary(item => item.Key, item => item.Choice);
public void ChooseAll(ConflictPolicy choice)
{
foreach (var item in Items.Where(item => item.IsConflict))
item.ChooseAtMost(choice);
}
}