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)
	local hasAppearence = block and block.appearance
	
	if hasAppearence and block.appearance.soundEffect then
		PlaySound(block.appearance.soundEffect, "Master");
	end
	SetBackgroundColor(frame, hasAppearence and block.appearance.backgroundColor or WhatsNext.db.profile.backgroundColor)
	SetTextColor(frame, hasAppearence and block.appearance.textColor or WhatsNext.db.profile.textColor)
	SetTextFont(frame, 	hasAppearence and block.appearance.font or WhatsNext.db.profile.font, 
						hasAppearence and block.appearance.outline or WhatsNext.db.profile.outline)
						
	frame.borderNoCDColor = hasAppearence and block.appearance.borderNoCDColor or WhatsNext.db.profile.borderNoCDColor
	frame.borderFullCDColor	 = hasAppearence and block.appearance.borderFullCDColor or WhatsNext.db.profile.borderFullCDColor
	
	frame:UpdateLayout()
end


function WhatsNext:ApplyBaseAppearance(frame)

	frame:EnableMouse(not self.db.profile.locked)
	
	frame.borderNoCDColor = WhatsNext.db.profile.borderNoCDColor
	frame.borderFullCDColor	 = WhatsNext.db.profile.borderFullCDColor
	
	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