local addonName, ns = ...
local L = ns.L

-- 序列化table为单行字符串
function ns.TableToString(tbl)
	if not tbl then return "{}" end
	local result = {}
	table.insert(result, "{")
	for k, v in pairs(tbl) do
		if type(v) == "table" then
			table.insert(result, string.format('[%q]=%s,', tostring(k), ns.TableToString(v)))
		elseif type(v) == "string" then
			table.insert(result, string.format('[%q]=%q,', tostring(k), v))
		elseif type(v) == "number" or type(v) == "boolean" then
			table.insert(result, string.format('[%q]=%s,', tostring(k), tostring(v)))
		end
	end
	table.insert(result, "}")
	return table.concat(result)
end

-- 字符串解析为table
function ns.StringToTable(str)
	local func, err = loadstring("return " .. str)
	if not func then
		return nil, err
	end
	local success, result = pcall(func)
	if not success then
		return nil, result
	end
	return result
end

-- 校验并合并导入的配置（缺少的key用默认值补全）
-- 返回: mergedDB, missingKeys, errMsg
function ns.ValidateAndMergeImport(importDB)
	if type(importDB) ~= "table" then
		return nil, nil, L["导入格式错误"]
	end

	-- 检查是否有基本字段（至少包含一个默认DB里的key）
	local hasAny = false
	for k in pairs(ns.PlateColorDB) do
		if importDB[k] ~= nil then
			hasAny = true
			break
		end
	end
	if not hasAny then
		return nil, nil, L["导入格式不匹配"]
	end

	local missingKeys = {}

	-- 按照DefaultDB的合并逻辑补全缺失的字段，同时记录缺失项
	for k, v in pairs(ns.PlateColorDB) do
		if type(v) == "table" then
			if importDB[k] == nil then
				importDB[k] = {}
				table.insert(missingKeys, tostring(k) .. " (" .. L["整个表缺失"] .. ")")
			else
				local subMissing = {}
				for k2, v2 in pairs(v) do
					if type(k2) ~= "number" and importDB[k][k2] == nil then
						importDB[k][k2] = v2
						table.insert(subMissing, tostring(k2))
					end
				end
				if #subMissing > 0 then
					table.insert(missingKeys, tostring(k) .. ".{" .. table.concat(subMissing, ", ") .. "}")
				end
			end
		else
			if importDB[k] == nil then
				importDB[k] = v
				table.insert(missingKeys, tostring(k))
			end
		end
	end

	return importDB, missingKeys, nil
end

-- 临时变量
ns.PCExportStr = ""
ns.PCPendingImportDB = nil

function ns.SetImportDB(text)
	if not text or text == "" then
		print("|cffff0000[PlateColor]|r " .. L["请输入配置字符串"])
		return
	end
	local importDB, parseErr = ns.StringToTable(text)
	if not importDB then
		print("|cffff0000[PlateColor]|r " .. L["字符串解析失败"] .. tostring(parseErr))
		return
	end
	local mergedDB, missingKeys, mergeErr = ns.ValidateAndMergeImport(importDB)
	if not mergedDB then
		print("|cffff0000[PlateColor]|r " .. L["配置校验失败"] .. tostring(mergeErr))
		return
	end
	if missingKeys and #missingKeys > 0 then
		print("|cffffff00[PlateColor]|r " .. L["以下配置项缺失"])
		for _, key in ipairs(missingKeys) do
			print("|cffffff00  - " .. key .. "|r")
		end
		ns.PCPendingImportDB = mergedDB
		StaticPopup_Show("PLATECOLOR_IMPORT_CONFIRM")
	else
		PlateColorDB = mergedDB
		print("|cff00ff00[PlateColor]|r " .. L["配置已导入"])
		ReloadUI()
	end
end

StaticPopupDialogs["PLATECOLOR_IMPORT_CONFIRM"] = {
	text = L["缺失配置确认"],
	button1 = HUD_EDIT_MODE_IMPORT_LAYOUT,
	button2 = CANCEL,
	timeout = 0,
	whileDead = true,
	hideOnEscape = true,
	OnAccept = function(self)
		if ns.PCPendingImportDB then
			PlateColorDB = ns.PCPendingImportDB
			ns.PCPendingImportDB = nil
			print("|cff00ff00[PlateColor]|r " .. L["配置已导入"])
			ReloadUI()
		end
	end,
	OnCancel = function(self)
		ns.PCPendingImportDB = nil
	end,
}

StaticPopupDialogs["PLATECOLOR_REDB"] = {
	text = L["继续执行将失去现有的配置\n是否需要恢复默认"],
	button1 = OKAY,
	button2 = CANCEL,
	timeout = 0,
	whileDead = true,
	hideOnEscape = true,
	OnAccept = function(self)
		PlateColorDB = ns.PlateColorDB
		ReloadUI()
	end,
	OnCancel = function() end,
}