class_name AudienceManager extends Node2D

@export var desk: Desk
@export var timer: Timer
@export var game_manager: GameManager
@export var bark_buttons: BarkButtons

signal ask_accepted
var bark_critical := false

#ideally variable for influencing how much the audience responds to barks
@export var audience_susceptibility := 0.4:
	set(value):
		audience_susceptibility = clamp(value, 0, 1)

#meant to make the audience more inclined to bid as day progresses
@export var audience_time_pressure := 1.4

@export var min_audience_think_time := 0.5
@export var max_audience_think_time := 2.0

@export var bid_threshold := 3.0
@export var think_chance := 0.5
@export var think_min_time := 1.0
@export var think_max_time := 5.0

var current_ask: int

func _ready() -> void:
	desk.numpad.ask_proposed.connect(_handle_ask_proposed)
	timer.timeout.connect(_handle_bid_delay_timeout)
	bark_buttons.auctioneer_bark.connect(_handle_auctioneer_bark)

func raise_paddle():
	#need to add in logic to animate paddle being raised
	game_manager.state = game_manager.bidding_state.BID
	print("Audience accepts the bid at $" + str(current_ask))
	game_manager.current_bid = desk.numpad.proposed_ask
	var collectors: Array[Node] = get_tree().get_nodes_in_group("ArtCollectors")
	collectors.shuffle()
	if bark_critical:
		collectors[0].critical_paddle()
		print("play crit paddle")
	else:
		collectors[0].normal_paddle()
		print("play norm paddle")
	bark_critical = false
	ask_accepted.emit()


func _handle_auctioneer_bark():
	if timer.time_left >= think_min_time:
		bark_critical = true
		timer.stop()
		timer.timeout.emit()
	pass

func _handle_bid_delay_timeout():
	if game_manager.state == game_manager.bidding_state.ASKING:
		if randf_range(1.2, 5.3) >= bid_threshold:
			raise_paddle()
			timer.stop()

func _handle_ask_proposed(amount):
	current_ask = amount

	if randf() <= think_chance:
		timer.stop()
		var ask_duration: float = randf_range(min_audience_think_time, max_audience_think_time)
		timer.wait_time = ask_duration
		timer.start()
