# Remaps keys UI for LocalizationEditor : MIT License
# @author Vladimir Petrenko
@tool
extends Panel

var _data: LocalizationData

@onready var _head_ui = $VBox/Head
@onready var _scroll_ui = $VBox/Scroll
@onready var _keys_ui = $VBox/Scroll/Keys

const LocalizationRemapsKey = preload("res://addons/localization_editor/scenes/remaps/LocalizationRemapsKey.tscn")

func get_v_scroll() -> int:
	return _scroll_ui.get_v_scroll()

func set_v_scroll(value: int) -> void:
	_scroll_ui.set_v_scroll(value)

func set_data(data: LocalizationData) -> void:
	_data = data
	_head_ui.set_data(_data)
	_init_connections()
	_update_view()

func _init_connections() -> void:
	if not _data.is_connected("data_changed", _update_view):
		assert(_data.data_changed.connect(_update_view) == OK)

func _update_view() -> void:
	_clear_view()
	_draw_view()

func _clear_view() -> void:
	for key_ui in _keys_ui.get_children():
		_keys_ui.remove_child(key_ui)
		key_ui.queue_free()

func _draw_view() -> void:
	for key in _data.remapkeys_filtered():
		_draw_key(key)

func _draw_key(key) -> void:
	var key_ui = LocalizationRemapsKey.instantiate()
	_keys_ui.add_child(key_ui)
	key_ui.set_data(key, _data)
