class_name ChipBar extends Control

# The value to represent in filled chips. In the future chips may be colored differently above certain thresholds to stop drawing too many
@export var value: int:
	set(v):
		value = v
		if is_node_ready():
			_refresh()

# The total number of chips. If value is less than this, those chips should be empty.
@export var max_value: int:
	set(v):
		max_value = v
		if is_node_ready():
			_refresh()

# Configures the size of the hflow based on the chip texture size.
@export var max_chips_per_row: int

# The texture for an empty chip
@export var empty_chip_texture: Texture2D

# The texture for a filled chip
@export var filled_chip_texture: Texture2D

@onready var _container: HFlowContainer = $HFlowContainer


func _ready() -> void:
	_refresh()


func _refresh() -> void:
	for child in _container.get_children():
		child.queue_free()

	if filled_chip_texture and max_chips_per_row > 0:
		var chip_width := filled_chip_texture.get_width()
		_container.custom_minimum_size.x = chip_width * max_chips_per_row

	var empty_count := max_value - value
	for i in max_value:
		var tex_rect := TextureRect.new()
		if i < empty_count:
			tex_rect.texture = empty_chip_texture
		else:
			tex_rect.texture = filled_chip_texture
		_container.add_child(tex_rect)
