class_name GameManager extends Node2D

@export var audience_manager: AudienceManager
@export var desk: Desk
@export var turn_manager: TurnManager

signal lost
signal won

var paintings_sold = 0
@export var paintings_total = 7

var paintings: Array = Array()
var current_painting = 0

# tracker variables for bid markers, proposing bids happens between numpad and audience
var current_bid = 0
var starting_price = 0
var final_bid = 0
@export var current_bid_display: RichTextLabel

var target_sales: int = 0
var total_sales: int = 0
@export var sales_magnitude = 100000
@export var single_painting_magnitude = 1000
@export var initial_value_reduction: float = 0.6

# state tracker for a given painting's auction
var bidding_open: bool = false
enum bidding_state {CLOSED, READY, ASKING, BID}
var state: int = bidding_state.CLOSED
signal new_painting_displayed

func _ready() -> void:
	audience_manager.ask_accepted.connect(_handle_ask_accepted)
	desk.gavel.gavel_hit.connect(_handle_gavel_hit)

	#paintings_total = randi_range(7,10)
	target_sales = randi_range(2,5) * sales_magnitude

	var new_painting = 0
	var new_painting_value: int

	while new_painting < paintings_total:
		new_painting_value = (target_sales/randf_range(paintings_total - 2, paintings_total + 2))
		new_painting_value = snappedi(new_painting_value, single_painting_magnitude)
		print(str(new_painting_value))
		paintings.append(new_painting_value)
		new_painting += 1

	print(paintings)
	print("You have " + str(paintings_total) + " paintings. Sell them for at least $" + str(target_sales) + " or face the consequences!")
	print("Hit the gavel and input the starting price to begin!")

	DisplayServer.tts_speak("You have " + str(paintings_total) + " paintings. Sell them fast for at least $" + str(target_sales) + " or face the consequences!", turn_manager.voice_id, turn_manager.tts_volume, turn_manager.tts_pitch)
	DisplayServer.tts_speak("Hit the gavel and input the starting price to begin!", turn_manager.voice_id, turn_manager.tts_volume, turn_manager.tts_pitch)

	next_painting(0)
	#build out the initialization process, which should:
	# -- generate paintings and assign values
	# -- start the turn timer/auction timer
	# ---- also add an auction timer
	# -- have tts announcement of starting bid and start of auction




func _handle_gavel_hit():
	if state == bidding_state.CLOSED:
		state = bidding_state.READY
		print(str(state))
		current_bid_display.set_text("Starting price: $" + str(starting_price))
	elif state == bidding_state.ASKING:
		if current_bid != 0:
			state = bidding_state.CLOSED
			desk.numpad.reminder_timer.stop()
			current_bid_display.set_text("Sold for $" + str(current_bid) + "!")
			sell_painting()
			print("Congrats on selling your painting for $" + str(current_bid) + "! You have made $" + str(total_sales) + " so far.")
			current_painting += 1
			if current_painting <= paintings_total - 1:
				next_painting((current_painting))
				print("The next painting is valued at $" + str(starting_price) + " (should be $" + str(paintings[current_painting]) + ")")
				(print(str(state)))
			else:
				end_auction()

	else:
		pass

func destroy_painting():
	# Painting destruction animation/particle effect
	$FailedPainting.play()
	current_painting += 1
	DisplayServer.tts_speak("Oh nooooooo! Took too long!", turn_manager.voice_id, turn_manager.tts_volume, turn_manager.tts_pitch)
	DisplayServer.tts_speak("Try to make it up on the next painting!", turn_manager.voice_id, turn_manager.tts_volume, turn_manager.tts_pitch)
	desk.numpad.reminder_timer.stop()
	if current_painting <= paintings_total - 1:
		next_painting((current_painting))
		print("The next painting is valued at $" + str(starting_price) + " (should be $" + str(paintings[current_painting]) + ")")
		(print(str(state)))
	else:
		end_auction()

func sell_painting():
	total_sales = total_sales + current_bid

func next_painting(a: int):
	$NextPainting.play()
	starting_price = paintings[a]
	current_bid = 0

	# will need to add animation/image swap
	new_painting_displayed.emit()

func _handle_ask_accepted():
	current_bid_display.set_text("Current bid: $" + str(current_bid))

func end_auction():
	#add in logic for displaying/transitioning to score screen
	if total_sales >= target_sales:
		#add context specific score text?
		DisplayServer.tts_speak("Congratulations! The auction house will run another day.", turn_manager.voice_id, turn_manager.tts_volume, turn_manager.tts_pitch)
		emit_signal("won")
	else:
		#add context specific score text?
		DisplayServer.tts_speak("You have failed. We must find a new auctioneer.", turn_manager.voice_id, turn_manager.tts_volume, turn_manager.tts_pitch)
		emit_signal("lost")

# OTHER THINGS TO ADD:
# UI elements for score
# Bark manager
