using Content.Shared.Examine;
using Content.Shared.Storage.Components;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Containers;
using Content.Shared.Emag.Systems;
namespace Content.Shared.Xenoarchaeology.Equipment;
///
/// This handles logic relating to
///
public abstract class SharedArtifactCrusherSystem : EntitySystem
{
[Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
[Dependency] protected readonly SharedAudioSystem AudioSystem = default!;
[Dependency] protected readonly SharedContainerSystem ContainerSystem = default!;
[Dependency] private readonly EmagSystem _emag = default!;
///
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnInit);
SubscribeLocalEvent(OnStorageAfterOpen);
SubscribeLocalEvent(OnStorageOpenAttempt);
SubscribeLocalEvent(OnExamine);
SubscribeLocalEvent(OnEmagged);
}
private void OnInit(Entity ent, ref ComponentInit args)
{
ent.Comp.OutputContainer = ContainerSystem.EnsureContainer(ent, ent.Comp.OutputContainerName);
}
private void OnStorageAfterOpen(Entity ent, ref StorageAfterOpenEvent args)
{
StopCrushing(ent);
ContainerSystem.EmptyContainer(ent.Comp.OutputContainer);
}
private void OnEmagged(Entity ent, ref GotEmaggedEvent args)
{
if (!_emag.CompareFlag(args.Type, EmagType.Interaction))
return;
if (_emag.CheckFlag(ent, EmagType.Interaction))
return;
if (ent.Comp.AutoLock)
return;
ent.Comp.AutoLock = true;
args.Handled = true;
}
private void OnStorageOpenAttempt(Entity ent, ref StorageOpenAttemptEvent args)
{
if (ent.Comp.AutoLock && ent.Comp.Crushing)
args.Cancelled = true;
}
private void OnExamine(Entity ent, ref ExaminedEvent args)
{
args.PushMarkup(ent.Comp.AutoLock ? Loc.GetString("artifact-crusher-examine-autolocks") : Loc.GetString("artifact-crusher-examine-no-autolocks"));
}
public void StopCrushing(Entity ent, bool early = true)
{
var (_, crusher) = ent;
if (!crusher.Crushing)
return;
crusher.Crushing = false;
Appearance.SetData(ent, ArtifactCrusherVisuals.Crushing, false);
if (early)
{
AudioSystem.Stop(crusher.CrushingSoundEntity?.Item1, crusher.CrushingSoundEntity?.Item2);
crusher.CrushingSoundEntity = null;
}
Dirty(ent, ent.Comp);
}
}