local AS = unpack(AddOnSkins)

function AS:Blizzard_PVPUI(event, addon)
	if addon ~= "Blizzard_PVPUI" then return end

	AS:StripTextures(PVPUIFrame)
	AS:StripTextures(PVPQueueFrame.HonorInset)

	for i = 1, 3 do
		local Button = PVPQueueFrame['CategoryButton'..i]
		AS:SkinBackdropFrame(Button, nil, true)
		Button.Background:SetTexture('')
		Button.Ring:SetTexture('')
		Button.Icon:SetSize(45, 45)
		AS:SkinTexture(Button.Icon, true)

		Button.Name:SetTextColor(1, 1, 1)

		Button:SetHighlightTexture('')
		Button:HookScript('OnEnter', function(self)
			self.Backdrop:SetBackdropBorderColor(1, .82, 0)
			self.Icon.Backdrop:SetBackdropBorderColor(1, .82, 0)
		end)
		Button:HookScript('OnLeave', function(self)
			if self:GetID() == self.isSelected then
				self.Backdrop:SetBackdropBorderColor(unpack(AS.Color))
				self.Icon.Backdrop:SetBackdropBorderColor(unpack(AS.Color))
			else
				self.Backdrop:SetBackdropBorderColor(unpack(AS.BorderColor))
				self.Icon.Backdrop:SetBackdropBorderColor(unpack(AS.BorderColor))
			end
		end)
	end

	hooksecurefunc('PVPQueueFrame_SelectButton', function(index)
		for i = 1, 3 do
			local Button = PVPQueueFrame["CategoryButton"..i]
			Button.isSelected = index
			if ( i == index ) then
				Button.Backdrop:SetBackdropBorderColor(unpack(AS.Color))
				Button.Icon.Backdrop:SetBackdropBorderColor(unpack(AS.Color))
			else
				Button.Backdrop:SetBackdropBorderColor(unpack(AS.BorderColor))
				Button.Icon.Backdrop:SetBackdropBorderColor(unpack(AS.BorderColor))
			end
		end
	end)

	AS:SkinDropDownBox(HonorFrameTypeDropDown)

	AS:SkinScrollBar(HonorFrameSpecificFrameScrollBar)
	AS:SkinButton(HonorFrameQueueButton, true)
	AS:StripTextures(HonorFrame.BonusFrame)
	AS:StripTextures(HonorFrame.BonusFrame.ShadowOverlay)

	local Buttons = { ['RandomBGButton'] = HonorFrame.BonusFrame, ['RandomEpicBGButton'] = HonorFrame.BonusFrame, ['Arena1Button'] = HonorFrame.BonusFrame, ['BrawlButton'] = HonorFrame.BonusFrame, ['RatedBG'] = ConquestFrame, ['Arena2v2'] = ConquestFrame, ['Arena3v3'] = ConquestFrame }

	for Section, Parent in pairs(Buttons) do
		local Button = Parent[Section]
		AS:SkinButton(Button)
		Button.SelectedTexture:SetTexture('')
		Button.SelectedTexture:Hide()

		AS:StripTextures(Button.Reward)
		AS:SkinTexture(Button.Reward.Icon, true)

		AS:StripTextures(Button.Reward.EnlistmentBonus)
		AS:SkinTexture(Button.Reward.EnlistmentBonus.Icon, true)

		Button:HookScript('OnEnter', function(self)
			self:SetBackdropBorderColor(1, .82, 0)
		end)
		Button:HookScript('OnLeave', function(self)
			if self.SelectedTexture:IsShown() then
				self:SetBackdropBorderColor(unpack(AS.Color))
			else
				self:SetBackdropBorderColor(unpack(AS.BorderColor))
			end
		end)
	end

	hooksecurefunc('PVPUIFrame_ConfigureRewardFrame', function(rewardFrame, honor, experience, itemRewards, currencyRewards)
		local rewardTexture, rewardQuaility, _ = nil, 1

		if currencyRewards then
			for _, reward in ipairs(currencyRewards) do
				local name, _, texture, _, _, _, _, quality = GetCurrencyInfo(reward.id)
				if quality == LE_ITEM_QUALITY_ARTIFACT then
					_, rewardTexture, _, rewardQuaility = CurrencyContainerUtil.GetCurrencyContainerInfo(reward.id, reward.quantity, name, texture, quality)
				end
			end
		end

		if not rewardTexture and itemRewards then
			local reward = itemRewards[1]
			if reward then
				_, _, rewardQuaility, _, _, _, _, _, _, rewardTexture = GetItemInfo(reward.id)
			end
		end

		if rewardTexture then
			rewardFrame.Icon:SetTexture(rewardTexture)
			if rewardFrame.Icon.Backdrop then
				rewardFrame.Icon.Backdrop:SetBackdropBorderColor(GetItemQualityColor(rewardQuaility))
			end
		end
	end)

	for _, func in pairs({ 'ConquestFrame_UpdateJoinButton', 'HonorFrame_UpdateQueueButtons' }) do
		hooksecurefunc(func, function()
			for Section, Parent in pairs(Buttons) do
				local Button = Parent[Section]
				if Button.SelectedTexture:IsShown() then
					Button:SetBackdropBorderColor(unpack(AS.Color))
				else
					Button:SetBackdropBorderColor(unpack(AS.BorderColor))
				end
			end
		end)
	end

	PVPQueueFrame.CategoryButton1.Icon:SetTexture([[Interface\Icons\achievement_bg_winwsg]])
	PVPQueueFrame.CategoryButton2.Icon:SetTexture([[Interface\Icons\achievement_bg_killxenemies_generalsroom]])
	PVPQueueFrame.CategoryButton3.Icon:SetTexture([[Interface\Icons\ability_warrior_offensivestance]])

	AS:SkinButton(ConquestJoinButton, true)
	AS:StripTextures(ConquestFrame.ShadowOverlay)
	AS:SkinFrame(ConquestTooltip)

	for _, Frame in pairs({ HonorFrame, ConquestFrame }) do
		AS:StripTextures(Frame)
		AS:StripTextures(Frame.Inset)

		AS:SkinCheckBox(Frame.DPSIcon.checkButton, true)
		AS:SkinCheckBox(Frame.TankIcon.checkButton, true)
		AS:SkinCheckBox(Frame.HealerIcon.checkButton, true)

		AS:SkinStatusBar(Frame.ConquestBar, AS.Faction == "Alliance" and {0.05, 0.15, 0.36} or {0.63, 0.09, 0.09})
		Frame.ConquestBar.Reward.Ring:Hide()
		Frame.ConquestBar.Reward.CircleMask:Hide()
		AS:SkinTexture(Frame.ConquestBar.Reward.Icon, true)

		Frame.ConquestBar.Reward:SetPoint("LEFT", Frame.ConquestBar, "RIGHT", -8, 0)

		hooksecurefunc(Frame.ConquestBar.Reward.Icon, 'SetTexture', function(self) -- Code taken from :GetConquestLevelInfo the function isn't returning the correct id somehow.
			local Quality
			for _, questID in ipairs(C_QuestLine.GetQuestLineQuests(782)) do
				if not IsQuestFlaggedCompleted(questID) and not C_QuestLog.IsOnQuest(questID) then
					break
				end
				if HaveQuestRewardData(questID) then
					local itemID = select(6, GetQuestLogRewardInfo(1, questID))
					Quality = select(3, GetItemInfo(itemID))
				else
					C_TaskQuest.RequestPreloadRewardData(questID) -- Taken from WorldMapFrame
				end
			end
			if Quality then
				self.Backdrop:SetBackdropBorderColor(GetItemQualityColor(Quality))
			else
				self.Backdrop:SetBackdropBorderColor(unpack(AS.BorderColor))
			end
		end)
	end

	AS:UnregisterSkinEvent(addon, event)
end

function AS:Blizzard_BattlefieldMap(event, addon)
	if addon ~= 'Blizzard_BattlefieldMap' then return end

	local function GetOpacity()
		return 1 - (BattlefieldMapOptions and BattlefieldMapOptions.opacity or 1)
	end

	local oldAlpha = GetOpacity()

	AS:SkinBackdropFrame(BattlefieldMapFrame)
	BattlefieldMapFrame.Backdrop:SetOutside(BattlefieldMapFrame.ScrollContainer)
	BattlefieldMapFrame.Backdrop:SetBackdropColor(0, 0, 0, oldAlpha)

	BattlefieldMapFrame:SetClampedToScreen(true)
	BattlefieldMapFrame:EnableMouse(true)
	BattlefieldMapFrame:SetMovable(true)
	BattlefieldMapFrame:SetFrameStrata('LOW')

	BattlefieldMapFrame.BorderFrame.CloseButton:SetFrameLevel(BattlefieldMapFrame.BorderFrame.CloseButton:GetFrameLevel()+1)
	AS:SkinCloseButton(BattlefieldMapFrame.BorderFrame.CloseButton)
	AS:Kill(BattlefieldMapTab)

	local function InitializeOptionsDropDown()
		BattlefieldMapTab:InitializeOptionsDropDown()
	end

	BattlefieldMapFrame.ScrollContainer:HookScript("OnMouseUp", function(_, btn)
		if btn == "LeftButton" then
			BattlefieldMapTab:StopMovingOrSizing()
			BattlefieldMapTab:SetUserPlaced(true)
		elseif btn == "RightButton" then
			UIDropDownMenu_Initialize(BattlefieldMapTab.OptionsDropDown, InitializeOptionsDropDown, "MENU")
			ToggleDropDownMenu(1, nil, BattlefieldMapTab.OptionsDropDown, BattlefieldMapFrame:GetName(), 0, -4)
		end

		if OpacityFrame:IsShown() then
			OpacityFrame:Hide()
		end
	end)

	BattlefieldMapFrame.ScrollContainer:HookScript("OnMouseDown", function(_, btn)
		if btn == "LeftButton" and (BattlefieldMapOptions and not BattlefieldMapOptions.locked) then
			BattlefieldMapTab:StartMoving()
		end
	end)

	local function setBackdropAlpha()
		if BattlefieldMapFrame.Backdrop then
			BattlefieldMapFrame.Backdrop:SetBackdropColor(0, 0, 0, GetOpacity())
		end
	end

	hooksecurefunc(BattlefieldMapFrame, "SetGlobalAlpha", setBackdropAlpha)
	hooksecurefunc(BattlefieldMapFrame, "RefreshAlpha", function()
		oldAlpha = GetOpacity()
	end)

	local function setOldAlpha()
		if oldAlpha then
			BattlefieldMapFrame:SetGlobalAlpha(oldAlpha)
			oldAlpha = nil
		end
	end

	local function setRealAlpha()
		oldAlpha = GetOpacity()
		BattlefieldMapFrame:SetGlobalAlpha(1)
	end

	BattlefieldMapFrame:HookScript('OnShow', setBackdropAlpha)
	BattlefieldMapFrame.ScrollContainer:HookScript('OnLeave', setOldAlpha)
	BattlefieldMapFrame.ScrollContainer:HookScript('OnEnter', setRealAlpha)
	BattlefieldMapFrame.BorderFrame.CloseButton:HookScript('OnLeave', setOldAlpha)
	BattlefieldMapFrame.BorderFrame.CloseButton:HookScript('OnEnter', setRealAlpha)

	AS:UnregisterSkinEvent(addon, event)
end

function AS:Blizzard_WorldStateScore()
	-- Macro to show the WorldStateScoreFrame: /run WorldStateScoreFrame:Show()
	AS:SkinFrame(WorldStateScoreFrame)
	AS:StripTextures(WorldStateScoreScrollFrame)

	AS:SkinCloseButton(WorldStateScoreFrameCloseButton)
	AS:SkinScrollBar(WorldStateScoreScrollFrameScrollBar)

	AS:SkinButton(WorldStateScoreFrameLeaveButton)
	AS:SkinButton(WorldStateScoreFrameQueueButton)

	for i = 1, 3 do
		AS:SkinTab(_G["WorldStateScoreFrameTab"..i])
	end

	hooksecurefunc('PVPHonorXPBar_SetNextAvailable', function(XPBar)
		XPBar:StripTextures()

		if XPBar.Bar and not XPBar.Bar.Backdrop then
			AS:CreateBackdrop(XPBar.Bar)
			if XPBar.Bar.Background then
				XPBar.Bar.Background:SetInside(XPBar.Bar.Backdrop)
			end
			if XPBar.Bar.Spark then
				XPBar.Bar.Spark:SetAlpha(0)
			end
			if XPBar.Bar.OverlayFrame and XPBar.Bar.OverlayFrame.Text then
				XPBar.Bar.OverlayFrame.Text:ClearAllPoints()
				XPBar.Bar.OverlayFrame.Text:Point("CENTER", XPBar.Bar)
			end
		end

		if XPBar.PrestigeReward and XPBar.PrestigeReward.Accept then
			XPBar.PrestigeReward.Accept:ClearAllPoints()
			XPBar.PrestigeReward.Accept:SetPoint("TOP", XPBar.PrestigeReward, "BOTTOM", 0, 0)
			AS:SkinButton(XPBar.PrestigeReward.Accept)
		end

		if XPBar.NextAvailable then
			if XPBar.Bar then
				XPBar.NextAvailable:ClearAllPoints()
				XPBar.NextAvailable:SetPoint("LEFT", XPBar.Bar, "RIGHT", 0, -2)
			end

			if XPBar.NextAvailable.Icon then
				AS:StripTextures(XPBar.NextAvailable)
				AS:CreateBackdrop(XPBar.NextAvailable.Icon, true)
				XPBar.NextAvailable.Icon:SetDrawLayer("ARTWORK")
			end
		end
	end)
end

AS:RegisterSkin("Blizzard_PVPUI", AS.Blizzard_PVPUI, 'ADDON_LOADED')
AS:RegisterSkin("Blizzard_BattlefieldMap", AS.Blizzard_BattlefieldMap, 'ADDON_LOADED')
AS:RegisterSkin("Blizzard_WorldStateScore", AS.Blizzard_WorldStateScore)
