local _G = getfenv(0)

FramesResized_SV = FramesResized_SV or {}
FramesResized_SV.GuildUI_Resize         = FramesResized_SV.GuildUI_Resize ~= nil         and FramesResized_SV.GuildUI_Resize         or true
FramesResized_SV.GuildUI_Moveable       = FramesResized_SV.GuildUI_Moveable ~= nil       and FramesResized_SV.GuildUI_Moveable       or true
FramesResized_SV.LootFrame_Resize       = FramesResized_SV.LootFrame_Resize ~= nil       and FramesResized_SV.LootFrame_Resize       or true
FramesResized_SV.RaidInfo_Resize        = FramesResized_SV.RaidInfo_Resize ~= nil        and FramesResized_SV.RaidInfo_Resize        or true
FramesResized_SV.TraidSkillUI_Resize    = FramesResized_SV.TraidSkillUI_Resize ~= nil    and FramesResized_SV.TraidSkillUI_Resize    or true
FramesResized_SV.TraidSkillUI_Moveable  = FramesResized_SV.TraidSkillUI_Moveable ~= nil  and FramesResized_SV.TraidSkillUI_Moveable  or true
FramesResized_SV.TrainerUI_Resize       = FramesResized_SV.TrainerUI_Resize ~= nil       and FramesResized_SV.TrainerUI_Resize       or true
FramesResized_SV.TrainerUI_Moveable     = FramesResized_SV.TrainerUI_Moveable ~= nil     and FramesResized_SV.TrainerUI_Moveable     or true


local original_GuildPerksToggleButton_OnClick
local original_LootFrame_OnShow

local additionalFrameHeight = 128

-- -----
-- Hook Functions
-- -----

local function FR_Hooks_GuildPerksToggleButton_OnClick(self)
	original_GuildPerksToggleButton_OnClick(self)
	if not GuildMainFrame.allPerks then
		GuildPerksToggleButton:SetPoint("TOPLEFT", GuildFrameInset, 0, -(192 + additionalFrameHeight))
	end
end

local function FR_Hooks_LootFrame_OnShow(self)
	if GetNumLootItems() > LOOTFRAME_NUMBUTTONS_ORG then
		LOOTFRAME_NUMBUTTONS = LOOTFRAME_NUMBUTTONS_ORG + LOOTFRAME_ADDBUTTONS
		LootFramePrev:SetPoint("BOTTOMLEFT", LootFrame_MidTextures, "BOTTOMLEFT", 57, 27)
		LootFrameNext:SetPoint("BOTTOMRIGHT", LootFrame_MidTextures, "BOTTOMLEFT", 142, 27)
		LootFrameUpButton:SetPoint("BOTTOMLEFT", LootFrame_MidTextures, "BOTTOMLEFT", 25, 16)
		LootFrameDownButton:SetPoint("BOTTOMLEFT", LootFrame_MidTextures, "BOTTOMLEFT", 143, 16)
		LootFrame_MidTextures:SetFrameLevel(LootFrame:GetFrameLevel() + 1)
		LootFrame_MidTextures:Show()
	else
		LOOTFRAME_NUMBUTTONS = LOOTFRAME_NUMBUTTONS_ORG
		LootFramePrev:SetPoint("BOTTOMLEFT", LootFrame, "BOTTOMLEFT", 57, 27)
		LootFrameNext:SetPoint("BOTTOMRIGHT", LootFrame, "BOTTOMLEFT", 142, 27)
		LootFrameUpButton:SetPoint("BOTTOMLEFT", LootFrame, "BOTTOMLEFT", 25, 16)
		LootFrameDownButton:SetPoint("BOTTOMLEFT", LootFrame, "BOTTOMLEFT", 143, 16)
		LootFrame_MidTextures:Hide()
	end
	original_LootFrame_OnShow(self)
end

-- -----
-- Resize/Moveable Functions
-- -----

local function FR_GuildUI_Resize()
	local GUILD_ROSTER_BUTTON_OFFSET = 2 -- Blizzard_GuildRoster.lua

	GuildFrame:SetHeight(424 + additionalFrameHeight)

	GuildInfoFrameBar1Left:SetPoint("BOTTOM", GuildInfoFrame, "TOP", 0, -(145 + additionalFrameHeight))
	GuildInfoEventsContainer:SetHeight(108 + additionalFrameHeight)
	HybridScrollFrame_CreateButtons(GuildInfoEventsContainer, "GuildNewsButtonTemplate", 0, 0, "TOPLEFT", "TOPLEFT", 0, 0, "TOP", "BOTTOM")
	local buttons = GuildInfoEventsContainer.buttons
	for i = 1, #buttons do
		buttons[i].isEvent = true
	end

	GuildNewsContainer:SetHeight(305 + additionalFrameHeight)
	HybridScrollFrame_CreateButtons(GuildNewsContainer, "GuildNewsButtonTemplate", 0, 0)

	GuildPerksContainer:SetHeight(288 + additionalFrameHeight)
	HybridScrollFrame_CreateButtons(GuildPerksContainer, "GuildPerksButtonTemplate", 8, 0, "TOPLEFT", "TOPLEFT", 0, 0, "TOP", "BOTTOM")
	GuildNewPerksFrameFaction:SetPoint("TOPRIGHT", -20, -(34 + additionalFrameHeight))
	-- fix background heights; has no name so scan over all textures for the frame
	for _, region in pairs({GuildNewPerksFrame:GetRegions()}) do
		if region:IsObjectType("Texture") and math.floor(region:GetHeight() + 0.5) == 162 then
			region:SetHeight(162 + additionalFrameHeight)
		end
	end
	for _, region in pairs({GuildAllPerksFrame:GetRegions()}) do
		if region:IsObjectType("Texture") and math.floor(region:GetHeight() + 0.5) == 288 then
			region:SetHeight(288 + additionalFrameHeight)
		end
	end
	if not GuildMainFrame.allPerks then
		GuildPerksToggleButton:SetPoint("TOPLEFT", GuildFrameInset, 0, -(192 + additionalFrameHeight))
	end

	GuildRewardsContainer:SetHeight(306 + additionalFrameHeight)
	HybridScrollFrame_CreateButtons(GuildRewardsContainer, "GuildRewardsButtonTemplate", 1, 0)
	GuildRewardsFrameBg:SetHeight(308 + additionalFrameHeight)

	GuildRosterContainer:SetHeight(300 + additionalFrameHeight)
	HybridScrollFrame_CreateButtons(GuildRosterContainer, "GuildRosterButtonTemplate", 0, 0, "TOPLEFT", "TOPLEFT", 0, -GUILD_ROSTER_BUTTON_OFFSET, "TOP", "BOTTOM")
	GuildRoster_SetView(GetCVar("guildRosterView"))

	original_GuildPerksToggleButton_OnClick = GuildPerksToggleButton:GetScript("OnClick")
	GuildPerksToggleButton:SetScript("OnClick", FR_Hooks_GuildPerksToggleButton_OnClick)
end

local function FR_GuildUI_Moveable()
	GuildFrame:SetMovable(true)
	GuildFrame:SetScript("OnMouseDown", function(self, button) if button == "LeftButton" then GuildFrame:StartMoving() end end)
	GuildFrame:SetScript("OnMouseUp", function(self, button) if button == "LeftButton" then GuildFrame:StopMovingOrSizing() end end)
end

local function FR_LootFrame_Resize()
	local frame = CreateFrame("Frame", "LootFrame_MidTextures", LootFrame, "FRTemplate_LootFrame_MidTextures")
	frame:ClearAllPoints()
	frame:SetPoint("TOPLEFT", LootFrame, "TOPLEFT")

	LOOTFRAME_ADDBUTTONS = 4
	LOOTFRAME_NUMBUTTONS_ORG = LOOTFRAME_NUMBUTTONS
	LOOTFRAME_NUMBUTTONS = LOOTFRAME_NUMBUTTONS_ORG + LOOTFRAME_ADDBUTTONS

	for index = LOOTFRAME_NUMBUTTONS_ORG + 1, LOOTFRAME_NUMBUTTONS do
		frame = CreateFrame("Button", "LootButton"..index, LootFrame_MidTextures, "LootButtonTemplate")
		frame:SetPoint("TOP", _G["LootButton"..(index-1)], "BOTTOM", 0, -4)
		frame:SetID(index)
	end

	LootFramePrev:SetPoint("BOTTOMLEFT", LootFrame_MidTextures, "BOTTOMLEFT", 57, 27)
	LootFrameNext:SetPoint("BOTTOMRIGHT", LootFrame_MidTextures, "BOTTOMLEFT", 142, 27)
	LootFrameUpButton:SetPoint("BOTTOMLEFT", LootFrame_MidTextures, "BOTTOMLEFT", 25, 16)
	LootFrameDownButton:SetPoint("BOTTOMLEFT", LootFrame_MidTextures, "BOTTOMLEFT", 143, 16)

	LootFrame_MidTextures:SetFrameLevel(LootFrame:GetFrameLevel() + 1)
	for index = 1, LOOTFRAME_NUMBUTTONS do
		button = _G["LootButton"..index]
		button:SetFrameLevel(LootFrame_MidTextures:GetFrameLevel() + 1)
	end
--	LootFramePrev:SetFrameLevel(LootFrame_MidTextures:GetFrameLevel() + 1)
--	LootFrameNext:SetFrameLevel(LootFrame_MidTextures:GetFrameLevel() + 1)

	original_LootFrame_OnShow = LootFrame:GetScript("OnShow")
	LootFrame:SetScript("OnShow", FR_Hooks_LootFrame_OnShow)
end

local function FR_RaidInfo_Resize()
	RaidInfoFrame:SetHeight(250 + 158)
	RaidInfoScrollFrame:SetHeight(157 + 158)
	HybridScrollFrame_CreateButtons(RaidInfoScrollFrame, "RaidInfoInstanceTemplate")
end

local function FR_TradeSkillUI_Resize()
	TRADE_SKILL_ADDLINES = floor(additionalFrameHeight / TRADE_SKILL_HEIGHT)
	TRADE_SKILLS_DISPLAYED_ORG = TRADE_SKILLS_DISPLAYED
	TRADE_SKILLS_DISPLAYED = TRADE_SKILLS_DISPLAYED_ORG + TRADE_SKILL_ADDLINES

	TradeSkillFrame:SetHeight(424 + additionalFrameHeight)
	TradeSkillHorizontalBarLeft:SetPoint("TOPLEFT", 2, -(208 + additionalFrameHeight))
	TradeSkillListScrollFrame:SetHeight(130 + additionalFrameHeight)

--[[
				<Texture name="$parentMiddle" file="Interface\PaperDollInfoFrame\UI-Character-ScrollBar">
					<Anchors>
						<Anchor point="TOPLEFT" relativeTo="$parentTop" relativePoint="BOTTOMLEFT"/>
						<Anchor point="BOTTOMRIGHT" relativeTo="$parentBottom" relativePoint="TOPRIGHT"/>
					</Anchors>
					<TexCoords left="0" right="0.484375" top="0.1640625" bottom="1"/>
				</Texture>

				<Texture urgency="5" file="Interface\ClassTrainerFrame\UI-ClassTrainer-ScrollBar">
					<Size>
						<AbsDimension x="30" y="120"/>
					</Size>
					<Anchors>
						<Anchor point="TOPLEFT" relativePoint="TOPRIGHT">
							<Offset>
								<AbsDimension x="-3" y="2"/>
							</Offset>
						</Anchor>
					</Anchors>
					<TexCoords left="0" right="0.46875" top="0.0234375" bottom="0.9609375"/>
				</Texture>
				<Texture urgency="5" file="Interface\ClassTrainerFrame\UI-ClassTrainer-ScrollBar">
					<Size>
						<AbsDimension x="30" y="123"/>
					</Size>
					<Anchors>
						<Anchor point="BOTTOMLEFT" relativePoint="BOTTOMRIGHT">
							<Offset>
								<AbsDimension x="-3" y="-2"/>
							</Offset>
						</Anchor>
					</Anchors>
					<TexCoords left="0.53125" right="1.0" top="0.03125" bottom="1.0"/>
				</Texture>
]]
	local ScrollBarMid = TradeSkillListScrollFrame:CreateTexture(nil, "BACKGROUND")
	ScrollBarMid:SetPoint("TOPLEFT", TradeSkillListScrollFrame, "TOPRIGHT", -3, (2 - 120))
	ScrollBarMid:SetPoint("BOTTOMRIGHT", TradeSkillListScrollFrame, "BOTTOMRIGHT", -3 + 30 + 1, -2 + 123)
	ScrollBarMid:SetTexture([[Interface\ClassTrainerFrame\UI-ClassTrainer-ScrollBar]])
	ScrollBarMid:SetTexCoord(0, 0.484375, 0.25, 0.75)

	for index = TRADE_SKILLS_DISPLAYED_ORG + 1, TRADE_SKILLS_DISPLAYED do
		frame = CreateFrame("Button", "TradeSkillSkill"..index, TradeSkillFrame, "TradeSkillSkillButtonTemplate")
		frame:SetPoint("TOPLEFT", _G["TradeSkillSkill"..(index-1)], "BOTTOMLEFT")
	end
end

local function FR_TradeSkillUI_Moveable()
	TradeSkillFrame:SetScript("OnMouseDown", function(self, button) if button == "LeftButton" then TradeSkillFrame:StartMoving() end end)
	TradeSkillFrame:SetScript("OnMouseUp", function(self, button) if button == "LeftButton" then TradeSkillFrame:StopMovingOrSizing() end end)
end

local function FR_TrainerUI_Resize()
	CLASS_TRAINER_SCROLL_HEIGHT_ORG = CLASS_TRAINER_SCROLL_HEIGHT
	CLASS_TRAINER_SCROLL_HEIGHT = CLASS_TRAINER_SCROLL_HEIGHT_ORG + additionalFrameHeight
	CLASS_TRAINER_SKILLS_DISPLAYED_ORG = CLASS_TRAINER_SKILLS_DISPLAYED
	CLASS_TRAINER_SKILLS_DISPLAYED = floor(CLASS_TRAINER_SCROLL_HEIGHT / CLASS_TRAINER_SKILL_HEIGHT)

	ClassTrainerFrame:SetHeight(424 + additionalFrameHeight)
	ClassTrainerFrame.scrollFrame:SetHeight(CLASS_TRAINER_SCROLL_HEIGHT)
	HybridScrollFrame_CreateButtons(ClassTrainerFrame.scrollFrame, "ClassTrainerSkillButtonTemplate", 1, -1, "TOPLEFT", "TOPLEFT", 0, 0, "TOP", "BOTTOM")
end

local function FR_TrainerUI_Moveable()
	ClassTrainerFrame:SetScript("OnMouseDown", function(self, button) if button == "LeftButton" then ClassTrainerFrame:StartMoving() end end)
	ClassTrainerFrame:SetScript("OnMouseUp", function(self, button) if button == "LeftButton" then ClassTrainerFrame:StopMovingOrSizing() end end)
end

-- -----
-- Event Handling
-- -----

local FR_frame = CreateFrame("Frame", "FramesResizedFrame")

FR_frame:RegisterEvent("PLAYER_LOGIN")

FR_frame:SetScript("OnEvent", function(self, event, arg1)
	if event == "ADDON_LOADED" then
		if		arg1 == "Blizzard_GuildUI" then
			if FramesResized_SV.GuildUI_Resize		then FR_GuildUI_Resize() end
			if FramesResized_SV.GuildUI_Moveable	then FR_GuildUI_Moveable() end
		elseif	arg1 == "Blizzard_TradeSkillUI" then
			if FramesResized_SV.TraidSkillUI_Resize		then FR_TradeSkillUI_Resize() end
			if FramesResized_SV.TraidSkillUI_Moveable	then FR_TradeSkillUI_Moveable() end
		elseif	arg1 == "Blizzard_TrainerUI" then
			if FramesResized_SV.TrainerUI_Resize		then FR_TrainerUI_Resize() end
			if FramesResized_SV.TrainerUI_Moveable		then FR_TrainerUI_Moveable() end
		end
	elseif event == "PLAYER_LOGIN" then
		if FramesResized_SV.LootFrame_Resize			then FR_LootFrame_Resize() end
		if FramesResized_SV.RaidInfo_Resize				then FR_RaidInfo_Resize() end
		if IsAddOnLoaded("Blizzard_GuildUI") then
			if FramesResized_SV.GuildUI_Resize		then FR_GuildUI_Resize() end
			if FramesResized_SV.GuildUI_Moveable	then FR_GuildUI_Moveable() end
		end
		if IsAddOnLoaded("Blizzard_TradeSkillUI") then
			if FramesResized_SV.TraidSkillUI_Resize		then FR_TradeSkillUI_Resize() end
			if FramesResized_SV.TraidSkillUI_Moveable	then FR_TradeSkillUI_Moveable() end
		end
		if IsAddOnLoaded("Blizzard_TrainerUI") then
			if FramesResized_SV.TrainerUI_Resize		then FR_TrainerUI_Resize() end
			if FramesResized_SV.TrainerUI_Moveable		then FR_TrainerUI_Moveable() end
		end
		FR_frame:RegisterEvent("ADDON_LOADED")
	end
end)
