extends Node

## 存储服务
## 负责本地配置的读写，包括用户账号、密码、Token 等

# 配置文件路径
const CONFIG_PATH: String = "user://user_config.cfg"

# ConfigFile 实例
var _config: ConfigFile

func _ready():
	_config = ConfigFile.new()
	_loadConfig()

## 加载配置文件
func _loadConfig() -> void:
	var error = _config.load(CONFIG_PATH)
	if error != OK and error != ERR_FILE_NOT_FOUND:
		push_error("加载配置文件失败: " + str(error))

## 保存配置文件
func _saveConfig() -> void:
	var error = _config.save(CONFIG_PATH)
	if error != OK:
		push_error("保存配置文件失败: " + str(error))

## 保存键值对
func save(section: String, key: String, value: Variant) -> void:
	_config.set_value(section, key, value)
	_saveConfig()

## 读取值
func getValue(section: String, key: String, defaultValue: Variant = null) -> Variant:
	return _config.get_value(section, key, defaultValue)

## 保存账号信息（包括加密的密码）
func saveAccount(username: String, password: String, remember: bool) -> void:
	save("account", "last_username", username)

	if remember and not password.is_empty():
		# 加密密码后保存
		var encryptedPassword = EncryptionService.encrypt(password)
		save("account", "saved_password", encryptedPassword)
		save("account", "remember_password", true)
	else:
		# 不记住密码，清除保存的密码
		clearSavedPassword()

## 读取保存的账号信息
func loadAccount() -> Dictionary:
	var username = getValue("account", "last_username", "")
	var rememberPassword = getValue("account", "remember_password", false)
	var encryptedPassword = getValue("account", "saved_password", "")

	var result = {
		"username": username,
		"remember_password": rememberPassword
	}

	# 如果记住密码，解密密码
	if rememberPassword and not encryptedPassword.is_empty():
		var decryptedPassword = EncryptionService.decrypt(encryptedPassword)
		result["password"] = decryptedPassword
	else:
		result["password"] = ""

	return result

## 清除保存的密码
func clearSavedPassword() -> void:
	save("account", "saved_password", "")
	save("account", "remember_password", false)

## 保存 Token
func saveToken(token: String, expireTime: int = 0) -> void:
	save("auth", "token", token)
	save("auth", "expire_time", expireTime)

## 读取 Token
func loadToken() -> Dictionary:
	var token = getValue("auth", "token", "")
	var expireTime = getValue("auth", "expire_time", 0)

	return {
		"token": token,
		"expire_time": expireTime
	}

## 清除 Token
func clearToken() -> void:
	save("auth", "token", "")
	save("auth", "expire_time", 0)

## 保存用户信息
func saveUserData(userData: UserData) -> void:
	if userData == null:
		return

	save("user", "id", userData.id)
	save("user", "username", userData.username)
	save("user", "email", userData.email)
	save("user", "level", userData.level)
	save("user", "exp", userData.exp)
	save("user", "avatar_url", userData.avatarUrl)

## 读取用户信息
func loadUserData() -> UserData:
	var userData = UserData.new()
	userData.id = str(getValue("user", "id", ""))
	userData.username = getValue("user", "username", "")
	userData.email = getValue("user", "email", "")
	userData.level = getValue("user", "level", 1)
	userData.exp = getValue("user", "exp", 0)
	userData.avatarUrl = getValue("user", "avatar_url", "")

	return userData if not userData.id.is_empty() else null

## 清除所有用户数据
func clearAll() -> void:
	clearSavedPassword()
	clearToken()
	_config.clear()
	_saveConfig()
