extends VBoxContainer

const COLOR_CURRENT_BG := Color(0.83, 0.63, 0.09, 1)
const COLOR_CURRENT_FONT := Color(0.05, 0.05, 0.05, 1)
const COLOR_FUTURE_BG := Color(0.05, 0.18, 0.05, 1)
const COLOR_FUTURE_FONT := Color(0.2, 1, 0.2, 1)
const COLOR_CLEARED_BG := Color(0.05, 0.12, 0.05, 1)
const COLOR_CLEARED_FONT := Color(0.2, 0.55, 0.2, 1)
const COLOR_GROUND_BG := Color(0.08, 0.25, 0.08, 1)
const COLOR_GROUND_FONT := Color(0.5, 1, 0.5, 1)
const COLOR_BORDER := Color(0.2, 1, 0.2, 1)

const STATE_CURRENT := 0
const STATE_CLEARED := 1
const STATE_FUTURE := 2
const STATE_GROUND := 3

var _cells: Array = []
var _current_floor: int = EventBus.STARTING_FLOOR
var _styles: Array[StyleBoxFlat] = []
var _font_colors: Array[Color] = []

func _ready():
	add_theme_constant_override("separation", 2)
	_build_styles()
	_build_cells()
	_refresh()
	EventBus.floor_changed.connect(_on_floor_changed)

func _build_styles():
	_styles = [
		_make_style(COLOR_CURRENT_BG, 2),
		_make_style(COLOR_CLEARED_BG, 1),
		_make_style(COLOR_FUTURE_BG, 1),
		_make_style(COLOR_GROUND_BG, 2),
	]
	_font_colors = [
		COLOR_CURRENT_FONT,
		COLOR_CLEARED_FONT,
		COLOR_FUTURE_FONT,
		COLOR_GROUND_FONT,
	]

func _make_style(bg: Color, border_width: int) -> StyleBoxFlat:
	var style := StyleBoxFlat.new()
	style.bg_color = bg
	style.border_color = COLOR_BORDER
	style.border_width_left = border_width
	style.border_width_right = border_width
	style.border_width_top = border_width
	style.border_width_bottom = border_width
	style.content_margin_left = 4
	style.content_margin_right = 4
	style.content_margin_top = 4
	style.content_margin_bottom = 4
	return style

func _build_cells():
	var total := EventBus.STARTING_FLOOR
	for i in range(total):
		var floor_num := total - i
		var cell := PanelContainer.new()
		cell.size_flags_horizontal = Control.SIZE_EXPAND_FILL
		cell.size_flags_vertical = Control.SIZE_EXPAND_FILL

		var label := Label.new()
		label.text = "G" if floor_num == 1 else str(floor_num)
		label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
		label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
		label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
		label.size_flags_vertical = Control.SIZE_EXPAND_FILL
		label.add_theme_font_size_override("font_size", 20)
		cell.add_child(label)

		add_child(cell)
		_cells.append({ "panel": cell, "label": label, "floor": floor_num })

func _on_floor_changed(floor_num: int):
	if floor_num == _current_floor:
		return
	_current_floor = floor_num
	_refresh()

func _refresh():
	for entry in _cells:
		_style_cell(entry)

func _style_cell(entry: Dictionary):
	var floor_num: int = entry["floor"]
	var panel: PanelContainer = entry["panel"]
	var label: Label = entry["label"]

	var state: int
	if floor_num == _current_floor:
		state = STATE_CURRENT
	elif floor_num > _current_floor:
		state = STATE_CLEARED
	elif floor_num == 1:
		state = STATE_GROUND
	else:
		state = STATE_FUTURE

	panel.add_theme_stylebox_override("panel", _styles[state])
	label.add_theme_color_override("font_color", _font_colors[state])
