using System;
using Content.Server.Hands.Components;
using Content.Server.Items;
using Content.Shared.Audio;
using Content.Shared.Examine;
using Content.Shared.Sound;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.Utility.Markup;
using Robust.Shared.ViewVariables;
namespace Content.Server.PowerCell.Components
{
///
/// Provides a "battery compartment" that can contain a of the matching
/// . Intended to supplement other components, not very useful by itself.
///
[RegisterComponent]
#pragma warning disable 618
public class PowerCellSlotComponent : Component, IExamine, IMapInit
#pragma warning restore 618
{
[Dependency] private readonly IEntityManager _entities = default!;
public override string Name => "PowerCellSlot";
///
/// What size of cell fits into this component.
///
[ViewVariables(VVAccess.ReadWrite)]
[DataField("slotSize")]
public PowerCellSize SlotSize { get; set; } = PowerCellSize.Small;
///
/// Can the cell be removed ?
///
[ViewVariables(VVAccess.ReadWrite)]
[DataField("canRemoveCell")]
public bool CanRemoveCell { get; set; } = true;
///
/// Should the "Remove cell" verb be displayed on this component?
///
[ViewVariables(VVAccess.ReadWrite)]
[DataField("showVerb")]
public bool ShowVerb { get; set; } = true;
///
/// String passed to String.Format when showing the description text for this item.
/// String.Format is given a single parameter which is the size letter (S/M/L) of the cells this component uses.
/// Use null to show no text.
///
[ViewVariables(VVAccess.ReadWrite)]
[DataField("descFormatString")]
public string? DescFormatString { get; set; } = "It uses size {0} power cells.";
///
/// File path to a sound file that should be played when the cell is removed.
///
/// "/Audio/Items/pistol_magout.ogg"
[ViewVariables(VVAccess.ReadWrite)]
[DataField("cellRemoveSound")]
public SoundSpecifier CellRemoveSound { get; set; } = new SoundPathSpecifier("/Audio/Items/pistol_magin.ogg");
///
/// File path to a sound file that should be played when a cell is inserted.
///
/// "/Audio/Items/pistol_magin.ogg"
[ViewVariables(VVAccess.ReadWrite)]
[DataField("cellInsertSound")]
public SoundSpecifier CellInsertSound { get; set; } = new SoundPathSpecifier("/Audio/Items/pistol_magout.ogg");
[ViewVariables] private ContainerSlot _cellContainer = default!;
[ViewVariables]
public PowerCellComponent? Cell
{
get
{
if (_cellContainer.ContainedEntity == null) return null;
return _entities.TryGetComponent(_cellContainer.ContainedEntity.Value, out PowerCellComponent? cell) ? cell : null;
}
}
[ViewVariables] public bool HasCell => Cell != null;
///
/// True if we don't want a cell inserted during map init.
///
[DataField("startEmpty")]
private bool _startEmpty = false;
///
/// If not null, this cell type will be inserted at MapInit instead of the default Standard cell.
///
[DataField("startingCellType")]
private string? _startingCellType = null;
protected override void Initialize()
{
base.Initialize();
_cellContainer = ContainerHelpers.EnsureContainer(Owner, "cellslot_cell_container", out _);
}
void IExamine.Examine(FormattedMessage.Builder message, bool inDetailsRange)
{
if (!inDetailsRange) return;
string sizeLetter = SlotSize switch
{
PowerCellSize.Small => Loc.GetString("power-cell-slot-component-small-size-shorthand"),
PowerCellSize.Medium => Loc.GetString("power-cell-slot-component-medium-size-shorthand"),
PowerCellSize.Large => Loc.GetString("power-cell-slot-component-large-size-shorthand"),
_ => "???"
};
if (DescFormatString != null) message.AddMarkup(string.Format(DescFormatString, sizeLetter));
}
///
/// Remove the cell from this component. If a user is specified, the cell will be put in their hands
/// or failing that, at their feet. If no user is specified the cell will be put at the location of
/// the parent of this component.
///
/// (optional) the user to give the removed cell to.
/// Should be played upon removal?
/// The cell component of the entity that was removed, or null if removal failed.
public PowerCellComponent? EjectCell(EntityUid user, bool playSound = true)
{
var cell = Cell;
if (cell == null || !CanRemoveCell) return null;
if (!_cellContainer.Remove(cell.Owner)) return null;
//Dirty();
if (user != null)
{
if (!_entities.TryGetComponent(user, out HandsComponent? hands) || !hands.PutInHand(_entities.GetComponent(cell.Owner)))
{
_entities.GetComponent(cell.Owner).Coordinates = _entities.GetComponent(user).Coordinates;
}
}
else
{
_entities.GetComponent(cell.Owner).Coordinates = _entities.GetComponent(Owner).Coordinates;
}
if (playSound)
{
SoundSystem.Play(Filter.Pvs(Owner), CellRemoveSound.GetSound(), Owner, AudioHelpers.WithVariation(0.125f));
}
_entities.EventBus.RaiseLocalEvent(Owner, new PowerCellChangedEvent(true), false);
return cell;
}
///
/// Tries to insert the given cell into this component. The cell will be put into the container of this component.
///
/// The cell to insert.
/// Should be played upon insertion?
/// True if insertion succeeded; false otherwise.
public bool InsertCell(EntityUid cell, bool playSound = true)
{
if (Cell != null) return false;
if (!_entities.HasComponent(cell)) return false;
if (!_entities.TryGetComponent(cell, out var cellComponent)) return false;
if (cellComponent.CellSize != SlotSize) return false;
if (!_cellContainer.Insert(cell)) return false;
//Dirty();
if (playSound)
{
SoundSystem.Play(Filter.Pvs(Owner), CellInsertSound.GetSound(), Owner, AudioHelpers.WithVariation(0.125f));
}
_entities.EventBus.RaiseLocalEvent(Owner, new PowerCellChangedEvent(false), false);
return true;
}
void IMapInit.MapInit()
{
if (_startEmpty || _cellContainer.ContainedEntity != null)
{
return;
}
string type;
if (_startingCellType != null)
{
type = _startingCellType;
}
else
{
type = SlotSize switch
{
PowerCellSize.Small => "PowerCellSmallStandard",
PowerCellSize.Medium => "PowerCellMediumStandard",
PowerCellSize.Large => "PowerCellLargeStandard",
_ => throw new ArgumentOutOfRangeException()
};
}
var cell = _entities.SpawnEntity(type, _entities.GetComponent(Owner).Coordinates);
_cellContainer.Insert(cell);
}
}
public class PowerCellChangedEvent : EntityEventArgs
{
///
/// If true, the cell was ejected; if false, it was inserted.
///
public bool Ejected { get; }
public PowerCellChangedEvent(bool ejected)
{
Ejected = ejected;
}
}
}