local MER, E, L, V, P, G = unpack(select(2, ...))
local MERS = E:GetModule("muiSkins")
local S = E:GetModule("Skins")

-- Cache global variables
-- Lua functions
local _G = _G

-- WoW API / Variables

--Global variables that we don't cache, list them here for the mikk's Find Globals script
-- GLOBALS:

local function styleQuestInfo()
	if E.private.skins.blizzard.enable ~= true or E.private.skins.blizzard.quest ~= true or E.private.muiSkins.blizzard.quest ~= true then return; end

	local function restyleSpellButton(bu)
		local name = bu:GetName()
		local icon = bu.Icon

		_G[name.."NameFrame"]:Hide()
		_G[name.."SpellBorder"]:Hide()

		icon:SetPoint("TOPLEFT", 3, -2)
		icon:SetDrawLayer("ARTWORK")
		icon:SetTexCoord(.08, .92, .08, .92)
		MERS:CreateBG(icon)

		local bg = _G.CreateFrame("Frame", nil, bu)
		bg:SetPoint("TOPLEFT", 2, -1)
		bg:SetPoint("BOTTOMRIGHT", 0, 14)
		bg:SetFrameLevel(0)
		MERS:CreateBD(bg, .25)
	end

	local function colourObjectivesText()
		if not QuestInfoFrame.questLog then return end

		local objectivesTable = QuestInfoObjectivesFrame.Objectives
		local numVisibleObjectives = 0

		for i = 1, GetNumQuestLeaderBoards() do
			local _, objectiveType, isCompleted = GetQuestLogLeaderBoard(i)

			if (objectiveType ~= "spell" and objectiveType ~= "log" and numVisibleObjectives < MAX_OBJECTIVES) then
				numVisibleObjectives = numVisibleObjectives + 1
				local objective = objectivesTable[numVisibleObjectives]

				if isCompleted then
					objective:SetTextColor(34/255, 255/255, 0/255)
				else
					objective:SetTextColor(1, 1, 1)
				end
			end
		end
	end

	local SkinQuestText
	local function MERS_SetTextColor(self, red, green, blue)
		if self.settingFont then return end
		self.settingFont = true
		self:SetTextColor(1, 1, 1)
		self.settingFont = nil
	end

	function SkinQuestText(font, hasShadow)
		if hasShadow then
			font:SetShadowColor(0.3, 0.3, 0.3)
		end
		font:SetTextColor(1, 1, 1)
		hooksecurefunc(font, "SetTextColor", MERS_SetTextColor)
	end

	hooksecurefunc("QuestMapFrame_ShowQuestDetails", colourObjectivesText)
	hooksecurefunc("QuestInfo_Display", function(template, parentFrame, acceptButton, material, mapView)
		local rewardsFrame = QuestInfoFrame.rewardsFrame
		local isQuestLog = QuestInfoFrame.questLog ~= nil
		local isMapQuest = rewardsFrame == MapQuestInfoRewardsFrame

		colourObjectivesText()

		if ( template.canHaveSealMaterial ) then
			local questFrame = parentFrame:GetParent():GetParent()
			questFrame.SealMaterialBG:Hide()
		end

		local numSpellRewards = isQuestLog and GetNumQuestLogRewardSpells() or GetNumRewardSpells()
		if numSpellRewards > 0 then
			-- Spell Headers
			for spellHeader in rewardsFrame.spellHeaderPool:EnumerateActive() do
				spellHeader:SetVertexColor(1, 1, 1)
			end
			-- Follower Rewards
			for followerReward in rewardsFrame.followerRewardPool:EnumerateActive() do
				if not followerReward.isSkinned then
					followerReward.PortraitFrame:SetScale(1)
					followerReward.PortraitFrame:ClearAllPoints()
					followerReward.PortraitFrame:SetPoint("TOPLEFT")
					if isMapQuest then
						followerReward.PortraitFrame.Portrait:SetSize(29, 29)
					end

					followerReward.BG:Hide()
					followerReward.BG:SetPoint("TOPLEFT", followerReward.PortraitFrame, "TOPRIGHT")
					followerReward.BG:SetPoint("BOTTOMRIGHT")
					MERS:CreateBD(followerReward, .25)
					followerReward:SetHeight(followerReward.PortraitFrame:GetHeight())

					if not isMapQuest then
						followerReward.Class:SetWidth(45)
					end

					followerReward.isSkinned = true
				end
				followerReward.PortraitFrame:SetBackdropBorderColor(followerReward.PortraitFrame.PortraitRingQuality:GetVertexColor())
			end
			-- Spell Rewards
			for spellReward in rewardsFrame.spellRewardPool:EnumerateActive() do
				if not spellReward.isSkinned then
					if isMapQuest then
						MERS:SmallItemButtonTemplate(spellReward)
					else
						MERS:LargeItemButtonTemplate(spellReward)

						select(3, spellReward:GetRegions()):Hide() --border
						spellReward.Icon:SetPoint("TOPLEFT", 0, 0)
						spellReward:SetHitRectInsets(0,0,0,0)
						spellReward:SetSize(147, 41)
					end
					spellReward.isSkinned = true
				end
			end
		end
	end)

	hooksecurefunc("QuestInfo_GetRewardButton", function(rewardsFrame, index)
		local button = rewardsFrame.RewardButtons[index]

		if not button.restyled then
			if rewardsFrame == MapQuestInfoRewardsFrame then
				MERS:SmallItemButtonTemplate(button)
			else
				MERS:LargeItemButtonTemplate(button)
			end
			button.restyled = true
		end
	end)

	restyleSpellButton(QuestInfoSpellObjectiveFrame)
	SkinQuestText(QuestInfoSpellObjectiveLearnLabel)

	--[[ QuestInfoRewardsFrame ]]
	local QuestInfoRewardsFrame = _G["QuestInfoRewardsFrame"]
	SkinQuestText(QuestInfoRewardsFrame.Header, true)
	SkinQuestText(QuestInfoRewardsFrame.ItemChooseText)
	SkinQuestText(QuestInfoRewardsFrame.ItemReceiveText)
	SkinQuestText(QuestInfoRewardsFrame.PlayerTitleText)

	for i, name in next, {"HonorFrame", "SkillPointFrame", "ArtifactXPFrame"} do
		MERS:LargeItemButtonTemplate(QuestInfoRewardsFrame[name])
	end
	SkinQuestText(QuestInfoRewardsFrame.XPFrame.ReceiveText)

	local QuestInfoPlayerTitleFrame = _G["QuestInfoPlayerTitleFrame"]
	QuestInfoPlayerTitleFrame.FrameLeft:Hide()
	QuestInfoPlayerTitleFrame.FrameCenter:Hide()
	QuestInfoPlayerTitleFrame.FrameRight:Hide()

	local titleBG = CreateFrame("Frame", nil, QuestInfoPlayerTitleFrame)
	titleBG:SetPoint("TOPLEFT", QuestInfoPlayerTitleFrame.FrameLeft, -2, 0)
	titleBG:SetPoint("BOTTOMRIGHT", QuestInfoPlayerTitleFrame.FrameRight, 0, -1)
	MERS:CreateBD(titleBG, .25)

	local ItemHighlight = QuestInfoRewardsFrame.ItemHighlight
	ItemHighlight:GetRegions():Hide()

	--[[ MapQuestInfoRewardsFrame ]]
	for i, name in next, {"HonorFrame", "MoneyFrame", "SkillPointFrame", "XPFrame", "ArtifactXPFrame", "TitleFrame"} do
		MERS:SmallItemButtonTemplate(MapQuestInfoRewardsFrame[name])
	end
	MapQuestInfoRewardsFrame.XPFrame.Name:SetShadowOffset(0, 0)

	--[[ QuestInfoFrame ]]
	SkinQuestText(QuestInfoTitleHeader, true)
	SkinQuestText(QuestInfoObjectivesText)
	SkinQuestText(QuestInfoRewardText)

	hooksecurefunc(QuestInfoRequiredMoneyText, "SetTextColor", function(self, red, green, blue)
		if red == 0 then
			self:SetTextColor(.8, .8, .8)
		elseif red == .2 then
			self:SetTextColor(1, 1, 1)
		end
	end)

	SkinQuestText(QuestInfoGroupSize)
	SkinQuestText(QuestInfoDescriptionHeader, true)
	SkinQuestText(QuestInfoObjectivesHeader, true)
	SkinQuestText(QuestInfoDescriptionText)

	--[= QuestInfoSealFrame =]
	local mask = QuestInfoSealFrame:CreateMaskTexture(nil, "BACKGROUND")
	mask:SetTexture([[Interface/QuestionFrame/answer-ChromieScenario-Chromie]], "CLAMPTOBLACKADDITIVE", "CLAMPTOBLACKADDITIVE")
	mask:SetTexCoord(0, 0.5, 0, 0.5)
	mask:SetPoint("TOPLEFT", QuestInfoSealFrame.Text, -28, 6)
	mask:SetPoint("BOTTOMRIGHT", QuestInfoSealFrame.Text, 24, -5)

	local bg = QuestInfoSealFrame:CreateTexture(nil, "BACKGROUND")
	bg:SetColorTexture(0, 0, 0, 0.25)
	bg:SetAllPoints(mask)
	bg:AddMaskTexture(mask)

	QuestInfoSealFrame.Text:SetShadowColor(103/255, 103/255, 103/255)
	QuestInfoSealFrame.Text:SetShadowOffset(0.6, -0.6)
end

S:AddCallback("mUIQuestInfo", styleQuestInfo)