local W ---@class WindTools
local F, E, L ---@type Functions, ElvUI, LocaleTable
W, F, E, L = unpack((select(2, ...)))
local C = W.Utilities.Color ---@type ColorUtility

local _G = _G
local format = format
local gmatch = gmatch
local nop = nop
local pairs = pairs
local pcall = pcall
local strmatch = strmatch
local strsub = strsub
local tinsert = tinsert
local tonumber = tonumber
local unpack = unpack
local xpcall = xpcall

local InCombatLockdown = InCombatLockdown

local C_PartyInfo_InviteUnit = C_PartyInfo.InviteUnit
local C_UI_Reload = C_UI.Reload

local ACCEPT, CANCEL = ACCEPT, CANCEL

---@cast F Functions

W.RegisteredModules = {}
W.Changelog = {}

W:InitializeMetadata()

-- Alerts
E.PopupDialogs.WINDTOOLS_ELVUI_OUTDATED = {
	text = format(
		"%s %s",
		format(L["%s not been loaded since you are using an outdated version of ElvUI."], W.Title),
		format(L["Please upgrade your ElvUI to %2.2f or newer version!"], W.SupportElvUIVersion)
	),
	button1 = ACCEPT,
	hideOnEscape = 1,
}

E.PopupDialogs.WINDTOOLS_OPEN_CHANGELOG = {
	text = format(L["Welcome to %s %s!"], W.Title, W.DisplayVersion),
	button1 = L["Open Changelog"],
	button2 = C.StringByTemplate(L["Next Time"], "gray-300"),
	OnAccept = function(self)
		E:ToggleOptions("WindTools,information,changelog")
	end,
	hideOnEscape = 1,
}

E.PopupDialogs.WINDTOOLS_BUTTON_FIX_RELOAD = {
	text = format(
		"%s\n%s\n\n%s",
		format(
			L["%s detects CVar %s has been changed."],
			W.Title,
			C.StringByTemplate("ActionButtonUseKeyDown", "blue-400")
		),
		L["It will cause some buttons not to work properly before UI reloading."],
		C.StringByTemplate(
			format(L["You can disable this alert in [%s]-[%s]-[%s]"], W.Title, L["Advanced"], L["Game Fix"]),
			"neutral-300"
		)
	),
	button1 = L["Reload UI"],
	button2 = CANCEL,
	OnAccept = C_UI_Reload,
}

-- Keybinds
_G.BINDING_CATEGORY_ELLESMEREUI_WINDTOOLS = W.Title
for i = 1, 5 do
	_G["BINDING_HEADER_WTEXTRAITEMSBAR" .. i] =
		C.StringWithRGB(L["Extra Items Bar"] .. " " .. i, E.db.general.valuecolor)
	for j = 1, 12 do
		_G[format("BINDING_NAME_CLICK WTExtraItemsBar%dButton%d:LeftButton", i, j)] = L["Button"] .. " " .. j
	end
end

_G.BINDING_CATEGORY_ELLESMEREUI_WINDTOOLS_EXTRA = W.Title .. " - " .. L["Extra"]
_G.BINDING_HEADER_WTEXTRABUTTONS = L["Extra Buttons"]
_G["BINDING_NAME_CLICK WTExtraBindingButtonLogout:LeftButton"] = L["Logout"]
_G["BINDING_NAME_CLICK WTExtraBindingButtonLeaveGroup:LeftButton"] = L["Leave Party"]
_G["BINDING_NAME_CLICK WTExtraBindingButtonLeaveDelve:LeftButton"] = L["Leave Delve"]

-- WindTools Link Operations
-- 1. Print "|Hwtlink:feature:arg1:arg2:arg3:..." in the chat
-- 2. Click the link, it will trigger the corresponding function with the provided arguments
-- => W.LinkOperations[feature](arg1, arg2, arg3, ...)
W.LinkOperations = {
	["changelog"] = E.PopupDialogs.WINDTOOLS_OPEN_CHANGELOG.OnAccept,
	["invite"] = function(name)
		if name then
			C_PartyInfo_InviteUnit(name)
		end
	end,
}

---Registers a link operation function for a specific feature.
---
---**WindTools Link Operations**
---1. Print `|Hwtlink:feature:arg1:arg2:arg3:...` in the chat
---2. Click the link, it will trigger the corresponding function with the provided arguments
---```
---local func = W.LinkOperations[feature]
---func(arg1, arg2, arg3, ...)
---```
---
---@param feature string|nil The name/identifier of the feature registering the operation
---@param func function|nil The function to be called when the link operation is triggered
function W:RegisterLinkOperation(feature, func)
	if not feature or not func then
		return
	end

	W.LinkOperations[feature] = func
end

function W:ItemRefTooltip_SetHyperlink(_, data)
	if strsub(data, 1, 6) ~= "wtlink" then
		return
	end

	local feature, argsString = strmatch(data, "^wtlink:([^:]+)(.*)$")
	if not feature then
		return
	end

	local args = {}
	if argsString and argsString ~= "" then
		argsString = strsub(argsString, 2)
		for arg in gmatch(argsString, "[^:]+") do
			tinsert(args, arg)
		end
	end

	if feature and W.LinkOperations[feature] then
		W.LinkOperations[feature](unpack(args))
	end
end

function W:AddCustomLinkSupport()
	if not W:IsHooked(_G.ItemRefTooltip, "SetHyperlink") then
		W:Hook(_G.ItemRefTooltip, "SetHyperlink", "ItemRefTooltip_SetHyperlink", true)
	end
end

---Register a new module
---@param name string The name of the module
function W:RegisterModule(name)
	if not name then
		F.Developer.ThrowError("The name of module is required!")
		return
	end
	if self.initialized then
		self:GetModule(name):Initialize()
	else
		tinsert(self.RegisteredModules, name)
	end
end

-- WindTools module initialization
function W:InitializeModules()
	for _, module in pairs(W.Modules) do
		if module.OnInitialize and not module.__windtoolsInitialized then
			module.__windtoolsInitialized = true
			xpcall(module.OnInitialize, F.Developer.LogDebug, module)
		end
	end

	for _, moduleName in pairs(W.RegisteredModules) do
		local module = self:GetModule(moduleName)
		if module.Initialize then
			xpcall(module.Initialize, F.Developer.LogDebug, module)
		end
	end
end

-- WindTools module update after profile switch
function W:UpdateModules()
	if self.UpdateScripts then
		self:UpdateScripts()
	end
	for _, moduleName in pairs(self.RegisteredModules) do
		local module = W:GetModule(moduleName)
		if module.ProfileUpdate then
			pcall(module.ProfileUpdate, module)
		end
	end
end

-- Check ElvUI version, if not matched, show a popup to user
function W:CheckElvUIVersion()
	return true
end

-- Check install version, show changelog and run update scripts
function W:ChangelogReadAlert()
	local function versionValue(version)
		local major, minor, patch = tostring(version):match("^(%d+)%.(%d+)%.?(%d*)")
		if not major then
			return tonumber(version) or 0
		end
		return tonumber(major) * 1000000 + tonumber(minor) * 1000 + (tonumber(patch) or 0)
	end

	local readVer = versionValue(E.global.WT.changelogRead)
	local currentVer = versionValue(W.Version)
	if readVer < currentVer then
		if E.global.WT.core.changlogPopup and not InCombatLockdown() then
			E:StaticPopup_Show("WINDTOOLS_OPEN_CHANGELOG")
		else
			F.Print(
				format(L["Welcome to version %s!"], C.StringByTemplate(W.Version, "teal-400")),
				C.StringByTemplate(format("|Hwtlink:changelog::|h[%s]|h", L["Open Changelog"]), "sky-400")
			)
		end
	end
end

function W:GameFixing()
	if E.global.WT.core.cvarAlert then
		self:RegisterEvent("CVAR_UPDATE", function(_, cvar, value)
			if cvar == "ActionButtonUseKeyDown" and W.UseKeyDown ~= (value == "1") then
				E:StaticPopup_Show("WINDTOOLS_BUTTON_FIX_RELOAD")
			end
		end)
	end

	if E.global.WT.core.fixSetPassThroughButtons then
		_G.QuestPinMixin.SetPassThroughButtons = nop
		_G.BonusObjectivePinMixin.SetPassThroughButtons = nop
		_G.WorldQuestPinMixin.SetPassThroughButtons = nop
		_G.FlightPointPinMixin.SetPassThroughButtons = nop
	end
end
