using Unity.Netcode;
using UnityEngine;
namespace AfterHours.Networking
{
///
/// Was der Spieler gerade trägt. Server-authoritativ – der Client darf
/// weder Produkt noch Menge selbst setzen.
///
public sealed class PlayerHands : NetworkBehaviour
{
public const int MaxCarry = 12;
private readonly NetworkVariable _productId = new(-1);
private readonly NetworkVariable _quantity = new(0);
public int ProductId => _productId.Value;
public int Quantity => _quantity.Value;
public bool IsEmpty => _quantity.Value <= 0;
/// Serverseitig: nimmt so viel wie möglich auf, gibt die tatsächliche Menge zurück.
public int TryTake(int productId, int quantity)
{
if (!IsServer) return 0;
if (quantity <= 0) return 0;
if (!IsEmpty && _productId.Value != productId)
return 0; // Hände sind mit etwas anderem belegt
int free = MaxCarry - _quantity.Value;
int taken = Mathf.Min(free, quantity);
if (taken <= 0) return 0;
_productId.Value = productId;
_quantity.Value += taken;
return taken;
}
/// Serverseitig: gibt bis zu ab.
public int TryGive(int quantity)
{
if (!IsServer || IsEmpty) return 0;
int given = Mathf.Min(_quantity.Value, quantity);
_quantity.Value -= given;
if (_quantity.Value == 0) _productId.Value = -1;
return given;
}
public void Clear()
{
if (!IsServer) return;
_productId.Value = -1;
_quantity.Value = 0;
}
}
}