
local function createSpacer(width)
	local label = WhatsNext.AceGUI:Create('Label');
	label:SetText(" ");
	if not width or width <= 0 then
		label:SetFullWidth(true)
	else
		label:SetWidth(width)
	end
	return label
end

local function setColor(picker,color)
	picker:SetColor(color.r,color.g,color.b,color.a)
end

local function populateFromAppearence()
	local frame = WhatsNext.appearenceFrame;
	local app = frame.appearence

	frame.soundEffectDropDown:SetDisabled(not app.soundEffect)
	frame.playSoundButton:SetDisabled(not app.soundEffect)
	if app.soundEffect then
		frame.soundEffectBox:SetValue(true)
		frame.soundEffectDropDown:SetValue(app.soundEffect)
		frame.soundEffectLabel.label:SetTextColor(1,1,1,1)
	else
		frame.soundEffectBox:SetValue(false)
		frame.soundEffectLabel.label:SetTextColor(.5,.5,.5,1)
	end
	
	
	
	frame.textColorPicker:SetDisabled(not app.textColor)
	if app.textColor then
		setColor(frame.textColorPicker,app.textColor)
		frame.textColorBox:SetValue(true)
		frame.textColorLabel.label:SetTextColor(1,1,1,1)
	else
		frame.textColorBox:SetValue(false)
		frame.textColorLabel.label:SetTextColor(.5,.5,.5,1)
	end
	
	
	frame.bgColorPicker:SetDisabled(not app.backgroundColor)
	if app.backgroundColor then
		setColor(frame.bgColorPicker,app.backgroundColor)
		frame.bgColorBox:SetValue(true)
		frame.bgColorLabel.label:SetTextColor(1,1,1,1)
	else
		frame.bgColorBox:SetValue(false)
		frame.bgColorLabel.label:SetTextColor(.5,.5,.5,1)
	end
	
	
	frame.borderColorPicker:SetDisabled(not app.borderNoCDColor)
	if app.borderNoCDColor then
		setColor(frame.borderColorPicker,app.borderNoCDColor)
		frame.borderColorBox:SetValue(true)
		frame.borderColorLabel.label:SetTextColor(1,1,1,1)
	else
		frame.borderColorBox:SetValue(false)
		frame.borderColorLabel.label:SetTextColor(.5,.5,.5,1)
	end
	
	
	frame.borderCDColorPicker:SetDisabled(not app.borderFullCDColor)
	if app.borderFullCDColor then
		setColor(frame.borderCDColorPicker,app.borderFullCDColor)
		frame.borderCDBox:SetValue(true)
		frame.borderCDColorLabel.label:SetTextColor(1,1,1,1)
	else
		frame.borderCDBox:SetValue(false)
		frame.borderCDColorLabel.label:SetTextColor(.5,.5,.5,1)
	end
	
	
	frame.fontPicker:SetDisabled(not app.font)
	if app.font then
		frame.fontPicker:SetValue(app.font);
		frame.fontBox:SetValue(true)
		frame.fontLabel.label:SetTextColor(1,1,1,1)
	else
		frame.fontBox:SetValue(false)
		frame.fontLabel.label:SetTextColor(.5,.5,.5,1)
	end

	
	
	
end

function WhatsNext:ShowAppearenceFrame(index, entry, confirmFunction, cancelFunction)
	if not self.appearenceFrame then
		local frame = self.AceGUI:Create("Window")
		_G["WhatsNext_AppearenceFrame"] = frame
		frame:PauseLayout()
		
		self.appearenceFrame = frame
		WhatsNext_AddOptionsScreen(WhatsNext.appearenceFrame.frame)
		
		tinsert(UISpecialFrames, "WhatsNext_AppearenceFrame") 
		frame:SetWidth(599)
		frame:SetHeight(408)
		frame:SetTitle("What's Next? - Appearence / Sound")
		frame:SetStatusText("")
		frame:EnableResize(false);
		frame:SetCallback("OnClose", cancelFunc)
		frame:SetLayout("Flow")
		frame:SetAutoAdjustHeight(false)
		
		frame.frame.texture = frame.frame:CreateTexture(nil, "BACKGROUND")
		frame.frame.texture:SetColorTexture(0,0,0,1)
		frame.frame.texture:SetPoint("TOPLEFT",frame.frame,"TOPLEFT",8,-8)
		frame.frame.texture:SetPoint("BOTTOMRIGHT",frame.frame,"BOTTOMRIGHT",-6,8)
		
		local group = self.AceGUI:Create("InlineGroup")
		group:SetWidth(580)
		group:SetLayout("Flow")
		
		frame.soundEffectBox = WhatsNext.AceGUI:Create("CheckBox")
		frame.soundEffectBox:SetWidth(20);
		frame.soundEffectBox:SetCallback('OnValueChanged', function(btn,event,value)
			frame.soundEffectDropDown:SetDisabled(not value)
			frame.playSoundButton:SetDisabled(not value)
			if value then
				frame.soundEffectLabel.label:SetTextColor(1,1,1,1)
			else
				frame.soundEffectLabel.label:SetTextColor(.5,.5,.5,1)
				frame.appearence.soundEffect = nil
			end
		end);
		group:AddChild(frame.soundEffectBox)
		
		
		frame.soundEffectLabel = WhatsNext.AceGUI:Create('Label');
		frame.soundEffectLabel:SetText("Sound Effect: ");
		frame.soundEffectLabel:SetFont("Fonts\\FRIZQT__.TTF", 13, "")
		frame.soundEffectLabel:SetWidth(140)
		frame.soundEffectLabel.label:SetJustifyH("RIGHT")
		frame.soundEffectLabel.label:SetTextColor(.5,.5,.5,1)
		group:AddChild(frame.soundEffectLabel)

		frame.soundEffectDropDown = WhatsNext.AceGUI:Create("Dropdown")
		frame.soundEffectDropDown:SetWidth(250)
		frame.soundEffectDropDown:SetList(WhatsNext.valueLists.soundEffects )
		frame.soundEffectDropDown:SetValue(89686)
		frame.soundEffectDropDown:SetDisabled(true)
		frame.soundEffectDropDown:SetCallback('OnValueChanged', function(btn,event,value)
			frame.appearence.soundEffect = value
		end);
		
		group:AddChild(frame.soundEffectDropDown)
		frame.playSoundButton = self.AceGUI:Create('Button');
		frame.playSoundButton:SetWidth(70);
		frame.playSoundButton:SetText('Play');
		frame.playSoundButton:SetDisabled(true)
		frame.playSoundButton:SetCallback('OnClick', function()
			PlaySound(frame.soundEffectDropDown:GetValue(), "Master");
		end);
		group:AddChild(frame.playSoundButton)
		frame:AddChild(group)

		
		group = self.AceGUI:Create("InlineGroup")
		group:SetWidth(287)
		group:SetLayout("Flow")
		
		
		frame.textColorBox = WhatsNext.AceGUI:Create("CheckBox")
		frame.textColorBox:SetWidth(20);
		frame.textColorBox:SetCallback('OnValueChanged', function(btn,event,value)
			frame.textColorPicker:SetDisabled(not value)
			if value then
				frame.textColorLabel.label:SetTextColor(1,1,1,1)
			else
				frame.textColorLabel.label:SetTextColor(.5,.5,.5,1)
				frame.appearence.textColor = nil
			end
		end);
		group:AddChild(frame.textColorBox)
		
		frame.textColorLabel = WhatsNext.AceGUI:Create('Label');
		frame.textColorLabel:SetText("Text: ");
		frame.textColorLabel:SetFont("Fonts\\FRIZQT__.TTF", 13, "")
		frame.textColorLabel:SetWidth(140)
		frame.textColorLabel.label:SetJustifyH("RIGHT")
		frame.textColorLabel.label:SetTextColor(.5,.5,.5,1)
		group:AddChild(frame.textColorLabel)

		frame.textColorPicker = WhatsNext.AceGUI:Create("ColorPicker")
		frame.textColorPicker:SetWidth(20)
		frame.textColorPicker:SetDisabled(true)
		frame.textColorPicker:SetCallback('OnValueConfirmed', function(btn,event,r,g,b,a)
			frame.appearence.textColor = {
				['r'] = r,
				['g'] = g,
				['b'] = b, 
				['a'] = a
			}
		end);
		--WhatsNext.db.profile.textColor
		group:AddChild(frame.textColorPicker)
		frame:AddChild(group)
		
		group = self.AceGUI:Create("InlineGroup")
		group:SetWidth(287)
		group:SetLayout("Flow")
		
		frame.borderColorBox = WhatsNext.AceGUI:Create("CheckBox")
		frame.borderColorBox:SetWidth(20);
		frame.borderColorBox:SetCallback('OnValueChanged', function(btn,event,value)
			frame.borderColorPicker:SetDisabled(not value)
			if value then
				frame.borderColorLabel.label:SetTextColor(1,1,1,1)
			else
				frame.borderColorLabel.label:SetTextColor(.5,.5,.5,1)
				frame.appearence.borderNoCDColor = nil
			end
		end);
		group:AddChild(frame.borderColorBox)
		
		frame.borderColorLabel = WhatsNext.AceGUI:Create('Label');
		frame.borderColorLabel:SetText("Border Off CD: ");
		frame.borderColorLabel:SetFont("Fonts\\FRIZQT__.TTF", 13, "")
		frame.borderColorLabel:SetWidth(140)
		frame.borderColorLabel.label:SetJustifyH("RIGHT")
		frame.borderColorLabel.label:SetTextColor(.5,.5,.5,1)
		group:AddChild(frame.borderColorLabel)

		frame.borderColorPicker = WhatsNext.AceGUI:Create("ColorPicker")
		frame.borderColorPicker:SetWidth(20)
		frame.borderColorPicker:SetDisabled(true)
		frame.borderColorPicker:SetCallback('OnValueConfirmed', function(btn,event,r,g,b,a)
			frame.appearence.borderNoCDColor = {
				['r'] = r,
				['g'] = g,
				['b'] = b, 
				['a'] = a
			}
		end);
		--WhatsNext.db.profile.textColor
		group:AddChild(frame.borderColorPicker)
		
		frame:AddChild(group)
		
		
		
		group = self.AceGUI:Create("InlineGroup")
		group:SetWidth(287)
		group:SetLayout("Flow")
		frame.bgColorBox = WhatsNext.AceGUI:Create("CheckBox")
		frame.bgColorBox:SetWidth(20);
		frame.bgColorBox:SetCallback('OnValueChanged', function(btn,event,value)
			frame.bgColorPicker:SetDisabled(not value)
			if value then
				frame.bgColorLabel.label:SetTextColor(1,1,1,1)
			else
				frame.bgColorLabel.label:SetTextColor(.5,.5,.5,1)
				frame.appearence.backgroundColor = nil
			end
		end);
		group:AddChild(frame.bgColorBox)
		
		frame.bgColorLabel = WhatsNext.AceGUI:Create('Label');
		frame.bgColorLabel:SetText("Background: ");
		frame.bgColorLabel:SetFont("Fonts\\FRIZQT__.TTF", 13, "")
		frame.bgColorLabel:SetWidth(140)
		frame.bgColorLabel.label:SetJustifyH("RIGHT")
		frame.bgColorLabel.label:SetTextColor(.5,.5,.5,1)
		group:AddChild(frame.bgColorLabel)

		frame.bgColorPicker = WhatsNext.AceGUI:Create("ColorPicker")
		frame.bgColorPicker:SetWidth(20)
		frame.bgColorPicker:SetDisabled(true)
		frame.bgColorPicker:SetCallback('OnValueConfirmed', function(btn,event,r,g,b,a)
			frame.appearence.backgroundColor = {
				['r'] = r,
				['g'] = g,
				['b'] = b, 
				['a'] = a
			}
		end);
		
		group:AddChild(frame.bgColorPicker)
		frame:AddChild(group)
		
		
		
		group = self.AceGUI:Create("InlineGroup")
		group:SetWidth(287)
		group:SetLayout("Flow")
		frame.borderCDBox = WhatsNext.AceGUI:Create("CheckBox")
		frame.borderCDBox:SetWidth(20);
		frame.borderCDBox:SetCallback('OnValueChanged', function(btn,event,value)
			frame.borderCDColorPicker:SetDisabled(not value)
			if value then
				frame.borderCDColorLabel.label:SetTextColor(1,1,1,1)
			else
				frame.borderCDColorLabel.label:SetTextColor(.5,.5,.5,1)
				frame.appearence.borderFullCDColor = nil
			end
		end);
		group:AddChild(frame.borderCDBox)
		
		frame.borderCDColorLabel = WhatsNext.AceGUI:Create('Label');
		frame.borderCDColorLabel:SetText("Border Full CD: ");
		frame.borderCDColorLabel:SetFont("Fonts\\FRIZQT__.TTF", 13, "")
		frame.borderCDColorLabel:SetWidth(140)
		frame.borderCDColorLabel.label:SetJustifyH("RIGHT")
		frame.borderCDColorLabel.label:SetTextColor(.5,.5,.5,1)
		group:AddChild(frame.borderCDColorLabel)

		frame.borderCDColorPicker = WhatsNext.AceGUI:Create("ColorPicker")
		frame.borderCDColorPicker:SetWidth(20)
		frame.borderCDColorPicker:SetDisabled(true)
		frame.borderCDColorPicker:SetCallback('OnValueConfirmed', function(btn,event,r,g,b,a)
			frame.appearence.borderFullCDColor = {
				['r'] = r,
				['g'] = g,
				['b'] = b, 
				['a'] = a
			}
		end);
		
		group:AddChild(frame.borderCDColorPicker)
		frame:AddChild(group)
		
		
		
		group = self.AceGUI:Create("InlineGroup")
		group:SetWidth(580)
		group:SetHeight(30)
		group:SetLayout("Flow")
		frame.fontBox = WhatsNext.AceGUI:Create("CheckBox")
		frame.fontBox:SetWidth(20);
		frame.fontBox:SetCallback('OnValueChanged', function(btn,event,value)
			frame.fontPicker:SetDisabled(not value)
			if value then
				frame.fontLabel.label:SetTextColor(1,1,1,1)
			else
				frame.fontLabel.label:SetTextColor(.5,.5,.5,1)
				frame.appearence.font = nil
			end
		end);
		group:AddChild(frame.fontBox)
		
		frame.fontLabel = WhatsNext.AceGUI:Create('Label');
		frame.fontLabel:SetText("Font: ");
		frame.fontLabel:SetFont("Fonts\\FRIZQT__.TTF", 13, "")
		frame.fontLabel:SetWidth(140)
		frame.fontLabel.label:SetJustifyH("RIGHT")
		frame.fontLabel.label:SetTextColor(.5,.5,.5,1)
		group:AddChild(frame.fontLabel)

		frame.fontPicker = WhatsNext.AceGUI:Create("LSM30_Font")
		frame.fontPicker:SetWidth(250)
		frame.fontPicker:SetDisabled(true)
		frame.fontPicker:SetList(WhatsNext.SharedMedia:HashTable("font"))
		frame.fontPicker:SetValue(WhatsNext.db.profile.font);
		frame.fontPicker:SetCallback('OnValueChanged', function(btn,event,value)
			frame.fontPicker:SetValue(value)
			frame.appearence.font = value
		end);
		group:AddChild(frame.fontPicker)
		frame:AddChild(group)
		
		
		frame:AddChild(createSpacer())
		frame:AddChild(createSpacer())
		
		
		frame.okayButton = WhatsNext.AceGUI:Create('Button');
		frame.okayButton:SetWidth(80);
		frame.okayButton:SetText('Okay');
		frame.okayButton:SetCallback('OnClick', function()
			frame.entry.appearence = frame.appearence
			frame.confirmFunction(frame.sourceIndex,self.appearenceFrame.entry)
		end);
		
		frame.cancelButton = WhatsNext.AceGUI:Create('Button');
		frame.cancelButton:SetWidth(80);
		frame.cancelButton:SetText('Cancel');
		frame.cancelButton:SetCallback('OnClick', function() 
			frame.cancelFunction();
		end);
		
		frame:AddChild(createSpacer(415))
		frame:AddChild(frame.okayButton)
		frame:AddChild(frame.cancelButton)
		
		frame:ResumeLayout()
		frame:DoLayout()
	end
	
	WhatsNext_ShowOptionsScreen(WhatsNext.appearenceFrame.frame)
	
	self.appearenceFrame.entry = entry;
	self.appearenceFrame.appearence = entry.appearence and DeepCopy(entry.appearence) or {};

	self.appearenceFrame:SetCallback("OnClose", cancelFunction)

	self.appearenceFrame.confirmFunction = confirmFunction
	self.appearenceFrame.cancelFunction = cancelFunction
	populateFromAppearence()
end