Quantcast
local A, L = ...

local w, h = unpack(L.C.bar.small)

local function SetupInstance(instance)
    if instance.styled then return end
    if not instance.baseframe then return end

    instance:ChangeSkin("Minimalistic")
    instance:InstanceWallpaper(false)
    instance:DesaturateMenu(true)
	instance:SetAutoHideMenu(true)
    instance:HideMainIcon(true)
    instance:SetBackdropTexture("Details Ground")
	instance:SetBackgroundAlpha(0)
	instance:InstanceColor(0, 0, 0, 0)
	instance:SetBarFollowPlayer(true)
	instance:SetWindowScale(L.C.scale, true)
    -- instance:MenuAnchor(16, 3)
    -- instance:ToolbarMenuButtonsSize(1)
    instance:AttributeMenu(false)
	instance:SetBarSettings(
		h,						-- bar height
		"Blizzard Raid Bar",	-- bar texture name of medialib
		true,					-- use class color
		{0, 0, 0, 0},			-- fixed color
		"Solid",				-- bg texture name of medialib
		false, 					-- bg use class color
		L.C.thin.bgColor,		-- fixed bg color
		0,						-- alpha
		"",						-- icon file
		true,					-- start after icon
		L.C.bar.space.small,	-- bar space
		""						-- custom texture file, u can use L.C.bar.tex or "" as default
	)

	instance:SetBarBackdropSettings(true, 1, {0,0,0,1}, "1 Pixel")
	instance:SetBarTextSettings(12, "Expressway", {1,1,1,1}, false, false, true, true)
	instance:SetBarSpecIconSettings(false)
	-- instance:SetBarRightTextSettings(false, true, false, "NONE")  -- just "dps/hps" no total and percent

    instance.styled = true
end

local function EmbedWindow(instance, x, y, width, height)
	if not instance.baseframe then return end
	instance.baseframe:ClearAllPoints()
	instance.baseframe:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", x, y)
	instance:SetSize(width, height)
	instance:SaveMainWindowPosition()
	instance:RestoreMainWindowPosition()
	instance:LockInstance(true)
end

local function Details()
	if not IsAddOnLoaded("Details") then return end

    local Details = _G.Details
	-- instance table can be nil sometimes
	Details.tabela_instancias = Details.tabela_instancias or {}
	Details.instances_amount = Details.instances_amount or 5
	-- toggle windows on init
	Details:ReabrirTodasInstancias()
	-- disable group
	Details.disable_lock_ungroup_buttons = false

	local index = 1
	local instance = Details:GetInstance(index)
	while instance do
		SetupInstance(instance)
		index = index + 1
		instance = Details:GetInstance(index)
	end

	-- Reanchor
	local instance1 = Details:GetInstance(1)
	local instance2 = Details:GetInstance(2)

	local height = (h+L.C.bar.space.small)*5 + 4
	local width = w

	if rSkinDB.resets.details then
		if instance1 then
			EmbedWindow(instance1, -10, 50, width, height)
			if instance2 then
				EmbedWindow(instance2, -15-width, 50, width, height)
			end

			rSkinDB.resets.details = false
		end
	end

	local listener = Details:CreateEventListener()
	listener:RegisterEvent("DETAILS_INSTANCE_OPEN")
	function listener:OnDetailsEvent(event, instance)
		if event == "DETAILS_INSTANCE_OPEN" then
			if not instance.styled and instance:GetId() == 2 then
				instance:SetSize(width, height)
				EmbedWindow(instance, -15-width, 50, width, height)
			end
			SetupInstance(instance)
		end
	end

	Details.OpenWelcomeWindow = function()
		if instance1 then
			EmbedWindow(instance1, -10, 50, width, height)
			SetupInstance(instance1)
		end
	end
end

rSkin:RegisterSkin("Details", Details)