extends Control

@export var player: NodePath
@export var world: NodePath
@export var hand_size = 3
@export var drawpile: Array = [
	preload("res://Cards/FirewallCard.tscn").instantiate(),
	preload("res://Cards/FirewallCard.tscn").instantiate(),
	preload("res://Cards/CursorGunCard.tscn").instantiate(),
	preload("res://Cards/CursorGunCard.tscn").instantiate(),
	preload("res://Cards/DashCard.tscn").instantiate(),
	preload("res://Cards/DashCard.tscn").instantiate(),
	#preload("res://Cards/ShieldCard.tscn").instantiate(),
	#preload("res://Cards/ShieldCard.tscn").instantiate(),
	#preload("res://Cards/OverclockCard.tscn").instantiate(),
	#preload("res://Cards/OverclockCard.tscn").instantiate(),
]

var active_card_index = 0

var discard_pile = [];

func _ready() -> void:
	drawpile.shuffle();
	print_debug("Shuffled deck, drawpile size: " + str(drawpile.size()))
	print_debug("Empty card slots: " + str(hand_size) + " - " + str(get_cards().size()) + " = " + str(hand_size - get_cards().size()))
	# print_debug("Drawing initial hand")
	for i in range(hand_size - get_cards().size()):
		draw_card()

func _process(_delta):
	if Input.is_action_pressed("play_card"):
		play_card()
	if Input.is_action_just_pressed("cycle_card_left"):
		cycle_card(-1);
	elif Input.is_action_just_pressed("cycle_card_right"):
		cycle_card(1);

func play_card():
	var slots = $HBoxContainer.get_children()
	if active_card_index >= 0 and active_card_index < slots.size():
		var card = slots[active_card_index].get_node("Card")
		card.activate(get_node(world), get_node(player));
		if card.ammo <= 0:
			discard(false);
		
		print(active_card_index)

func discard(do_ability = true):
	var cards = get_cards();
	if active_card_index >= 0 and active_card_index < cards.size():
		var card = cards[active_card_index]
		if do_ability:
			card.discard(get_node(world), get_node(player))

		discard_pile.append(card)
		card.get_parent().remove_child(card)

		#active_card_index = min(active_card_index, hand.size() - 1);
		draw_card();

func cycle_card(index_shift):
	if get_cards().size() > 0:
		active_card_index = (active_card_index + index_shift) % get_cards().size();
	else:
		draw_card();

func draw_card():
	print_debug("Drawing card")
	var held_card_count = get_cards().size()
	if held_card_count == 0:
		shuffle_deck();
	if drawpile.size() > 0 and held_card_count < hand_size:
		for i in range(hand_size - held_card_count):
			var card = drawpile.pop_back();
			if active_card_index == -1:
				active_card_index = 0;
			print_debug("Instantiating card from drawpile, remaining size: " + str(drawpile.size()))
			var empty_slots = get_card_container().get_children().filter(func(slot): return not slot.has_method("get_node") or slot.get_node_or_null("Card") == null)
			if empty_slots.size() > 0:
				empty_slots[0].add_child(card)
	else:
		var first_filled_slot_index = -1
		var slots = get_card_container().get_children()
		for i in range(slots.size()):
			if slots[i].has_method("get_node") and slots[i].get_node_or_null("Card") != null:
				first_filled_slot_index = i
				break
		active_card_index = first_filled_slot_index

# TODO: Reboot mechanics
func shuffle_deck():
	drawpile.append_array(discard_pile)
	discard_pile.clear();
	drawpile.shuffle();
	active_card_index = 0;

func get_cards() -> Array:
	return get_card_container().get_children().map(func(c): return c.get_node_or_null("Card")).filter(func(c): return c != null)

func get_card_container():
	return $HBoxContainer

func get_active_card():
	return get_cards().get(active_card_index).get_node("Card")
