using Content.Shared.Botany.Components;
using Content.Shared.Botany.Events;
using Content.Shared.Botany.Items.Components;
using Content.Shared.Botany.Systems;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.FixedPoint;
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Robust.Shared.Player;
namespace Content.Shared.Botany.Items.Systems;
///
/// System for taking a sample of a plant.
///
public sealed partial class BotanyProduceSystem : EntitySystem
{
[Dependency] private BotanySystem _botany = default!;
[Dependency] private PlantSystem _plant = default!;
[Dependency] private PlantTraySystem _plantTray = default!;
[Dependency] private SharedPopupSystem _popup = default!;
[Dependency] private SharedSolutionContainerSystem _solutionContainer = default!;
[Dependency] private EntityQuery _trayQuery;
[SubscribeLocalEvent]
private void OnAfterInteract(Entity ent, ref AfterInteractEvent args)
{
if (args.Target == null || args.Handled || !args.CanReach || !_trayQuery.HasComp(args.Target))
return;
var ev = new CompostingProduceAttemptEvent(ent, args.User);
RaiseLocalEvent(args.Target.Value, ref ev);
args.Handled = true;
}
[SubscribeLocalEvent]
private void OnCompostingProduceAttempt(Entity ent, ref CompostingProduceAttemptEvent args)
{
if (args.Cancelled)
return;
_popup.PopupCursor(Loc.GetString("plant-produce-component-compost-popup",
("owner", ent.Owner),
("usingItem", args.Produce.Owner)),
args.User,
PopupType.Medium);
_popup.PopupEntity(Loc.GetString("plant-produce-component-compost-others-popup",
("user", Identity.Entity(args.User, EntityManager)),
("usingItem", args.Produce.Owner),
("owner", ent.Owner)),
ent.Owner,
Filter.PvsExcept(args.User),
true);
if (_solutionContainer.TryGetSolution(args.Produce.Owner, args.Produce.Comp.TargetSolution, out var soln2, out var solution2))
{
if (_solutionContainer.ResolveSolution(ent.Owner, ent.Comp.SoilSolutionName, ref ent.Comp.SoilSolution, out var solution1))
{
// We try to fit as much of the composted plant's contained solution into the hydroponics tray as we can,
// since the plant will be consumed anyway.
var fillAmount = FixedPoint2.Min(solution2.Volume, solution1.AvailableVolume);
_solutionContainer.TryAddSolution(ent.Comp.SoilSolution.Value, _solutionContainer.SplitSolution(soln2.Value, fillAmount));
if (_plantTray.TryGetPlant(ent.AsNullable(), out var plantUid))
_plant.ForceUpdate(plantUid.Value);
}
}
if (_botany.TryGetPlantComponent(args.Produce.Comp.PlantData, args.Produce.Comp.PlantProtoId, out var compostPlant))
{
var nutrientBonus = compostPlant.Potency / args.Produce.Comp.NutrientDivider;
_plantTray.AdjustNutrient(ent.AsNullable(), nutrientBonus);
}
PredictedQueueDel(args.Produce.Owner);
}
}