using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Cthangover.Core.Factories.Impls;
using Cthangover.Core.Items;
using Cthangover.Tools.EntityEditor;
namespace Cthangover.Tools.ItemsEditor
{
///
/// Editor of items ("Kind": "item", ): name and
/// description in every language of the mod, price, picture in the
/// items group (the inventory icon), item types and the use action
/// offered from the actions the loaded mods register. Fields of other mods'
/// item C# (heal, verb…) are "other fields" of the base window.
/// New items go to items/<mod>.json.
///
public partial class ItemEditorWindow : EntityEditorWindow
{
/// Builds the window and opens the first mod that has items.
public ItemEditorWindow() : base("tools/items_editor/title", "item", "items", Fields())
{
}
private static List Fields() => new()
{
new EntityField { Key = "Name", LabelKey = "tools/items_editor/name", Kind = EntityFieldKind.LocaleText, KeyPattern = "items/{0}/name" },
new EntityField { Key = "Description", LabelKey = "tools/items_editor/description", Kind = EntityFieldKind.LocaleText, KeyPattern = "items/{0}/desc" },
new EntityField { Key = "Cost", LabelKey = "tools/items_editor/cost", TooltipKey = "tools/items_editor/cost_tip", Kind = EntityFieldKind.Integer },
new EntityField { Key = "Sprite", LabelKey = "tools/items_editor/sprite", TooltipKey = "tools/items_editor/sprite_tip", Kind = EntityFieldKind.Picture, Group = "items" },
new EntityField { Key = "ItemType", LabelKey = "tools/items_editor/type", TooltipKey = "tools/items_editor/type_tip", Kind = EntityFieldKind.Flags, Options = ItemTypes },
new EntityField { Key = "ItemAction", LabelKey = "tools/items_editor/action", TooltipKey = "tools/items_editor/action_tip", Kind = EntityFieldKind.Choice, Options = Actions }
};
private static IEnumerable ItemTypes() => Enum.GetNames(typeof(ItemType)).Where(n => n != nameof(ItemType.None));
private static IEnumerable Actions() => new[] { "null" }.Concat(ItemActionFactory.Instance.Ids.OrderBy(id => id, StringComparer.Ordinal));
/// A resource worth one coin with no use action; the picture is the author's to pick.
protected override JsonObject CreateEntity(string id) => new()
{
["Id"] = id,
["Cost"] = 1,
["ItemType"] = nameof(ItemType.Resource),
["ItemAction"] = "null"
};
/// <mod>/new_item, the folder-style ids items use.
protected override string NewIdBase(string mod) => mod + "/new_item";
}
}