# Shop.gd
extends Node2D

signal item_purchased(item_name: String)  # Define the signal
signal gold_spent(amount: int)
signal next_round_requested

@onready var gold_display: Label = $GoldDisplay
@onready var shop_item_scene = preload("res://scenes/ShopItem.tscn")
@onready var gold_amount: Label = $MainContainer/GoldDisplay/HBoxContainer/GoldAmount
@onready var gold_icon: TextureRect = $MainContainer/GoldDisplay/HBoxContainer/GoldIcon

var player_gold: int = 0

var shop_items = {
	"pieces": [
		{"name": "Pawn", "price": 1, "texture": "res://assets/Resized Chess Piece Assets/White_Pawn.png"},
		{"name": "Rook", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/White_Rook.png"},
		{"name": "Bishop", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/White_Bishop.png"},
		{"name": "Knight", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/White_Knight.png"},
		{"name": "Queen", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/White_Queen.png"}
	],
	"upgrades": [
		{"name": "Upgrade 1", "price": 3, "texture": "res://assets/Resized Chess Piece Assets/Black_Pawn.png"},
		{"name": "Upgrade 2", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Rook.png"},
		{"name": "Upgrade 3", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Bishop.png"},
		{"name": "Upgrade 4", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Knight.png"},
		{"name": "Upgrade 5", "price": 5, "texture": "res://assets/Resized Chess Piece Assets/Black_Queen.png"}
	]
}

func _ready():
	self.visible = false  # Start hidden
	print("Shop scene loaded!")  # Check if this prints
	populate_rows()  # Populate the shop rows
	update_gold_display(player_gold)  # Initialize gold display
	
func set_visibility(visible: bool):
	self.visible = visible
	
func update_gold_display(gold: int):
	player_gold = gold  # Store gold locally for shop logic
	gold_amount.text = str(gold)  # Update the label text

func _on_back_button_pressed():
	get_tree().change_scene_to_file("res://Game.tscn")  # Return to game

func populate_rows():
	for item in shop_items["pieces"]:
		add_item_to_row(item, $MainContainer/RowsContainer/PiecesRow)
	for item in shop_items["upgrades"]:
		add_item_to_row(item, $MainContainer/RowsContainer/UpgradesRow)


func add_item_to_row(item_data: Dictionary, row: HBoxContainer):
	var shop_item = shop_item_scene.instantiate()

	shop_item.get_node("MarginContainer/VBoxContainer/ItemName").text = item_data["name"]
	shop_item.get_node("MarginContainer/VBoxContainer/ItemPrice").text = "%dg" % item_data["price"]
	shop_item.get_node("MarginContainer/VBoxContainer/ItemSprite").texture = load(item_data["texture"])

	# Get the BuyButton
	var buy_button = shop_item.get_node("MarginContainer/VBoxContainer/BuyButton")

	# Connect the button to a handler, passing the item_data
	buy_button.connect("pressed", Callable(self, "_on_item_purchased").bind(item_data))

	row.add_child(shop_item)

func _on_item_purchased(item_data: Dictionary):
	if player_gold >= item_data["price"]:
		player_gold -= item_data["price"]
		update_gold_display(player_gold)
		emit_signal("gold_spent", item_data["price"])  # Emit the signal
		emit_signal("item_purchased", item_data["name"])  # Notify the main game
		print("Purchased: ", item_data["name"])
		# TODO: Add a sound effect / Animation for purchasing items
	else:
		# TODO: Add a sound effect / warning for not enough gold
		print("Not enough gold!")
	


func _on_next_round_button_pressed():
	print("Next Round button pressed!")
	visible = false  # Hide the shop
	emit_signal("next_round_requested")  # Notify the main game script