local SML = LibStub("LibSharedMedia-3.0");

local function SetTextColor(frame, color)
	if color then
		frame.spellName:SetTextColor(color.r, color.g, color.b, color.a)
		frame.cd:SetTextColor(color.r, color.g, color.b, color.a)
		frame.blockIndex:SetTextColor(color.r, color.g, color.b, color.a)
	end
end

local function SetTextFont(frame,font,outline)
	if font and outline then
		frame.spellName:SetFont(SML:Fetch('font', font), 16, outline)
		frame.cd:SetFont(SML:Fetch('font', font), 10, outline)
		frame.blockIndex:SetFont(SML:Fetch('font', font), 9, outline)
	end
end

local function SetBackgroundColor(frame,color)
	if color then
		frame.bg:SetColorTexture(color.r,color.g,color.b,color.a)
	end
end

function WhatsNext:ApplyAppearance(frame, block, force)
	if block and block.appearance then
		SetBackgroundColor(frame, block and block.appearance and block.appearance.backgroundColor or WhatsNext.db.profile.backgroundColor)
		SetTextColor(frame, block and block.appearance and block.appearance.textColor or WhatsNext.db.profile.textColor)
		SetTextFont(frame, 	block and block.appearance and block.appearance.font or WhatsNext.db.profile.font, 
							block and block.appearance and block.appearance.outline or WhatsNext.db.profile.outline)
	else
		SetBackgroundColor(frame,WhatsNext.db.profile.backgroundColor)
		SetTextColor(frame, WhatsNext.db.profile.textColor)
		SetTextFont(frame, WhatsNext.db.profile.font, WhatsNext.db.profile.outline)
	end
	frame:UpdateLayout()
end


function WhatsNext:ApplyBaseAppearance(frame)

	frame:EnableMouse(not self.db.profile.locked)
	
	if self.db.profile.showBlockIndex then
		frame.blockIndex:Show()
	else
		frame.blockIndex:Hide()
	end
	
	frame:ClearAllPoints()
	if frame.index == 1 then
		frame:SetScale(self.db.profile.scale)
		local scale = frame:GetEffectiveScale()
		local pos = self.db.profile.pos
		frame:SetPoint(pos.p1,nil,pos.p2,pos.x/scale,pos.y/scale)
	else
		frame:SetScale(self.db.profile.subsequentScale)
		if self.db.profile.stackDirection == "bottom" then
			frame:SetPoint("TOPLEFT",WhatsNext:GetSuggestionFrame(frame.index-1),"BOTTOMLEFT",2.2,-4)
		elseif self.db.profile.stackDirection == "top" then
			frame:SetPoint("BOTTOMLEFT",WhatsNext:GetSuggestionFrame(frame.index-1),"TOPLEFT",2.2,4)
		elseif self.db.profile.stackDirection == "left" then
			frame:SetPoint("RIGHT",WhatsNext:GetSuggestionFrame(frame.index-1),"LEFT",-4,0)
		else
			frame:SetPoint("LEFT",WhatsNext:GetSuggestionFrame(frame.index-1),"RIGHT",4,0)
		end
	end
	
	frame:UpdateLayout()
end