extends SubStateMachine

class_name WeaponSubStateMachine

@export var animation_prefix: StringName
@export var droppable: StringName
@export var visibility_target: StringName

@export var max_ammo: int
@export var ammo: int = -1:
	set(value):
		if value < 0:
			ammo = 0
		else:
			ammo = value
		ammo_updated.emit()
		if ammo <= 0 and remaining_ammo <= 0:
			ammo_depleted.emit()
	get:
		return ammo
@export var ammo_mags: int = 3
@export var remaining_ammo: int = -1:
	set(value):
		if value < 0:
			remaining_ammo = 0
		else:
			remaining_ammo = value

@export var speed_modifier: float = 1.0
@export var can_be_previous: bool = true
@export var destroy_when_empty: bool = false

@export var slot: StringName

signal request_return
signal ammo_updated
signal ammo_depleted

var system: WeaponSystem
var animation_player: AnimationPlayer
var player_camera: PlayerCamera
var player: Player 

func _ready() -> void:
	if remaining_ammo == -1:
		remaining_ammo = max_ammo * ammo_mags
	if ammo == -1:
		ammo = max_ammo
	for child in get_children():
		if child is WeaponState:
			states[child.name] = child
			child.machine = self
			child.transition.connect(on_transition_required)
			child.return_to_previous.connect(request_return.emit)

func enter() -> void:
	super()
	player.weapon_models[visibility_target].show()

func exit() -> void:
	super()
	player.weapon_models[visibility_target].hide()

@rpc("authority","call_local","reliable")
func use_begin() -> void:
	current_state.use_begin()
@rpc("authority","call_local","reliable")
func use_end() -> void:
	current_state.use_end()
func alternate_state() -> void:
	current_state.alternate_state()
# Need to clarify naming; Switch mode like firemode. For different states use
# alternate_state
func switch_mode() -> void:
	current_state.switch_mode()
