## GameState — global game state: character positions, outfit, held items, object states, current room.
extends Node

signal state_changed
signal character_moved(character_id: String, position: Vector2)

var _character_positions: Dictionary = {}
var _character_outfits: Dictionary = {}
var _character_held_items: Dictionary = {}
var _object_states: Dictionary = {}
var _chest_states: Dictionary = {}
var current_room: String = "reception"
var music_volume: float = 0.6
var sfx_volume: float = 1.0


func has_character_position(id: String) -> bool:
	return _character_positions.has(id)


func get_character_position(id: String) -> Vector2:
	return _character_positions.get(id, Vector2.ZERO)


func set_character_position(id: String, pos: Vector2) -> void:
	_character_positions[id] = pos
	character_moved.emit(id, pos)
	state_changed.emit()


func get_character_outfit(id: String) -> Array:
	return _character_outfits.get(id, ["", "", ""])


func set_character_outfit(id: String, outfit: Array) -> void:
	_character_outfits[id] = outfit
	state_changed.emit()


func get_character_held_item(id: String, hand: String) -> String:
	if not _character_held_items.has(id):
		return ""
	return _character_held_items[id].get(hand, "")


func set_character_held_item(id: String, hand: String, item_id: String) -> void:
	if not _character_held_items.has(id):
		_character_held_items[id] = {"left": "", "right": ""}
	_character_held_items[id][hand] = item_id
	state_changed.emit()


func get_object_state(id: String) -> String:
	return _object_states.get(id, "idle")


func set_object_state(id: String, state: String) -> void:
	_object_states[id] = state
	state_changed.emit()


func get_chest_state(chest_id: String) -> Array:
	return _chest_states.get(chest_id, [])


func set_chest_state(chest_id: String, spawned_item_ids: Array[String]) -> void:
	_chest_states[chest_id] = spawned_item_ids
	state_changed.emit()


func clear_chest_state(chest_id: String) -> void:
	_chest_states.erase(chest_id)
	state_changed.emit()


func set_current_room(room: String) -> void:
	current_room = room
	state_changed.emit()


func get_save_data() -> Dictionary:
	var positions: Dictionary = {}
	for key: String in _character_positions:
		var pos: Vector2 = _character_positions[key]
		positions[key] = [pos.x, pos.y]
	return {
		"version": 3,
		"character_positions": positions,
		"character_outfits": _character_outfits.duplicate(true),
		"character_held_items": _character_held_items.duplicate(true),
		"object_states": _object_states,
		"chest_states": _chest_states.duplicate(true),
		"current_room": current_room,
		"music_volume": music_volume,
		"sfx_volume": sfx_volume,
	}


func apply_save_data(data: Dictionary) -> void:
	if data.has("character_positions"):
		_character_positions = {}
		for key: String in data["character_positions"]:
			var val: Variant = data["character_positions"][key]
			if val is Array and val.size() >= 2:
				_character_positions[key] = Vector2(val[0], val[1])
	if data.has("character_outfits"):
		_character_outfits = data["character_outfits"].duplicate(true)
	else:
		_character_outfits = {}
	if data.has("character_held_items"):
		_character_held_items = data["character_held_items"].duplicate(true)
	else:
		_character_held_items = {}
	if data.has("object_states"):
		_object_states = data["object_states"]
	else:
		_object_states = {}
	if data.has("current_room"):
		current_room = data["current_room"]
	if data.has("music_volume"):
		music_volume = data["music_volume"]
	if data.has("sfx_volume"):
		sfx_volume = data["sfx_volume"]
	if data.has("chest_states"):
		_chest_states = data["chest_states"].duplicate(true)
	else:
		_chest_states = {}
