local _G = getfenv(0)

function Skinner:FriendsFrame()
	if not self.db.profile.FriendsFrame or self.initialized.FriendsFrame then return end
	self.initialized.FriendsFrame = true

	self:SecureHook("FriendsFrame_ShowSubFrame", function(frameName)
--		self:Debug("FriendsFrame_ShowSubFrame: [%s, %s]", frameName, FriendsFrame.selectedTab)
		for i, v in pairs(FRIENDSFRAME_SUBFRAMES) do
--			self:Debug("FRIENDSFRAME_SUBFRAMES: [%s, %s]", i, v)
    		-- change the texture for the Active and Inactive tabs
	        if self.db.profile.TexturedTab then 
				-- handle Friends and Ignore on the same Tab
				j = ( i > 1 and i - 1 or i)
				-- handle additional Tabs with altered names or numbers 
				local prefix = (v == "BadapplesFrame" and "Badapples" or "")
				local tabId = ((v == "BadapplesFrame" or v == "CT_RAOptionsFrame") and 5 or j)
				-- ignore the IgnoreListFrame
				if i ~= 2 then self:setInactiveTab(_G[prefix.."FriendsFrameTab"..tabId]) end
				if v == frameName then
					self:setActiveTab(_G[prefix.."FriendsFrameTab"..tabId])
				end
			end 
			if v == "CT_RAOptionsFrame" then PanelTemplates_TabResize(0, FriendsFrameTab5, nil, 60) end
			-- adjust the width of the Badapples tab to fit the frame
			if v == "BadapplesFrame" then 
				local tabWidth = math.floor(BadapplesFriendsFrameTab5:GetWidth())
--				self:Debug("BaFFT5_GW: [%s]", tabWidth)
				if tabWidth == 94 then BadapplesFriendsFrameTab5:SetWidth(85) end
			end
		end
		end)

	self:SecureHook("GuildStatus_Update", function()
--		self:Debug("GuildStatus_Update")
		local _, _, _, xOfs, _ = GuildFrameGuildListToggleButton:GetPoint()
		if xOfs == 284 then
			self:moveObject(GuildFrameGuildListToggleButton, "+", 23, "-", 46)  
		else
			self:moveObject(GuildFrameGuildListToggleButton, nil, nil, "-", 46)
		end
		end)
	
-->>-- Friends Frame
	self:keepRegions(FriendsFrame, {6}) -- N.B. 6 is the text region
	
	FriendsFrame:SetWidth(FriendsFrame:GetWidth() * FxMult)
	FriendsFrame:SetHeight(FriendsFrame:GetHeight() * FyMult)
	
  	self:moveObject(FriendsFrameTitleText, nil, nil, "+", 6)  
  	self:moveObject(FriendsFrameCloseButton, "+", 30, "+", 8)  
  
	self:skinFFToggleTabs("FriendsFrameToggleTab") --N.B. Prefix string
		
--	FriendsDropDown
--	FriendsListFrame

  	self:moveObject(FriendsFrameAddFriendButton, "-", 9, "-", 70)  
  	self:moveObject(FriendsFrameFriendButton1, nil, nil, "+", 15)  
	
	self:removeRegions(FriendsFrameFriendsScrollFrame)
  	self:moveObject(FriendsFrameFriendsScrollFrame, "+", 35, "+", 14)  
	self:skinScrollBar(FriendsFrameFriendsScrollFrame)
	
-->>--	Ignore Frame	
	self:keepRegions(IgnoreListFrame, {6}) -- N.B. 6 is the text region
	
	IgnoreListFrame:SetWidth(IgnoreListFrame:GetWidth() * FxMult)
	IgnoreListFrame:SetHeight(IgnoreListFrame:GetHeight() * FyMult)
	
    self:skinFFToggleTabs("IgnoreFrameToggleTab") --N.B. Prefix string
		
  	self:moveObject(FriendsFrameIgnorePlayerButton, "-", 7, "-", 70)  
 	self:moveObject(FriendsFrameIgnoreButton1, nil, nil, "+", 15)  
	
	self:removeRegions(FriendsFrameIgnoreScrollFrame)
  	self:moveObject(FriendsFrameIgnoreScrollFrame, "+", 35, "+", 14)  
	self:skinScrollBar(FriendsFrameIgnoreScrollFrame)
	
-->>--	WhoFrame
	WhoFrame:SetWidth(WhoFrame:GetWidth() * FxMult)
	WhoFrame:SetHeight(WhoFrame:GetHeight() * FyMult)
	
	self:skinFFColHeads("WhoFrameColumnHeader") --N.B. Prefix string
	self:moveObject(WhoFrameColumnHeader1, nil, nil, "+", 25)

	self:keepRegions(WhoFrameDropDown, {4,5}) -- N.B. regions 4 & 5 are text
  	self:moveObject(WhoFrameDropDown, "+", 5, "+", 1)  
 	self:moveObject(WhoFrameButton1, nil, nil, "+", 15)  
	
	self:removeRegions(WhoListScrollFrame)
  	self:moveObject(WhoListScrollFrame, "+", 35, "+", 20)  
	self:skinScrollBar(WhoListScrollFrame)
	
  	self:moveObject(WhoFrameEditBox, "+", 20, "-", 65)  
	WhoFrameEditBox:SetWidth(WhoFrameEditBox:GetWidth() + 30)
	self:skinEditBox(WhoFrameEditBox)
	
	self:moveObject(WhoFrameTotals, nil, nil, "-", 66)
  	self:moveObject(WhoFrameGroupInviteButton, "+", 30, "-", 71)  
	
-->>--	GuildFrame
	GuildFrame:SetWidth(GuildFrame:GetWidth() * FxMult)
	GuildFrame:SetHeight(GuildFrame:GetHeight() * FyMult)
		
	-- show offline members text and checkbox
	self:removeRegions(GuildFrameLFGFrame)
  	self:moveObject(GuildFrameLFGButton, "+", 42, "+", 12)  
	
	self:skinFFColHeads("GuildFrameColumnHeader") --N.B. Prefix string
	self:moveObject(GuildFrameColumnHeader1, nil, nil, "+", 25)
	self:skinFFColHeads("GuildFrameGuildStatusColumnHeader") --N.B. Prefix string
	self:moveObject(GuildFrameGuildStatusColumnHeader1, nil, nil, "+", 25)

	self:removeRegions(GuildListScrollFrame)
  	self:moveObject(GuildListScrollFrame, "+", 35, "+", 20)  
	self:skinScrollBar(GuildListScrollFrame)
	
  	self:moveObject(GuildFrameTotals, "+", 10, "-", 86)  
  	self:moveObject(GuildFrameNotesLabel, "-", 8, nil, nil)  
  	self:moveObject(GuildFrameControlButton, "+", 30, "-", 71)  
	
	-- where should this be moved to ?, if anywhere	
--	self:moveObject(GuildMOTDEditButton, nil, nil, nil, nil) 

	-- Guild Control Popup Frame
	self:keepRegions(GuildControlPopupFrame, {5, 6}) --N.B. regions 5 & 6 are text
  	self:moveObject(GuildControlPopupFrame, "+", 30, nil, nil)  
	
--	GuildControlPopupFrameCheckboxes
	self:keepRegions(GuildControlPopupFrameDropDown, {4,5}) -- N.B. regions 4 & 5 are text
	GuildControlPopupFrameEditBox:SetWidth(GuildControlPopupFrameEditBox:GetWidth() + 30)
	self:skinEditBox(GuildControlPopupFrameEditBox)
  	self:applySkin(GuildControlPopupFrame)

--	GuildInfoFrame	
	self:keepRegions(GuildInfoFrame, {2}) -- N.B. region 2 is text
  	self:moveObject(GuildInfoTitle, nil, nil, "+", 3)  
	
	self:removeRegions(GuildInfoFrameScrollFrame)
	self:skinScrollBar(GuildInfoFrameScrollFrame)
		
	self:applySkin(GuildInfoTextBackground)
  	self:applySkin(GuildInfoFrame)

--	GuildMemberDetailFrame
	self:keepRegions(GuildMemberDetailFrame, {10, 11, 12, 13, 14, 15, 16, 17, 18}) -- N.B. regions 10-18 are text	
  	self:moveObject(GuildMemberDetailFrame, "+", 28, nil, nil)  
	self:moveObject(GuildFramePromoteButton, nil, nil, "-", 30)
  	self:applySkin(GuildMemberDetailFrame)

-->>--	Raid Frame
  	self:moveObject(RaidFrameConvertToRaidButton, "-", 30, "+", 10)  
	
	if IsAddOnLoaded("Blizzard_RaidUI") then self:RaidUI() end

--	RaidInfoFrame
	self:keepRegions(RaidInfoFrame, {12, 13, 14, 15}) -- N.B. regions 12-15 are text
  	self:moveObject(RaidInfoFrame, "+", 35, nil, nil)  
	self:removeRegions(RaidInfoScrollFrame)
  	self:moveObject(RaidInfoScrollFrame, "+", 5, nil, nil)  
	self:skinScrollBar(RaidInfoScrollFrame)
 	self:applySkin(RaidInfoFrame)
 	
-->>-- Frame Tabs
    for i = 1, 4 do
		self:keepRegions(_G["FriendsFrameTab"..i], {7, 8}) -- N.B. region 7 is the Text, 8 is the highlight
		if i == 1 then
			self:moveObject(_G["FriendsFrameTab"..i], nil, nil, "-", 72)  
		else
			self:moveObject(_G["FriendsFrameTab"..i], "+", 9, nil, nil)  
		end
        if self.db.profile.TexturedTab then self:applySkin(_G["FriendsFrameTab"..i], nil ,0)
		else self:applySkin(_G["FriendsFrameTab"..i]) end
	end

  	self:applySkin(FriendsFrame)

end

function Skinner:skinFFToggleTabs(tabName)
--	self:Debug("skinFFToggleTabs: [%s]", tabName)

	for i = 1, 2 do
		local togTab = _G[tabName..i]
   		self:keepRegions(togTab, {7, 8}) -- N.B. regions 7 & 8 are text/scripts
		togTab:SetHeight(togTab:GetHeight() - 5)
		if i == 1 then self:moveObject(togTab, nil, nil, "+", 3) end 
		self:moveObject(_G[togTab:GetName().."Text"], "-", 2, "+", 3)
		self:moveObject(_G[togTab:GetName().."HighlightTexture"], "-", 2, "+", 5)
  		self:applySkin(togTab)
  	end
	
end

function Skinner:skinFFColHeads(buttonName)
--	self:Debug("skinFFColHeads: [%s]", buttonName)

	for i = 1, 4 do
		self:keepRegions(_G[buttonName..i], {4,5}) -- N.B. regions 4 & 5 are text
		self:applySkin(_G[buttonName..i])
	end
	
end

function Skinner:TradeSkill()
	if not self.db.profile.TradeSkill or self.initialized.TradeSkill then return end
	self.initialized.TradeSkill = true

	self:keepRegions(TradeSkillFrame, {6}) -- N.B. region 6 is text
	TradeSkillFrame:SetWidth(TradeSkillFrame:GetWidth() * FxMult)
	TradeSkillFrame:SetHeight(TradeSkillFrame:GetHeight() * FyMult)
	self:moveObject(TradeSkillFrameTitleText, nil, nil, "+", 8)
	self:moveObject(TradeSkillFrameCloseButton, "+", 28, "+", 8)
	self:moveObject(TradeSkillRankFrame, "-", 40, "+", 8)
	self:removeRegions(TradeSkillRankFrameBorder, {1}) -- N.B. region 2 is bar texture
	self:glazeStatusBar(TradeSkillRankFrame, 0)
	self:removeRegions(TradeSkillExpandButtonFrame)
	self:moveObject(TradeSkillExpandButtonFrame, nil, nil, "+", 12)
	self:keepRegions(TradeSkillSubClassDropDown, {4}) -- N.B. 4 is the text region
	self:keepRegions(TradeSkillInvSlotDropDown, {4}) -- N.B. 4 is the text region
	self:moveObject(TradeSkillInvSlotDropDown, "+", 20, "+", 12)
	self:moveObject(TradeSkillSkill1, nil, nil, "+", 12)
	self:removeRegions(TradeSkillListScrollFrame)
	self:moveObject(TradeSkillListScrollFrame, "+", 35, "+", 12)
	self:skinScrollBar(TradeSkillListScrollFrame)
	self:removeRegions(TradeSkillDetailScrollFrame)
	self:moveObject(TradeSkillDetailScrollFrame, "-", 4, "+", 12)
	self:skinScrollBar(TradeSkillDetailScrollFrame)
	self:moveObject(TradeSkillInputBox, "-", 5, nil, nil)
	self:skinEditBox(TradeSkillInputBox)
	self:moveObject(TradeSkillCreateButton, "-", 10, "+", 10)
	self:moveObject(TradeSkillCancelButton, "-", 7, "+", 10)
	for i = 1, 8 do
		self:moveObject(_G["TradeSkillReagent"..i.."Count"], "+", 4, nil, nil)
	end
	self:applySkin(TradeSkillFrame)

end

function Skinner:CraftFrame()
	if not self.db.profile.CraftFrame or self.initialized.CraftFrame then return end
	self.initialized.CraftFrame = true

	self:keepRegions(CraftFrame, {6, 11, 12, 13}) -- N.B. 6, 11-13 are text regions
	CraftFrame:SetWidth(CraftFrame:GetWidth() * FxMult)
	CraftFrame:SetHeight(CraftFrame:GetHeight() * FyMult)
	self:moveObject(CraftFrameTitleText, nil, nil, "+", 6)
	self:moveObject(CraftFrameCloseButton, "+", 28, "+", 8)
	self:moveObject(CraftFrameIsMakeableText, nil, nil, "+", 15)                    
	self:keepRegions(CraftFrameFilterDropDown, {4,5}) -- N.B. regions 4 & 5 are text
	self:moveObject(CraftFrameFilterDropDown, nil, nil, "+", 15)
	self:removeRegions(CraftRankFrameBorder)
	self:moveObject(CraftRankFrame, "-", 40, nil, nil)
	self:glazeStatusBar(CraftRankFrame, 0)
	self:removeRegions(CraftExpandButtonFrame)
	self:moveObject(Craft1, nil, nil, "+", 20)
	self:removeRegions(CraftListScrollFrame)
	self:moveObject(CraftListScrollFrame, "+", 35, "+", 20)
	self:skinScrollBar(CraftListScrollFrame)
	self:removeRegions(CraftDetailScrollFrame)
	self:moveObject(CraftDetailScrollFrame, "-", 4, "+", 10)
	self:skinScrollBar(CraftDetailScrollFrame)
	self:moveObject(CraftCreateButton, "-", 10, "+", 10)
	self:moveObject(CraftCancelButton, "-", 10, "+", 10)
	for i = 1, 8 do
		self:moveObject(_G["CraftReagent"..i.."Count"], "+", 4, nil, nil)
	end
	self:applySkin(CraftFrame)

end

function Skinner:TradeFrame()
	if not self.db.profile.TradeFrame or self.initialized.TradeFrame then return end
	self.initialized.TradeFrame = true

	self:keepRegions(TradeFrame, {7, 8, 9, 10}) -- N.B. regions 7-10 are text

	TradeFrame:SetWidth(TradeFrame:GetWidth() - 20)
	TradeFrame:SetHeight(TradeFrame:GetHeight() - 80)

	self:moveObject(TradeFrameRecipientNameText, nil, nil, "+", 6)
	self:moveObject(TradeFramePlayerNameText, nil, nil, "+", 6)

	-- move everything up
	local yOfs = 40
	self:moveObject(TradeFramePlayerEnchantText, nil, nil, "+", yOfs)
	self:moveObject(TradeHighlightPlayer, nil, nil, "+", yOfs)
	self:moveObject(TradeHighlightRecipient, nil, nil, "+", yOfs)
	self:moveObject(TradeRecipientItem1, nil, nil, "+", yOfs)
	self:moveObject(TradePlayerItem1, nil, nil, "+", yOfs)
	self:moveObject(TradeRecipientMoneyFrame, "+", 20, "+", yOfs)
	self:moveObject(TradePlayerInputMoneyFrame, nil, nil, "+", yOfs)
	self:skinMoneyFrame(TradePlayerInputMoneyFrame)

	self:moveObject(TradeFrameTradeButton, "+", 20, "-", 44)
	self:moveObject(TradeFrameCloseButton, "+", 26, "+", 8)

	self:applySkin(TradeFrame)

end

function Skinner:QuestLog()
	if not self.db.profile.QuestLog.skin or self.initialized.QuestLog then return end
	self.initialized.QuestLog = true

	self:SecureHook("QuestLog_UpdateQuestDetails", function(doNotScroll)
--		self:Debug("QuestLog_UpdateQuestDetails")
		for i = 1, 10 do
    		local r, g, b, a = _G["QuestLogObjective"..i]:GetTextColor()
    		_G["QuestLogObjective"..i]:SetTextColor(BTr - r, BTg - g, BTb)
    	end
    	local r, g, b, a = QuestLogRequiredMoneyText:GetTextColor()
    	QuestLogRequiredMoneyText:SetTextColor(BTr - r, BTg - g, BTb)
    	QuestLogRewardTitleText:SetTextColor(HTr, HTg, HTb)
    	QuestLogItemChooseText:SetTextColor(BTr, BTg, BTb)
    	QuestLogItemReceiveText:SetTextColor(BTr, BTg, BTb)
		end)
	
	self:keepRegions(QuestLogFrame, {1, 7, 11}) -- N.B. region 1 is dummy, regions 7 & 11 are text
	QuestLogFrame:SetWidth(QuestLogFrame:GetWidth() - 40)
	QuestLogFrame:SetHeight(QuestLogFrame:GetHeight() - 70)
	self:moveObject(QuestLogTitleText, nil, nil, "+", 10)
	self:moveObject(QuestLogFrameCloseButton, "+", 28, "+", 8)

	-- movement values
	local xOfs, yOfs = 8, 24
	self:moveObject(QuestLogExpandButtonFrame, "-", xOfs, "+", yOfs)
	self:keepRegions(QuestLogCollapseAllButton, {4, 6}) -- N.B. region 4 is button, 6 is text
	self:moveObject(QuestLogQuestCount, nil, nil, "+", 20)
	self:moveObject(QuestLogTitle1, "-", xOfs, "+", yOfs)
	self:moveObject(QuestLogListScrollFrame, "-", xOfs, "+", yOfs)
	self:keepRegions(EmptyQuestLogFrame, {5}) -- N.B. region 5 is text

	QuestLogQuestTitle:SetTextColor(HTr, HTg, HTb)
	QuestLogObjectivesText:SetTextColor(BTr, BTg, BTb)
	for i = 1, 10 do
   		local r, g, b, a = _G["QuestLogObjective"..i]:GetTextColor()
   		_G["QuestLogObjective"..i]:SetTextColor(BTr - r, BTg - g, BTb)
   	end
	QuestLogSuggestedGroupNum:SetTextColor(HTr, HTg, HTb)
	QuestLogDescriptionTitle:SetTextColor(HTr, HTg, HTb)
	QuestLogQuestDescription:SetTextColor(BTr, BTg, BTb)
	
	self:skinScrollBar(QuestLogListScrollFrame)
	self:skinScrollBar(QuestLogDetailScrollFrame)
	self:moveObject(QuestLogFrameAbandonButton, "-", 12, "-", 47)
	self:moveObject(QuestFrameExitButton, "+", 32, "-", 47)
	self:applySkin(QuestLogFrame)
	
end

function Skinner:reSizeQW()

	if self.db.profile.QuestLog.size == 1 then
		self.QWfont = GameFontHighlight
	else
		self.QWfont = GameFontHighlightSmall
	end
	for i = 1, 30 do
		_G["QuestWatchLine"..i]:SetFontObject(self.QWfont)
	end
	
end

function Skinner:RaidUI()
	if not self.db.profile.RaidUI or self.initialized.RaidUI then return end
	self.initialized.RaidUI = true

	self:moveObject(RaidFrameAddMemberButton, "-", 40, "+", 10)
	self:moveObject(RaidGroup1, "-", 7, "+", 8)
	for i = 1, 10 do
		local tabName = _G["RaidClassButton"..i]
        self:keepRegions(tabName, {2,3}) -- N.B. region 2 is the icon, 3 is the text
		if i == 10 then self:moveObject(tabName, "+", 31, "-", 70) end
    end
	
	self:keepRegions(ReadyCheckFrame, {3}) -- N.B. region 3 is text
	ReadyCheckFrame:SetWidth(ReadyCheckFrame:GetWidth() * FxMult)
	ReadyCheckFrame:SetHeight(ReadyCheckFrame:GetHeight() * FyMult)
	self:moveObject(ReadyCheckFrameText, "-", 12, "+", 10)
	self:moveObject(ReadyCheckFrameYesButton, "-", 15, "-", 5)
	self:applySkin(ReadyCheckFrame)

	local pulloutsSkinned = {}
	
	self:SecureHook("RaidPullout_GetFrame", function(filterID)
		for i = 1, NUM_RAID_PULLOUT_FRAMES 	do
			local pulloutButton = _G["RaidPullout"..i]
			if not pulloutsSkinned[pulloutButton] then
				pulloutsSkinned[pulloutButton] = true
				self:applySkin(pulloutButton)
			end
		end
		end)
		
end
