----------------------------
-- Temp Draggable Frames
----------------------------

FluidFrames.FramesToSkipInCombat = {
	["SpellBookFrame"] = true,
	["WorldMapFrame"] = true, 
	["FriendsFrame"] = true,
	["KeyBindingFrame"] = true,
	["TradeSkillFrame"] = true,
	["TradeFrame"] = true,
	-- addon frames with secure parentage
	["BindPadMacroFrame"] = true,
}

FluidFrames.FramesToDragByParent = {
-- 	"PaperDollFrame",
	"ReputationFrame",
-- 	"SkillFrame",
-- 	"PVPFrameHonor",
-- 	"PetPaperDollFrame",
-- 	"PetPaperDollFrameCompanionFrame",
	"TokenFrame",
--  	"PVPFrame",
	"SendMailFrame",
	"ReforgingFrameInvisibleButton", -- doesn't affect reforging ui in 4.3
}

function FluidFrames.InitTempDraggableFrames()
	-- Make UIPanels temporarily draggable (position not saved)
	for frameName,info in pairs(UIPanelWindows) do
		if (info.area ~= "full") then
			local frame = _G[frameName]
			if (frame and frame.GetTitleRegion) then
				local titleRegion = frame:GetTitleRegion()
				if (not titleRegion) then
					titleRegion = frame:CreateTitleRegion()
				end
				if FluidFrames.FramesToSkipInCombat[frameName] and (InCombatLockdown() or UnitAffectingCombat("player") or UnitAffectingCombat("pet")) then
					-- 
				else
					frame:EnableMouse(1)
				end
			end
		end
	end
	
	-- Disable the mouse on a few child frames so that the parent is draggable
	for i,frameName in ipairs(FluidFrames.FramesToDragByParent) do
		local frame = _G[frameName]
		if (frame) then
			frame:EnableMouse(nil)
		end
	end

	-- Update Enabled (mostly for LoD)
	if (FluidFrames_SavedVars) then
		FluidFrames.ToggleTempDraggableFrames(FluidFrames_SavedVars.TempDraggable == "1")
		FluidFrames.ToggleTempDraggableTimeManager(FluidFrames_SavedVars.TempDraggableTimeManager == "1") -- Blizzard_TimeManager is LoD
	end
end

function FluidFrames.ToggleTempDraggableFrames(value)
	for frameName,info in pairs(UIPanelWindows) do
		if (info.area ~= "full") then
			local frame = _G[frameName]
			if (frame and frame.GetTitleRegion) then
				local titleRegion = frame:GetTitleRegion()
				if (titleRegion) then
					if (value) then
						titleRegion:SetAllPoints(frame)
					else
						titleRegion:ClearAllPoints()
						titleRegion:SetHeight(0)
						titleRegion:SetWidth(0)
					end
				end
			end
		end
	end
		
	if (value and AchievementFrame) then
		AchievementFrameHeader:EnableMouse(false)
	end
end

function FluidFrames.ToggleTempDraggableMinimap(value)
	local titleRegion = MinimapCluster:GetTitleRegion()
	if (not titleRegion) then
		if (value) then
			--Init
			MinimapCluster:SetScript("OnEnter", function()
				MinimapZoneTextButton:GetScript("OnEnter")(MinimapZoneTextButton)
			end)
			MinimapCluster:SetScript("OnLeave", function()
				MinimapZoneTextButton:GetScript("OnLeave")(MinimapZoneTextButton)
			end)
			titleRegion = MinimapCluster:CreateTitleRegion()
			titleRegion:SetAllPoints(MinimapCluster)
			MinimapZoneTextButton:EnableMouse(nil)
		end
	else
		if (value) then
			titleRegion:SetAllPoints(MinimapCluster)
		else
			titleRegion:ClearAllPoints()
			titleRegion:SetHeight(0)
			titleRegion:SetWidth(0)
		end
	end
end

function FluidFrames.ToggleTempDraggableTimeManager(value)
	if not TimeManagerFrame then return end -- return if it hasn't loaded yet, will initialize later in ADDON_LOADED
	local titleRegion = TimeManagerFrame:GetTitleRegion()
	if (not titleRegion) then
		if (value) then
			--Init
			titleRegion = TimeManagerFrame:CreateTitleRegion()
			titleRegion:SetAllPoints(TimeManagerFrame)
		end
	else
		if (value) then
			titleRegion:SetAllPoints(TimeManagerFrame)
		else
			titleRegion:ClearAllPoints()
			titleRegion:SetHeight(0)
			titleRegion:SetWidth(0)
		end
	end
end