using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cthangover.Core.Items;
using Cthangover.Core.Mods.Resolvers;
using Cthangover.Mods.Rpg;
using Godot;
namespace Cthangover.Tools.RpgEditor
{
///
/// The items the running game knows (Kind: item of every loaded mod),
/// sorted by display name, for laying them on a map. The look is the item's
/// own Sprite in the items group, the picture the inventory
/// shows; an item whose picture is missing gets none (the texture loader
/// would log an error for it).
///
public static class RpgItemCatalog
{
/// Reads the item list anew.
public static List Load(RpgTextures textures)
{
var pictures = GroupFiles(RpgTextures.IconGroup);
return (Entities.Collect()?.Values ?? Enumerable.Empty())
.Where(info => !string.IsNullOrEmpty(info.ID))
.Select(info => Entry(info, pictures.Contains(info.Sprite ?? string.Empty) ? info.Sprite : null, textures))
.OrderBy(entry => entry.Name, StringComparer.CurrentCultureIgnoreCase)
.ToList();
}
/// Paths of a file group without extensions, case-insensitive: what exists to be loaded.
public static HashSet GroupFiles(string group)
{
var paths = new HashSet(StringComparer.OrdinalIgnoreCase);
var files = Files.Collect(group);
if (files != null)
{
foreach (var key in files.Keys)
paths.Add(Path.ChangeExtension(key, null));
}
return paths;
}
private static RpgPickerEntry Entry(ItemInfo info, string picture, RpgTextures textures) => new()
{
Id = info.ID,
Name = string.IsNullOrEmpty(info.Name) ? info.ID : TranslationServer.Translate(info.Name).ToString(),
Look = picture,
Icon = picture == null ? null : textures.Icon(picture)
};
}
}