class_name TurnManager extends Node2D

@export var turn_timer := 7.0
@export var tts_number_speed = 7.0
@export var tts_sentence_speed = 5.0
@export var tts_volume = 75
@export var tts_pitch = 1.0

@export var desk: Desk
var timer: Timer
@export var game_manager: GameManager
@export var audience_manager: AudienceManager

var voices = DisplayServer.tts_get_voices_for_language("en")
var voice_id = voices[0]

func _ready() -> void:
	audience_manager.ask_accepted.connect(_handle_ask_accepted)
	desk.numpad.ask_proposed.connect(_speak_ask_proposed)
	timer = desk.numpad.reminder_timer
	timer.wait_time = turn_timer
	desk.numpad.reminder_timer.timeout.connect(_handle_reminder_timer_timeout)

func turn_timer_animation():
	pass

func _speak_ask_proposed(amount):
	var speech_variance = randf()
	if speech_variance < 0.45:
		DisplayServer.tts_speak(str(amount), voice_id, tts_volume, tts_pitch, tts_number_speed)
	elif speech_variance > 0.45 and speech_variance < 0.65:
		DisplayServer.tts_speak("Do I hear" + str(amount), voice_id, tts_volume, tts_pitch, tts_sentence_speed)
	elif speech_variance > 0.65 and speech_variance < 0.8:
		DisplayServer.tts_speak(str(amount) + ", anybody?", voice_id, tts_volume, tts_pitch, tts_sentence_speed)
	else:
		DisplayServer.tts_speak("Can I get a" + str(amount), voice_id, tts_volume, tts_pitch, tts_sentence_speed)

func _handle_ask_accepted():
	timer.start()
	turn_timer_animation()

func _handle_reminder_timer_timeout() -> void:
	if game_manager.state == game_manager.bidding_state.ASKING or game_manager.state == game_manager.bidding_state.BID:
		game_manager.state = game_manager.bidding_state.CLOSED
		game_manager.destroy_painting()
