extends Interactible

var plant: StringName
@onready var defuse_timer: Timer = $DefuseTimer
@export var bomb_audio: MultiplayerAudio3D
@export var tick_curve : Curve
var tick_time: float
var tick_stop: bool

func _ready() -> void:
	super()
	if multiplayer.is_server():
		Session.bomb_timer.timeout.connect(on_timeout)
		Session.begin_bomb_stage()

func _process(delta: float) -> void:
	if tick_stop:
		return
	if multiplayer.is_server():
		tick_time += delta
		if tick_time >= tick_curve.sample(1-Session.bomb_timer.time_left/Session.bomb_timer.wait_time):
			bomb_audio.multiplayer_play()
			
			tick_time = 0

func on_timeout():
	if multiplayer.is_server() == false:
		return
	tick_stop = true
	$BoomAudio.play()
	Session.kill_site(plant)

func on_defuse_timeout():
	Session.defuse_win()
	tick_stop = true

func interaction_start(player_id: int):
	if tick_stop or Session.player_nodes[player_id].team != Session.TEAMS.DEFENCE:
		return
	super(player_id)
	defuse_timer.start()
	$DefuseAudio.multiplayer_play()
	Session.player_nodes[player_id].passive.rpc_id(player_id)

func interaction_end():
	Session.player_nodes[interacted_id].depassive.rpc_id(interacted_id)
	
	super()
	defuse_timer.stop()
