-- Core/Commands.lua
-- Slash commands. /twid (also /tweakid, /ti). Loads last so all modules exist.

local ADDON, ns = ...

local C = {}
ns.Commands = C

-- Keep this list in sync with C.Handle below: every recognized command/alias
-- and every unrecognized-parameter path routes here. `unknown` (optional) names
-- the offending token so the user sees what was rejected before the full list.
local function help(unknown)
	if unknown and unknown ~= "" then
		ns.Print("unknown command: " .. ns.Colorize("ffff4040", unknown))
	end
	ns.Print("commands:")
	print("  |cffffff00/twid|r                 toggle the window")
	print("  |cffffff00/twid pick|r            pick frame(s) under the cursor to monitor")
	print("  |cffffff00/twid identify|r        hover-identify only (who made this frame?)")
	print("  |cffffff00/twid name <Frame>|r    select by global name (expands numbered siblings)")
	print("  |cffffff00/twid start|r / |cffffff00stop|r    start / stop monitoring")
	print("  |cffffff00/twid timed [sec]|r     monitor for N seconds then auto-stop (default 30)")
	print(ns.HasSourceLocation
		and "  |cffffff00/twid deep|r [|cffffff00on|r/|cffffff00off|r]  toggle deep investigation (frame creators + hidden driver changes; needs reload)"
		or  "  |cffffff00/twid deep|r [|cffffff00on|r/|cffffff00off|r]  toggle deep investigation (hidden driver changes; needs reload)")
	print("  |cffffff00/twid deep persist|r [|cffffff00on|r/|cffffff00off|r]  keep deep enabled across reloads")
	print("  |cffffff00/twid clear|r / |cffffff00copy|r / |cffffff00saved|r   log / copy dump / saved investigations")
	print("  |cffffff00/twid simple|r / |cffffff00detail|r  picker tooltip verbosity")
	print("  |cffffff00/twid show|r / |cffffff00hide|r    force the window open / closed")
	print("Pick mode: Up/Down depth, Enter select, Esc cancel.")
end

function C.Handle(msg)
	msg = (msg or ""):gsub("^%s+", ""):gsub("%s+$", "")
	local cmd, rest = msg:match("^(%S*)%s*(.-)$")
	cmd = (cmd or ""):lower()

	if cmd == "" or cmd == "toggle" then ns.UI.Toggle()
	elseif cmd == "pick" then ns.Picker.Start(true)
	elseif cmd == "identify" or cmd == "hover" then ns.Picker.Start(false)
	elseif cmd == "name" or cmd == "select" then ns.Picker.SelectByName(rest)
	elseif cmd == "start" or cmd == "monitor" or cmd == "on" then ns.Monitor.Start()
	elseif cmd == "stop" or cmd == "off" then ns.Monitor.Stop()
	elseif cmd == "timed" then
		if rest == "" then ns.Monitor.StartTimed(30)
		elseif tonumber(rest) then ns.Monitor.StartTimed(tonumber(rest))
		else help("timed " .. rest) end
	elseif cmd == "deep" then
		local sub, arg = rest:lower():match("^(%S*)%s*(.-)$")
		if sub == "" then ns.Deep.Toggle()
		elseif sub == "off" then ns.Deep.Stop()
		elseif sub == "on" then ns.Deep.Arm()
		elseif sub == "persist" or sub == "keep" or sub == "permanent" then
			if arg == "" then ns.Deep.TogglePersist()
			elseif arg == "off" then ns.Deep.SetPersist(false)
			elseif arg == "on" then ns.Deep.SetPersist(true)
			else help("deep persist " .. arg) end
		else help("deep " .. sub) end
	elseif cmd == "clear" then ns.Monitor.Clear()
	elseif cmd == "copy" then ns.UI.CopyAll()
	elseif cmd == "saved" then ns.UI.ShowSaved()
	elseif cmd == "simple" then ns.db.simpleView = true; ns.Print("Picker: simple view.")
	elseif cmd == "detail" then ns.db.simpleView = false; ns.Print("Picker: detail view.")
	elseif cmd == "show" then ns.UI.Show()
	elseif cmd == "hide" then ns.UI.Hide()
	else help(cmd) end
end

SLASH_TWEAKIDENTIFIER1 = "/tweakid"
SLASH_TWEAKIDENTIFIER2 = "/twid"
SLASH_TWEAKIDENTIFIER3 = "/ti"
SlashCmdList["TWEAKIDENTIFIER"] = function(msg) C.Handle(msg) end
