local _G = _G -- import globals for faster usage

local font = "Interface\\AddOns\\!BeautyLoot\\Fonts\\vixar.ttf"

for i = 1, 2 do
    select(i, _G["LootFrame"]:GetRegions()):SetAlpha(0)
end
_G["LootCloseButton"]:Hide() -- cba lol
_G["LootFramePortraitOverlay"]:SetAlpha(0)
	
local LootBG = CreateFrame('Frame', nil, _G["LootFrame"])
LootBG:SetPoint('TOPLEFT', 1, -1)
LootBG:SetPoint('RIGHT', -1, 1)
LootBG:SetFrameStrata("LOW")	
LootBG:SetBackdrop({			
    bgFile = "Interface\\Buttons\\WHITE8x8",
	insets = {top = 12, left = 15, bottom = 6, right = 66},
})
LootBG:SetBackdropColor(0,0,0,0.75)
CreateBorderLight(LootBG, 7, .25, .25, .25, -14, -11, -65, -11, -14, -6, -65, -6)

for i = 1, 4 do
    for _, lootfont in pairs({
       _G['LootButton'..i..'Text'],
    }) do
        lootfont:SetFont(font, 12, "THINOUTLINE")
        lootfont:SetShadowOffset(1, -1)
	    lootfont:SetShadowColor(0, 0, 0)
    end
	for _, lootcount in pairs({
	    _G['LootButton'..i..'Count'],
	}) do
	    lootcount:SetFont(font, 14, "THINOUTLINE")
		lootcount:SetPoint('BOTTOMRIGHT', -2, 3) -- place the count text where its more seenable.
        lootcount:SetShadowOffset(1, -1)
	    lootcount:SetShadowColor(0, 0, 0)
	end
end

hooksecurefunc("LootFrame_UpdateButton", function(index)
	local texture, item, quantity, quality, locked, isQuestItem, questId, isActive = GetLootSlotInfo(index)
	CreateBorderLight(_G["LootButton"..index], 7, .25, .25, .25, 1)
	_G["LootButton"..index.."IconQuestTexture"]:SetAlpha(0) -- hide that pesky quest item texture
	_G["LootButton"..index.."NameFrame"]:SetAlpha(0) -- hide sucky fagdrops :D
	if isQuestItem then
	    ColorBorder(_G["LootButton"..index], 1.0, 0.82, 0)
	else
        ColorBorder(_G["LootButton"..index], .25, .25, .25)	
	end	
end)

------------------------------------------------------------------------------------------------------------
-- Improved Loot Frame - http://www.curse.com/addons/wow/improved-loot-frame -- by Cybeloras of Mal'Ganis --
------------------------------------------------------------------------------------------------------------

-- Woah, nice coding, blizz. Anchor something positioned at the top of the frame to the center of the frame instead, and make it an anonymous font string so I have to work to find it
local i, t = 1, "Interface\\LootFrame\\UI-LootPanel"
while true do
	local r = select(i, _G["LootFrame"]:GetRegions())
	if not r then break end
	if r.GetText and r:GetText() == ITEMS then
		r:ClearAllPoints()
		r:SetPoint("TOP", -12, -19.5)
	elseif r.GetTexture and r:GetTexture() == t then
		r:Hide()
	end
	i = i + 1
end

local p, r, x, y = "TOP", "BOTTOM", 0, -4
local buttonHeight = _G["LootButton1"]:GetHeight() + abs(y)
local baseHeight = _G["LootFrame"]:GetHeight() - (buttonHeight * LOOTFRAME_NUMBUTTONS)

local old_LootFrame_Show = LootFrame_Show
function LootFrame_Show(self, ...)
	_G["LootFrame"]:SetHeight(baseHeight + (GetNumLootItems() * buttonHeight))
	local num = GetNumLootItems()
	for i = 1, GetNumLootItems() do
		if i > LOOTFRAME_NUMBUTTONS then
			if not _G["LootButton"..i] then
				_G["LootButton"..i] = CreateFrame("Button", "LootButton"..i, LootFrame, "LootButtonTemplate", i)
			end
			LOOTFRAME_NUMBUTTONS = i
		end
		if i > 1 then
			_G["LootButton"..i]:ClearAllPoints()
			_G["LootButton"..i]:SetPoint(p, "LootButton"..(i-1), r, x, y)
		end
	end
	
	return old_LootFrame_Show(self, ...)
end

-- the following is inspired by http://us.battle.net/wow/en/forum/topic/2353268564 and is hacktastic
local framesRegistered = {}
local function populateframesRegistered(...)
	wipe(framesRegistered)
	for i = 1, select("#", ...) do
		framesRegistered[i] = select(i, ...)
	end
end

local old_LootButton_OnClick = LootButton_OnClick
function LootButton_OnClick(self, ...)
	populateframesRegistered(GetFramesRegisteredForEvent("ADDON_ACTION_BLOCKED"))
	
	for i, frame in pairs(framesRegistered) do
		frame:UnregisterEvent("ADDON_ACTION_BLOCKED") -- fuck the rice-a-roni! (Blizzard throws a false taint error when attemping to loot the coins from a mob when the coins are the only loot on the mob)
	end
	old_LootButton_OnClick(self, ...)
	
	for i, frame in pairs(framesRegistered) do
		frame:RegisterEvent("ADDON_ACTION_BLOCKED")
	end
end