local W, F, E, L = unpack((select(2, ...))) ---@type WindTools, Functions, ElvUI, LocaleTable
local S = W.Modules.Skins ---@type Skins

local _G = _G
local abs = abs
local hooksecurefunc = hooksecurefunc
local pairs = pairs

local RunNextFrame = RunNextFrame

local TEX_PREFIX = "Interface\\AddOns\\MountRoutePlanner\\Assets\\"

local function trySkin(func)
	return function(button)
		if button.__wind then
			return
		end

		if func(button) ~= false then
			button.__wind = true
		end
	end
end

local function reskinTextButton(button)
	if not button.Text then
		return false
	end

	S:Proxy("HandleButton", button)

	if W.ChineseLocale then
		button:Width(80)
	end
end

local function closeButton(button)
	if not S:IsTexturePathEqual(button:GetNormalTexture(), TEX_PREFIX .. "Frame_Close.tga") then
		return false
	end

	S:Proxy("HandleCloseButton", button)
end

local function settingButton(button)
	if not S:IsTexturePathEqual(button:GetNormalTexture(), TEX_PREFIX .. "Frame_Settings.tga") then
		return false
	end

	S:ReskinIconButton(button, W.Media.Icons.buttonSetting, 14)
end

local function resetButton(button)
	if not S:IsTexturePathEqual(button:GetNormalTexture(), TEX_PREFIX .. "Frame_Reset.tga") then
		return false
	end

	S:ReskinIconButton(button, W.Media.Icons.buttonGoStart, 18)
end

local function discordButton(button)
	if not S:IsTexturePathEqual(button:GetNormalTexture(), TEX_PREFIX .. "Frame_Discord.tga") then
		return false
	end

	S:ReskinIconButton(button, W.Media.Icons.buttonDiscord, 15)
end

local function mountButton(button)
	local width, height = button:GetSize()
	if not width or not height or abs(width - 32) > 0.01 or abs(height - 32) > 0.01 then
		return false
	end

	button:GetNormalTexture():SetTexCoords()
	button:GetHighlightTexture():SetTexCoords()
	button:CreateBackdrop()
end

local function actionButton(button)
	local width, height = button:GetSize()
	if not width or not height or abs(width - 40) > 0.01 or abs(height - 40) > 0.01 then
		return false
	end

	button:GetNormalTexture():SetTexCoords()
	button:GetHighlightTexture():SetTexCoords()
	button:SetTemplate()
end

local function changelogFrame()
	local ChangelogFrame = _G.MRPChangelogFrame
	if not ChangelogFrame then
		return
	end

	ChangelogFrame:SetTemplate("Transparent")
	S:CreateShadow(ChangelogFrame)

	local ScrollBar = ChangelogFrame.scrollFrame and ChangelogFrame.scrollFrame.ScrollBar
	if ScrollBar then
		S:Proxy("HandleScrollBar", ScrollBar)
	end

	local dismissCheckbox = ChangelogFrame.dismissCheckbox
	if dismissCheckbox then
		S:Proxy("HandleCheckBox", dismissCheckbox)
		dismissCheckbox:Size(24)
	end

	for _, child in pairs({ ChangelogFrame:GetChildren() }) do
		local objectType = child.GetObjectType and child:GetObjectType()
		if objectType == "Button" then
			if child:GetNumRegions() == 4 then
				trySkin(closeButton)(child)
			end
		end
	end
end

function S:MountRoutePlanner()
	if not E.private.WT.skins.enable or not E.private.WT.skins.addons.mountRoutePlanner then
		return
	end

	RunNextFrame(changelogFrame)

	local frame = _G.MRP_Frame
	if not frame then
		return
	end

	frame:SetTemplate("Transparent")
	self:CreateShadow(frame)

	frame.progressBar:SetTexture(E.media.normTex)
	frame.progressBar:SetVertexColor(W.Utilities.Color.ExtractRGBFromTemplate("green-500"))
	frame.progressBarBG:Kill()
	frame.progressBar:CreateBackdrop()
	frame.progressBar.backdrop:SetOutside(frame.progressBarBG)

	for _, region in pairs({ frame:GetRegions() }) do
		if region.GetObjectType and region:GetObjectType() == "FontString" then
			F.SetFont(region)
		end
	end

	for _, child in pairs({ frame:GetChildren() }) do
		local objectType = child.GetObjectType and child:GetObjectType()
		if objectType == "Button" then
			trySkin(discordButton)(child)
			trySkin(resetButton)(child)
			trySkin(settingButton)(child)
			trySkin(closeButton)(child)
			trySkin(reskinTextButton)(child)
			trySkin(actionButton)(child)
		elseif objectType == "CheckButton" then
			self:Proxy("HandleCheckBox", child)
			child:Size(24)
		end
	end

	hooksecurefunc(frame.stepText, "SetText", function()
		if frame.rewardIcons then
			for _, button in pairs(frame.rewardIcons) do
				trySkin(mountButton)(button)
			end
		end
	end)
end

-- function S:MountRoutePlannerOptions(frame)
-- 	--TODO: Skin the options frame if needed
-- end

S:AddCallbackForAddon("MountRoutePlanner")
-- S:ReskinSettingFrame("Mount Route Planner", "MountRoutePlannerOptions")
