# Shop.gd
extends Node2D

signal purchase_attempted(item_data: Dictionary)
signal next_round_requested

@onready var shop_item_scene = preload("res://scenes/ShopItem.tscn")


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

func _ready():
	self.visible = false  # Start hidden
	print("Shop scene loaded!")  # Check if this prints
	populate_rows()  # Populate the shop rows
	
func set_visibility(visible: bool):
	self.visible = visible

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"]:
		# Create a copy to avoid modifying the original data
		var item_copy = item.duplicate()
		add_item_to_row(item_copy, $MainContainer/RowsContainer/PiecesRow)

	for item in shop_items["upgrades"]:
		var item_copy = item.duplicate()
		add_item_to_row(item_copy, $MainContainer/RowsContainer/UpgradesRow)


func add_item_to_row(item_data: Dictionary, row: HBoxContainer):
	if item_data["purchased"]:
		return  # Skip purchased items

	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_buy_button_pressed").bind(item_data))

	row.add_child(shop_item)

func _on_buy_button_pressed(item_data: Dictionary):
	if !item_data["purchased"]:
		print("Attempting to Purchase: ", item_data["name"])
		# Emit signals to let Game.gd handle the purchase
		emit_signal("purchase_attempted", item_data)  # Emit the full item data
		# TODO: Add a sound effect / Animation for purchasing items
	else:
		print("Item already purchased!")
	


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