using OpenTheBox.Core.Characters;
using OpenTheBox.Core.Enums;
using OpenTheBox.Core.Items;
namespace OpenTheBox.Core;
///
/// Represents the current/max pair for a resource.
///
public sealed record ResourceState(int Current, int Max);
///
/// The complete player state, containing all inventory, progression, and configuration data.
///
public sealed class GameState
{
public required string PlayerName { get; set; }
public required PlayerAppearance Appearance { get; set; }
public required List Inventory { get; set; }
public required Dictionary Resources { get; set; }
public required Dictionary Stats { get; set; }
public required HashSet UnlockedUIFeatures { get; set; }
public required HashSet UnlockedWorkstations { get; set; }
public required HashSet UnlockedAdventures { get; set; }
public required HashSet UnlockedCosmetics { get; set; }
public required HashSet CompletedAdventures { get; set; }
public required Dictionary AdventureSaveData { get; set; }
public required HashSet VisibleResources { get; set; }
public required HashSet VisibleStats { get; set; }
public required int TotalBoxesOpened { get; set; }
public required Locale CurrentLocale { get; set; }
public required DateTime CreatedAt { get; set; }
public required TimeSpan TotalPlayTime { get; set; }
public required HashSet AvailableFonts { get; set; }
public required HashSet AvailableTextColors { get; set; }
///
/// Returns the current value of a resource, or 0 if the resource is not tracked.
///
public int GetResource(ResourceType type)
=> Resources.TryGetValue(type, out var state) ? state.Current : 0;
///
/// Returns true if the inventory contains at least one item with the given definition id.
///
public bool HasItem(string defId)
=> Inventory.Any(i => i.DefinitionId == defId);
///
/// Counts the total quantity of items matching the given definition id.
///
public int CountItems(string defId)
=> Inventory.Where(i => i.DefinitionId == defId).Sum(i => i.Quantity);
///
/// Returns true if the given UI feature has been unlocked.
///
public bool HasUIFeature(UIFeature feature)
=> UnlockedUIFeatures.Contains(feature);
///
/// Adds an item instance to the inventory.
///
public void AddItem(ItemInstance item)
=> Inventory.Add(item);
///
/// Removes an item instance from the inventory by its unique instance id.
/// Returns true if the item was found and removed.
///
public bool RemoveItem(Guid id)
{
var item = Inventory.FirstOrDefault(i => i.Id == id);
if (item is null)
return false;
return Inventory.Remove(item);
}
///
/// Factory method to create a new GameState with empty collections and sensible defaults.
///
public static GameState Create(string name, Locale locale) => new()
{
PlayerName = name,
Appearance = new PlayerAppearance(),
Inventory = [],
Resources = [],
Stats = [],
UnlockedUIFeatures = [],
UnlockedWorkstations = [],
UnlockedAdventures = [],
UnlockedCosmetics = [],
CompletedAdventures = [],
AdventureSaveData = [],
VisibleResources = [],
VisibleStats = [],
TotalBoxesOpened = 0,
CurrentLocale = locale,
CreatedAt = DateTime.UtcNow,
TotalPlayTime = TimeSpan.Zero,
AvailableFonts = [],
AvailableTextColors = []
};
}