local _G = getfenv(0)

-- CharacterFrame / ReputationFrame / SkillFrame
function Skinner:CharacterFrames()
	if not self.db.profile.CharacterFrames or self.initialized.CharacterFrames then return end
	self.initialized.CharacterFrames = true

	-- hook this to adjust the widths of the Tabs
	self:SecureHook("CharacterFrame_ShowSubFrame")
	-- hook this to move tabs when Pet is called
	self:SecureHook("PetTab_Update", "CF_PetTab_Update")
		
	-- handle each frame
	self:CharacterFrame()
	self:PaperDollFrame()
	self:PetPaperDollFrame()
	self:ReputationFrame()
	self:SkillFrame()
	self:PVPFrame()

end

function Skinner:CharacterFrame()

	self:removeRegions(CharacterFrame)	

	CharacterFrame:SetWidth(CharacterFrame:GetWidth() * FxMult)
	CharacterFrame:SetHeight(CharacterFrame:GetHeight() * FyMult)

	self:moveObject(CharacterNameText, nil, nil, "-", 30)
	self:moveObject(CharacterFrameCloseButton, "+", 28, "+", 8)
	
	self:keepRegions(PlayerTitleDropDown, {4,5}) -- N.B. regions 4 & 5 are text
	self:keepRegions(PlayerStatFrameLeftDropDown, {4,5}) -- N.B. regions 4 & 5 are text
	self:keepRegions(PlayerStatFrameRightDropDown, {4,5}) -- N.B. regions 4 & 5 are text
	
--	CharacterFrameTab1-5
	for i = 1, #CHARACTERFRAME_SUBFRAMES do

    	local tabName = _G["CharacterFrameTab"..i]
		self:keepRegions(tabName, {7, 8}) -- N.B. region 7 is the Text, 8 is the highlight
		if i == 1 then
			self:moveObject(tabName, "-", 6, "-", 71)
		else
			-- handle no pet out or not a pet class
			self:moveObject(tabName, "+", ((i == 3 and not HasPetUI()) and 0 or 11), nil, nil)
		end
        if self.db.profile.TexturedTab then self:applySkin(tabName, nil, 0, 1)
		else self:applySkin(tabName) end
	
	end

	self:applySkin(CharacterFrame)	

end

function Skinner:PaperDollFrame()

	self:keepRegions(PaperDollFrame, {5, 6, 7}) -- N.B. regions 5-7 are text
	
	CharacterModelFrameRotateLeftButton:Hide()
	CharacterModelFrameRotateRightButton:Hide()
	self:makeMFRotatable(CharacterModelFrame)
	
	local xOfs, yOfs = 9, 10
	self:moveObject(CharacterModelFrame, "-", xOfs, "+", yOfs)
	self:moveObject(CharacterHeadSlot, "-", xOfs, "+", yOfs)
	self:moveObject(CharacterHandsSlot, "-", xOfs, "+", yOfs)
	self:moveObject(CharacterResistanceFrame, "-", xOfs, "+", yOfs)
	self:moveObject(CharacterAttributesFrame, "-", xOfs, "+", yOfs)

	self:moveObject(CharacterMainHandSlot, "-", xOfs, "-", 70)

	self:removeRegions(CharacterAmmoSlot, {1})
	
end

function Skinner:PetPaperDollFrame()

	self:removeRegions(PetPaperDollFrame, {1, 2, 3, 4}) -- N.B. regions 5-9 are text
	
	PetModelFrameRotateLeftButton:Hide()
	PetModelFrameRotateRightButton:Hide()
	self:makeMFRotatable(PetModelFrame)
	
	self:moveObject(PetNameText, nil, nil, "-", 30)

	local xOfs, yOfs = 10, 25
	self:moveObject(PetModelFrame, "-", xOfs, "+", yOfs)
	self:moveObject(PetAttributesFrame, "-", xOfs, "+", yOfs)
	self:moveObject(PetResistanceFrame, "-", xOfs, "+", yOfs)
	
	-- move outside the Model Frame otherwise the tooltip doesn't work
	self:moveObject(PetPaperDollPetInfo, nil, nil, "+", 90)
	
	self:keepRegions(PetPaperDollFrameExpBar, {3, 4}) -- N.B. region 3 is text
	self:moveObject(PetPaperDollFrameExpBar, "-", 10, "-", 72)
	self:glazeStatusBar(PetPaperDollFrameExpBar, 0)

	self:moveObject(PetTrainingPointText, nil, nil, "-", 72)
	self:moveObject(PetPaperDollCloseButton, "-", 5, "+", 9)
	
end

function Skinner:ReputationFrame()

	self:keepRegions(ReputationFrame, {5, 6}) -- N.B. regions 5 & 6 are text
	
	local xOfs, yOfs = 5, 20
	self:moveObject(ReputationFrameFactionLabel, "-", xOfs, "+", yOfs)
	self:moveObject(ReputationFrameStandingLabel, "-", xOfs, "+", yOfs)
	self:moveObject(ReputationBar1, "-", xOfs, "+", yOfs)

	for i = 1 , NUM_FACTIONS_DISPLAYED do
		self:removeRegions(_G["ReputationBar"..i], {1, 2}) -- N.B. regions 3 & 4 are text
		self:glazeStatusBar(_G["ReputationBar"..i], 0)
	end

	self:removeRegions(ReputationListScrollFrame)
	self:moveObject(ReputationListScrollFrame, "+", 35, "+", 20)
	self:skinScrollBar(ReputationListScrollFrame)

	self:keepRegions(ReputationDetailFrame, {10, 11}) -- N.B. regions 10 & 11 are text
	self:moveObject(ReputationDetailFrame, "+", 30, nil, nil)
	self:applySkin(ReputationDetailFrame)
	
end

function Skinner:SkillFrame()

	self:removeRegions(SkillFrame)
	
	local xOfs, yOfs = 5, 20
	self:removeRegions(SkillFrameExpandButtonFrame)
	self:moveObject(SkillFrameExpandButtonFrame, "-", 57, "+", yOfs)
	self:moveObject(SkillTypeLabel1, "-", xOfs, "+", yOfs)
	self:moveObject(SkillRankFrame1, "-", xOfs, "+", yOfs)
	
	self:removeRegions(SkillListScrollFrame)
	self:moveObject(SkillListScrollFrame, "+", 35, "+", 20)
	self:skinScrollBar(SkillListScrollFrame)

	for i = 1, SKILLS_TO_DISPLAY do
		self:removeRegions(_G["SkillRankFrame"..i.."Border"], {1}) -- N.B. region 2 is highlight
		self:glazeStatusBar(_G["SkillRankFrame"..i], 0)
	end
	
	self:removeRegions(SkillDetailScrollFrame)
	self:skinScrollBar(SkillDetailScrollFrame)
	
	self:removeRegions(SkillDetailStatusBar, {1, 4, 5}) -- N.B. regions 2 & 3 are text
	self:glazeStatusBar(SkillDetailStatusBar, 0)
	
	self:moveObject(SkillFrameCancelButton, "-", 5, "+", 9)
	
	self:SecureHook("SkillDetailFrame_SetStatusBar", function(skillIndex, adjustedSkillPoints)
		self:moveObject(SkillDetailStatusBarSkillName, nil, nil, "-", 2)
		end)
	
end

function Skinner:PVPFrame()

	self:removeRegions(PVPFrame, {1, 2, 3, 4, 5}) -- N.B. other regions are required
	PVPFrameCloseButton:Hide()
	self:moveObject(PVPFrameHonorLabel, "-", 25, nil, nil)
	self:moveObject(PVPFrameHonorPoints, "+", 30, nil, nil)
	self:moveObject(PVPFrameArenaLabel, "-", 25, nil, nil)
	self:moveObject(PVPFrameArenaPoints, "+", 30, nil, nil)
	self:moveObject(PVPHonorTodayLabel, "+", 10, "+", 0)
	self:moveObject(PVPHonorTodayHonor, "+", 22, "+", 0)
	self:moveObject(PVPTeam1Standard, "-", 10, "+", 10)
	self:moveObject(PVPTeam2Standard, "-", 10, "+", 10)
	self:moveObject(PVPTeam3Standard, "-", 10, "+", 10)

end

function Skinner:CharacterFrame_ShowSubFrame(frameName)
--  self:Debug("CF_SSF: [%s]", frameName)
	-- Character subframe names
	local cfSubframes = { "PaperDollFrame", "PetPaperDollFrame", "ReputationFrame", "SkillFrame", "PVPFrame" }
	
    if self.db.profile.TexturedTab then
		-- change the texture for the Active and Inactive tabs
		for i, v in pairs(cfSubframes) do
    		if v == frameName then
    			self:setActiveTab(_G["CharacterFrameTab"..i])
    		else
    			self:setInactiveTab(_G["CharacterFrameTab"..i])
    		end
    	end
    end
	-- get frame and tab widths
    local frameWidth, tabWidths = self:CF_GetWidths()
	-- resize tabs if required
	if tabWidths > frameWidth and self.initialized.CF_SSF
	then self.initialized.CF_SSF = nil end

	if self.initialized.CF_SSF then return end

	self.initialized.CF_SSF = true
    self:CF_ResizeTabs(frameWidth / tabWidths)

end

function Skinner:CF_PetTab_Update()
--  self:Debug("CF_PetTab_Update")
    		
    -- check to see if the Pet Tab has been shown
	local point,relativeTo,relativePoint,xOfs,yOfs = CharacterFrameTab3:GetPoint()
	if math.floor(xOfs) == -17 then
		self:moveObject(CharacterFrameTab3, "+", 11, nil, nil)
    	-- get frame and tab widths
        local frameWidth, tabWidths = self:CF_GetWidths() 
    	-- resize tabs if required
        if tabWidths > frameWidth then self:CF_ResizeTabs(frameWidth / tabWidths) end
    else
        self:CF_ResizeTabs(1)    
	end


end

function Skinner:CF_GetWidths()

    local fWidth = CharacterFrame:GetWidth()
    local tWidths = 0

    for i = 1, #CHARACTERFRAME_SUBFRAMES do
        tWidths = tWidths + _G["CharacterFrameTab"..i]:GetWidth()
    end
    
    -- if Pet not out then don't include its tab width
    if not HasPetUI() then
        tWidths = tWidths - CharacterFrameTab2:GetWidth()
    end

--  self:Debug("CF_GW: [%s, %s]", math.floor(fWidth), math.floor(tWidths))

    return math.floor(fWidth), math.floor(tWidths)
    
end

function Skinner:CF_ResizeTabs(CFTxMult)
--  self:Debug("CF_ResizeTabs: [%s]", CFTxMult)
    
	for i = 1, #CHARACTERFRAME_SUBFRAMES do
    	local tabName = _G["CharacterFrameTab"..i]
    	if CFTxMult == 1 then
            PanelTemplates_TabResize(0, tabName)
    	else
            tabName:SetWidth(tabName:GetWidth() * CFTxMult)
            local hlTexture = _G[tabName:GetName().."HighlightTexture"]
        	if ( hlTexture ) then
                hlTexture:SetWidth(tabName:GetWidth())
            end
        end
	end

end

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

	self:removeRegions(PetStableFrame, {1, 2, 3, 4, 5})  -- N.B. regions 6-10 are text

	PetStableFrame:SetWidth(PetStableFrame:GetWidth() * FxMult)
	PetStableFrame:SetHeight(PetStableFrame:GetHeight() * FyMult)

	self:moveObject(PetStableFrameCloseButton, "+", 28, "+", 8)
	
	PetStableModelRotateLeftButton:Hide()
	PetStableModelRotateRightButton:Hide()
	self:makeMFRotatable(PetStableModel)

	self:moveObject(PetStableTitleLabel, nil, nil, "+", 6)
	local xOfs, yOfs = 0, 75
	self:moveObject(PetStableCurrentPet, "-", xOfs, "-", yOfs)
	self:moveObject(PetStableSlotText, "-", xOfs, "-", yOfs)
	self:moveObject(PetStableCostLabel, "-", xOfs, "-", yOfs)
	self:moveObject(PetStablePurchaseButton, "-", xOfs, "-", yOfs)
	self:moveObject(PetStableMoneyFrame, "-", xOfs, "-", yOfs - 10)

	-- move outside the Model Frame otherwise the tooltip doesn't work
	self:moveObject(PetStablePetInfo, nil, nil, "+", 90)

	self:applySkin(PetStableFrame)

end

local spellbooktypes = { BOOKTYPE_SPELL, BOOKTYPE_PET, nil}

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

    if self.db.profile.TexturedTab then
    	-- hook to handle tabs
    	self:SecureHook("ToggleSpellBook", function(bookType)
--          self:Debug("ToggleSpellBook: [%s, %s]", bookType, SpellBookFrame.bookType)
    		local sbfbt = SpellBookFrame.bookType
    		if sbfbt == bookType then return end
        	for i, v in pairs(spellbooktypes) do
--        		self:Debug("sbt : [%s]", v)
        		if v == bookType then 
        			self:setActiveTab(_G["SpellBookFrameTabButton"..i]) 
        		else 
        			self:setInactiveTab(_G["SpellBookFrameTabButton"..i]) 
        		end
    		end
    		end)
    end
		
	self:keepRegions(SpellBookFrame, {6, 7}) -- N.B. regions 6 & 7 are text
	
	SpellBookFrame:SetWidth(SpellBookFrame:GetWidth() * 0.9)
	SpellBookFrame:SetHeight(SpellBookFrame:GetHeight() * 0.84)

	self:moveObject(SpellBookCloseButton, "+", 28, "+", 8)
	self:moveObject(SpellBookTitleText, nil, nil, "-", 25)

	self:moveObject(SpellBookPageText, nil, nil, "-", 70)
	self:moveObject(SpellBookPrevPageButton, "-", 20, "-", 70)
	self:moveObject(SpellBookNextPageButton, nil, nil, "-", 70)
	
	for i = 1, SPELLS_PER_PAGE do
        self:removeRegions(_G["SpellButton"..i], {1})
        if i == 1 then self:moveObject(_G["SpellButton"..i], "-" , 10, "+", 20) end
        _G["SpellButton"..i.."SpellName"]:SetTextColor(HTr, HTg, HTb)        
        _G["SpellButton"..i.."SubSpellName"]:SetTextColor(BTr, BTg, BTb)        
    end
    
	for i = 1, MAX_SKILLLINE_TABS do
		local tabName = _G["SpellBookSkillLineTab"..i]
        self:removeRegions(tabName, {1}) -- N.B. other regions are icon and highlight
--      self:Debug("SBSLT: [%s, %s]", tabName:GetWidth(), tabName:GetHeight())
		tabName:SetWidth(tabName:GetWidth() * 1.25)
		tabName:SetHeight(tabName:GetHeight() * 1.25)
		if i == 1 then self:moveObject(tabName, "+", 28, nil, nil) end
        self:applySkin(tabName)
    end
    
    for i = 1, 3 do
    	local tabName = _G["SpellBookFrameTabButton"..i]
		self:keepRegions(tabName, {1, 3}) -- N.B. region 1 is the Text, 3 is the highlight
		tabName:SetWidth(tabName:GetWidth() * FTyMult)
		tabName:SetHeight(tabName:GetHeight() * FTxMult)
		local left, right, top, bottom = tabName:GetHitRectInsets()
--		self:Debug("SBFTB: [%s, %s, %s, %s, %s]", i, left, right, top, bottom)
		tabName:SetHitRectInsets(left * FTyMult, right * FTyMult, top * FTxMult, bottom * FTxMult)
        if self.db.profile.TexturedTab then self:applySkin(tabName, nil, 0)
		else self:applySkin(tabName) end
		if i == 1 then
			self:moveObject(tabName, "-", 18, "-", 70)
			if self.db.profile.TexturedTab then self:setActiveTab(tabName) end
		else
			self:moveObject(tabName, "+", 15, nil, nil)
			if self.db.profile.TexturedTab then self:setInactiveTab(tabName) end
		end

	end   

	self:applySkin( SpellBookFrame)
	
end

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

	self:SecureHook("TalentFrame_Update", function()
--		self:Debug("TalentFrame_Update")
		for i = 1, MAX_TALENT_TABS do
			local tabName = _G["TalentFrameTab"..i]
			tabName:SetWidth(tabName:GetWidth() * FTyMult)
            if self.db.profile.TexturedTab then
    			if i == TalentFrame.selectedTab then
    				self:setActiveTab(tabName)
    			else
    				self:setInactiveTab(tabName)
    			end
            end
		end
		end)
		
	self:removeRegions(TalentFrame, {1, 2, 3, 4, 5, 11, 12, 13}) -- N.B. 6, 7, 8 & 9 are the background picture, 10, 14, 15 & 16 are text regions
	
	TalentFrame:SetWidth(TalentFrame:GetWidth() * FxMult)
	TalentFrame:SetHeight(TalentFrame:GetHeight() * FyMult)

	self:moveObject(TalentFrameTitleText, nil, nil, "+", 6)
	self:moveObject(TalentFrameCloseButton, "+", 28, "+", 8)
	self:moveObject(TalentFrameSpentPoints, "-", 35, "+", 15)
	self:moveObject(TalentFrameTalentPointsText, "-", 10, "-", 70)
	self:moveObject(TalentFrameBackgroundTopLeft, "-", 10, "+", 15)
	
	self:removeRegions(TalentFrameScrollFrame)
	self:moveObject(TalentFrameScrollFrame, "+", 35, "+", 15)
	self:skinScrollBar(TalentFrameScrollFrame)

	self:moveObject(TalentFrameCancelButton, "-", 10, "+", 8)
	
    for i=1,MAX_TALENT_TABS do
		local tabName = _G["TalentFrameTab"..i]
		self:keepRegions(tabName, {7, 8}) -- N.B. region 7 is text, 8 is highlight
		tabName:SetWidth(tabName:GetWidth() * FTyMult)
        if self.db.profile.TexturedTab then self:applySkin(tabName, nil, 0)
        else self:applySkin(_G["TalentFrameTab"..i]) end
		if i == 1 then
			self:moveObject(tabName, "-", 8, "-", 71)
			if self.db.profile.TexturedTab then self:setActiveTab(tabName) end 
		else
			self:moveObject(tabName, "+", 10, nil, nil)
			if self.db.profile.TexturedTab then self:setInactiveTab(tabName) end 
		end
		 
	end   

	self:applySkin(TalentFrame)

end

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

	self:removeRegions(DressUpFrame, {1, 2, 3, 4, 5}) -- N.B. regions 6 & 7 are text, 8- 11 are the background picture
	DressUpFrame:SetWidth(DressUpFrame:GetWidth() * FxMult)
	DressUpFrame:SetHeight(DressUpFrame:GetHeight() * FyMult)

	DressUpModelRotateLeftButton:Hide()
	DressUpModelRotateRightButton:Hide()
	self:makeMFRotatable(DressUpModel)
	
	self:moveObject(DressUpFrameTitleText, nil, nil, "+", 6)
	self:moveObject(DressUpFrameDescriptionText, nil, nil, "+", 6)
	self:moveObject(DressUpFrameCloseButton, "+", 28, "+", 8)
	self:moveObject(DressUpBackgroundTopLeft, "-", 8, "+", 15)
	self:moveObject(DressUpModel, nil, nil, "-", 60)
	self:moveObject(DressUpFrameCancelButton, "-", 6, "+", 10)
	
	self:applySkin(DressUpFrame)

end
